|
| 1 | +--- |
| 2 | +sidebar_position: 2 |
| 3 | +title: 🐳 Custom Dockerfile |
| 4 | +--- |
| 5 | + |
| 6 | +<!-- cSpell:ignore WORKDIR --> |
| 7 | + |
| 8 | +# Custom Dockerfile 🐳 |
| 9 | + |
| 10 | +A `Dockerfile` is automatically generated when creating a production build via the `dart_frog build` command. |
| 11 | + |
| 12 | +To use a custom `Dockerfile`, you can create a `Dockerfile` at the root of the project. |
| 13 | + |
| 14 | +``` |
| 15 | +├── Dockerfile <-- NEW |
| 16 | +├── README.md |
| 17 | +├── analysis_options.yaml |
| 18 | +├── pubspec.lock |
| 19 | +├── pubspec.yaml |
| 20 | +├── routes |
| 21 | +│ └── index.dart |
| 22 | +└── test |
| 23 | + └── routes |
| 24 | + └── index_test.dart |
| 25 | +``` |
| 26 | + |
| 27 | +The following `Dockerfile` is a good starting point: |
| 28 | + |
| 29 | +```dockerfile |
| 30 | +# An example of using a custom Dockerfile with Dart Frog |
| 31 | +# Official Dart image: https://hub.docker.com/_/dart |
| 32 | +# Specify the Dart SDK base image version using dart:<version> (ex: dart:2.17) |
| 33 | +FROM dart:stable AS build |
| 34 | + |
| 35 | +WORKDIR /app |
| 36 | + |
| 37 | +# Resolve app dependencies. |
| 38 | +COPY pubspec.* ./ |
| 39 | +RUN dart pub get |
| 40 | + |
| 41 | +# Copy app source code and AOT compile it. |
| 42 | +COPY . . |
| 43 | + |
| 44 | +# Generate a production build. |
| 45 | +RUN dart pub global activate dart_frog_cli |
| 46 | +RUN dart pub global run dart_frog_cli:dart_frog build |
| 47 | + |
| 48 | +# Ensure packages are still up-to-date if anything has changed. |
| 49 | +RUN dart pub get --offline |
| 50 | +RUN dart compile exe build/bin/server.dart -o build/bin/server |
| 51 | + |
| 52 | +# Build minimal serving image from AOT-compiled `/server` and required system |
| 53 | +# libraries and configuration files stored in `/runtime/` from the build stage. |
| 54 | +FROM scratch |
| 55 | +COPY --from=build /runtime/ / |
| 56 | +COPY --from=build /app/build/bin/server /app/bin/ |
| 57 | +# Uncomment the following line if you are serving static files. |
| 58 | +# COPY --from=build /app/build/public /public/ |
| 59 | + |
| 60 | +# Start the server. |
| 61 | +CMD ["/app/bin/server"] |
| 62 | +``` |
| 63 | + |
| 64 | +To build your Docker image run: |
| 65 | + |
| 66 | +```sh |
| 67 | +docker build . -t dart-frog-app |
| 68 | +``` |
| 69 | + |
| 70 | +Then run your Docker container via: |
| 71 | + |
| 72 | +```sh |
| 73 | +docker run -i -t -p 8080:8080 dart-frog-app |
| 74 | +``` |
0 commit comments