dart-lang / dart-docker

Docker images for the Dart programming language (https://dart.dev)
BSD 3-Clause "New" or "Revised" License
70 stars 15 forks source link

Any example of how to use the Dart VM for local development with Docker? #79

Closed bradcypert closed 2 years ago

bradcypert commented 2 years ago

This may be out of the scope of this project and if so, my apologies, but I'm trying to setup Dart with Docker in such a way that I can use the Dart VM instead of an AoT executable. I want to do this to support local development as if I was running dart run.

I'm so very close to having it working but Im not sure what I need to change to get it to successfully run!

My issue seems to be stemming from errors that look like this:

lib/controllers/account_controller.dart:7:8: Error: Error when reading '/Users/bradcypert/.pub-cache/hosted/pub.dartlang.org/steward-0.1.0/lib/steward.dart': No such file or directory

lib/models/license.dart:1:8: Error: Error when reading '/Users/bradcypert/.pub-cache/hosted/pub.dartlang.org/stormberry-0.4.0/lib/stormberry.dart': No such file or directory

lib/models/account.dart:3:8: Error: Error when reading '/Users/bradcypert/.pub-cache/hosted/pub.dartlang.org/uuid-3.0.5/lib/uuid.dart': No such file or directory

Namely, any and all of my dependencies are not being found when running inside of the container. It looks like you can set the pub cache directory via an environment variable, and I had some success setting that to my local directory, but then running the application outside of docker becomes a pain. I have those dependencies installed locally and I can run my app by running dart run lib/app.dart as long as I'm not running the app through docker (but this isnt ideal).

I've tried running the AoT version shared in this project but a library that im using uses Dart Mirrors, too.

Any tips?

Thanks and files are attached for context.

Here's my dockerfile:

FROM dart:2.16
WORKDIR /app
COPY . /app
RUN dart pub get
CMD /bin/bash -c "dart run lib/app.dart"

and my compose:

version: "3.9"
services:
  server:
    build: .
    ports:
      - "4040:4040"
    volumes:
      - .:/app
cpswan commented 2 years ago

You may need to add an ENV HOMEDIR= to your Dockerfile to get dart pub get to drop the files into the right place.

Here's a Dockerfile that I know works running an app in the VM.

bradcypert commented 2 years ago

@cpswan I will try this out soon! thank you so much!

bradcypert commented 2 years ago

I finally fully understood what my issue was. My issue was that I was using a volume that was my entire app folder and it was pushing .dart_tool/ into the container as well, which was referencing paths on my host machine.

My docker file ultimately ended up like this:

FROM dart:2.17
ENV HOMEDIR=/manudac
WORKDIR /app
COPY . .
RUN rm /app/pubspec.lock
RUN rm -rf .dart_tool
RUN dart pub get --no-offline
RUN dart pub update
EXPOSE 4040
CMD ["dart", "run", "lib/app.dart"]

I can probably clean that up a bit, but at least its no longer a blocker. Thanks again @cpswan ! :)