Skip to content

Commit 22c624e

Browse files
authored
docs(advanced): custom Dockerfile (#435)
1 parent 60a88a3 commit 22c624e

File tree

10 files changed

+124
-46
lines changed

10 files changed

+124
-46
lines changed

.github/workflows/.docker_tests.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ on:
77
required: false
88
type: string
99
default: ""
10+
docker_directory:
11+
required: false
12+
type: string
13+
default: "build"
1014
test_directory:
1115
required: false
1216
type: string
@@ -41,7 +45,7 @@ jobs:
4145
run: dart_frog build
4246

4347
- name: 🐳 Build Docker Image
44-
run: docker build -q build -t e2e-image
48+
run: docker build -q ${{inputs.docker_directory}} -t e2e-image
4549

4650
- name: ⚡️ Run Docker Image
4751
run: docker run -d -p 8080:8080 --name e2e --rm e2e-image

.github/workflows/examples_kitchen_sink.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,10 @@ jobs:
3232
with:
3333
setup: rm pubspec_overrides.yaml && dart pub global activate --source path ../../packages/dart_frog_cli
3434
working_directory: examples/kitchen_sink
35+
36+
docker_custom:
37+
uses: ./.github/workflows/.docker_tests.yaml
38+
with:
39+
setup: rm pubspec_overrides.yaml && dart pub global activate --source path ../../packages/dart_frog_cli
40+
docker_directory: .
41+
working_directory: examples/kitchen_sink
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
```

docs/docs/advanced/powered_by_header.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 2
2+
sidebar_position: 3
33
title: 🔋 Powered By Header
44
---
55

docs/docusaurus.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ const config = {
153153
copyright: `Copyright © ${new Date().getFullYear()} Very Good Ventures.<br/>Built with 💙 by <a target="_blank" rel="noopener" aria-label="Very Good Ventures" href="https://verygood.ventures">Very Good Ventures</a>.`,
154154
},
155155
prism: {
156-
additionalLanguages: ['bash', 'dart', 'yaml'],
156+
additionalLanguages: ['bash', 'dart', 'docker', 'yaml'],
157157
theme: lightCodeTheme,
158158
darkTheme: darkCodeTheme,
159159
},

docs/yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2958,9 +2958,9 @@ caniuse-api@^3.0.0:
29582958
lodash.uniq "^4.5.0"
29592959

29602960
caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001335, caniuse-lite@^1.0.30001349:
2961-
version "1.0.30001355"
2962-
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001355.tgz#e240b7177443ed0198c737a7f609536976701c77"
2963-
integrity sha512-Sd6pjJHF27LzCB7pT7qs+kuX2ndurzCzkpJl6Qct7LPSZ9jn0bkOA8mdgMgmqnQAWLVOOGjLpc+66V57eLtb1g==
2961+
version "1.0.30001434"
2962+
resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001434.tgz"
2963+
integrity sha512-aOBHrLmTQw//WFa2rcF1If9fa3ypkC1wzqqiKHgfdrXTWcU8C4gKVZT77eQAPWN1APys3+uQ0Df07rKauXGEYA==
29642964

29652965
ccount@^1.0.0:
29662966
version "1.1.0"

examples/kitchen_sink/Dockerfile

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# An example of using a custom Dockerfile with Dart Frog
2+
# Official Dart image: https://hub.docker.com/_/dart
3+
# Specify the Dart SDK base image version using dart:<version> (ex: dart:2.17)
4+
FROM dart:stable AS build
5+
6+
WORKDIR /app
7+
8+
# Resolve app dependencies.
9+
COPY pubspec.* ./
10+
RUN dart pub get
11+
12+
# Copy app source code and AOT compile it.
13+
COPY . .
14+
15+
# Generate a production build.
16+
RUN dart pub global activate dart_frog_cli
17+
RUN dart pub global run dart_frog_cli:dart_frog build
18+
19+
# Ensure packages are still up-to-date if anything has changed.
20+
RUN dart pub get --offline
21+
RUN dart compile exe build/bin/server.dart -o build/bin/server
22+
23+
# Build minimal serving image from AOT-compiled `/server` and required system
24+
# libraries and configuration files stored in `/runtime/` from the build stage.
25+
FROM scratch
26+
COPY --from=build /runtime/ /
27+
COPY --from=build /app/build/bin/server /app/bin/
28+
COPY --from=build /app/build/public /public/
29+
30+
# Start the server.
31+
CMD ["/app/bin/server"]

packages/dart_frog_cli/e2e/test/dart_frog_build_test.dart

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -45,44 +45,6 @@ void main() {
4545
expect(path.basename(entities.first.path), equals(projectName));
4646
});
4747

48-
test('creates the project directory with a custom Dockerfile', () async {
49-
final customDockerFileContents = '''
50-
# My Custom Dockerfile
51-
FROM dart:2.18 AS build
52-
53-
WORKDIR /app
54-
55-
COPY pubspec.* ./
56-
RUN dart pub get
57-
58-
COPY . .
59-
60-
RUN dart pub get --offline
61-
RUN dart compile exe bin/server.dart -o bin/server
62-
63-
FROM scratch
64-
COPY --from=build /runtime/ /
65-
COPY --from=build /app/bin/server /app/bin/
66-
67-
CMD ["/app/bin/server"]
68-
''';
69-
final tempDirectory = Directory.systemTemp.createTempSync();
70-
final projectDirectoryPath = path.join(tempDirectory.path, projectName);
71-
final projectDirectory = Directory(projectDirectoryPath);
72-
await dartFrogCreate(projectName: projectName, directory: tempDirectory);
73-
File(
74-
path.join(projectDirectoryPath, 'Dockerfile'),
75-
).writeAsStringSync(customDockerFileContents);
76-
await dartFrogBuild(directory: projectDirectory);
77-
expect(
78-
File(
79-
path.join(projectDirectoryPath, 'build', 'Dockerfile'),
80-
).readAsStringSync(),
81-
equals(customDockerFileContents),
82-
);
83-
await tempDirectory.delete(recursive: true);
84-
});
85-
8648
testServer('GET / returns 200 with greeting', (host) async {
8749
final response = await http.get(Uri.parse(host));
8850
expect(response.statusCode, equals(HttpStatus.ok));

packages/dart_frog_cli/lib/src/commands/build/build.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class BuildCommand extends DartFrogCommand {
4747
final _ = await generator.generate(
4848
DirectoryGeneratorTarget(cwd),
4949
vars: vars,
50-
fileConflictResolution: FileConflictResolution.skip,
50+
fileConflictResolution: FileConflictResolution.overwrite,
5151
);
5252

5353
logger.detail('[codegen] running post-gen...');

packages/dart_frog_cli/test/src/commands/build/build_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void main() {
6565
() => generator.generate(
6666
any(),
6767
vars: any(named: 'vars'),
68-
fileConflictResolution: FileConflictResolution.skip,
68+
fileConflictResolution: FileConflictResolution.overwrite,
6969
),
7070
).thenAnswer((_) async => []);
7171
when(

0 commit comments

Comments
 (0)