Skip to content

Commit 80f97c8

Browse files
authored
Sharing dev container (#69)
* How to share dev container to multiple services * Upgrade dependencies
1 parent 3cb2397 commit 80f97c8

File tree

3 files changed

+3093
-821
lines changed

3 files changed

+3093
-821
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,15 @@ make deps
137137
# Start vitepress server for local development
138138
make dev
139139
# Wait till the message 'vite v2.5.3 dev server running at' appears
140-
# Access the website in your browser at http://localhost:8080/
140+
# Access the website in your browser at http://127.0.0.1:5173/
141141
# \<ctrl-c\> to stop
142142

143143
# Build static site
144144
make build
145145

146146
# Serve static site for local development
147147
make serveDev
148-
# Access the website in your browser at http://localhost:8080/
148+
# Access the website in your browser at http://127.0.0.1:5173/
149149
# \<ctrl-c\> to stop
150150

151151
# Serve static website (headless)

docs/guide/project-dependencies.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
---
2+
outline: 'deep'
3+
---
4+
15
# Project dependencies
26

37
Dependencies play a major role when comes time to test, build, and deploy a project. A project can have many dependencies such as programing languages, third party packages, databases, etc. This section covers some approaches to handle them with the 3 Musketeers.
@@ -40,8 +44,6 @@ If a project has specific dependency requirements, then creating (and maintainin
4044
FROM alpine:latest
4145
RUN apk --update add bash curl nodejs npm git \
4246
&& rm -rf /var/cache/apk/*
43-
# install Hugo
44-
# ...
4547
# install node modules
4648
RUN npm install -g \
4749
postcss-cli \
@@ -52,12 +54,35 @@ RUN npm install -g \
5254
```yaml
5355
# compose.yml
5456
services:
55-
mycontainer:
57+
devcontainer:
5658
build: .
57-
image: flemay/myimage:local
5859
# ...
5960
```
6061

62+
### Share Dev container between services
63+
64+
There are situations where having multiple services sharing the same image is useful. For instance, there could be a base service that does not expose any port and another service that does. The base image would be used in CI without any port collision and the other one used locally.
65+
66+
```yaml
67+
# compose.yml
68+
services:
69+
devcontainer: &devcontainer
70+
build: .
71+
image: localhost:5000/myproject-devcontainer
72+
pull_policy: never
73+
74+
devcontainer-withports:
75+
<<: *devcontainer
76+
ports:
77+
- "127.0.0.1:3000:3000"
78+
```
79+
80+
In the snippet above, `devcontainer` includes the combination of [`build`, `image` and `pull_policy`](https://docs.docker.com/reference/compose-file/build/#using-build-and-image) to direct `Compose` to build the image `localhost:5000/myproject-devcontainer` if it is not cached already. The reason why the name is specified is for service `devcontainer-withports` to use the same image. By default, if `image` is omitted, `Compose` would build 2 images: `myproject-devcontainer` and `myproject-devcontainer-withports`.
81+
82+
[Fragment](https://docs.docker.com/reference/compose-file/fragments/) is used to repeat the configuration of service `devcontainer` in service `devcontainer-withports`.
83+
84+
Lastly, the image name contains `localhost:5000/` to prevent from pushing the image to a different Docker registry by mistake. With the command `docker compose push devcontainer`, `Compose` will attempt to push the image `localhost:5000/myproject-devcontainer` and fail unless there is a registry service running locally.
85+
6186
## Share dependencies with host or not
6287

6388
All the approaches discussed above can share third party dependencies with the host, usually by mounting a host directory to a Docker container and letting the container install the dependencies. An example would be like the 3 Musketeers website where a NodeJS container installs the packages and the `node_modules` folder is shared with the host. This is useful when developing as IDEs can provide intellisense (autocomplete). The dependencies can also be bundled and passed along the pipeline stages which is usually faster that re-installing them.

0 commit comments

Comments
 (0)