-
Notifications
You must be signed in to change notification settings - Fork 43
chore: add docs re docker inside envbuilder-built-envs #191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
# Docker inside Envbuilder | ||
|
||
There are a number of approaches you can use to have access to a Docker daemon | ||
from inside Envbuilder: | ||
|
||
## Docker Outside of Docker (DooD) | ||
|
||
**Security:** None | ||
**Convenience:** High | ||
|
||
This approach re-uses the host Docker socket and passes it inside the container. | ||
It is the simplest approach, but offers **no security** -- any process inside the | ||
container that can connect to the Docker socket will have access to the | ||
underlying host. | ||
Only use it if you are the only person using the Docker socket (for example, if | ||
you are experimenting on your own workstation). | ||
|
||
Example: | ||
|
||
```console | ||
docker run -it --rm \ | ||
-v /tmp/envbuilder:/workspaces \ | ||
-e ENVBUILDER_GIT_URL=https://github.com/coder/envbuilder \ | ||
-e ENVBUILDER_DEVCONTAINER_DIR=/workspaces/envbuilder/examples/docker/01_dood \ | ||
-e ENVBUILDER_INIT_SCRIPT=bash \ | ||
-v /var/run/docker.socket:/var/run/docker.socket \ | ||
ghcr.io/coder/envbuilder:latest | ||
``` | ||
|
||
|
||
## Docker-in-Docker (DinD) | ||
|
||
**Security:** Low | ||
**Convenience:** High | ||
|
||
This approach entails running a Docker daemon inside the container. | ||
This requires a privileged container to run, and therefore has a wide potential | ||
attack surface. | ||
|
||
Example: | ||
|
||
> Note that due to a lack of init system, the Docker daemon | ||
> needs to be started separately inside the container. In this example, we | ||
> create a custom entrypoint to start the Docker daemon in the background and | ||
> call this entrypoint via `ENVBUILDER_INIT_SCRIPT`. | ||
|
||
```console | ||
docker run -it --rm \ | ||
--privileged \ | ||
-v /tmp/envbuilder:/workspaces \ | ||
-e ENVBUILDER_GIT_URL=https://github.com/coder/envbuilder \ | ||
-e ENVBUILDER_DEVCONTAINER_DIR=/workspaces/envbuilder/examples/docker/02_dind \ | ||
-e ENVBUILDER_INIT_SCRIPT=/entrypoint.sh \ | ||
ghcr.io/coder/envbuilder:latest | ||
``` | ||
|
||
### DinD via Devcontainer Feature | ||
|
||
The above can also be accomplished using the [`docker-in-docker` Devcontainer | ||
feature](https://github.com/devcontainers/features/tree/main/src/docker-in-docker). | ||
|
||
> Note: we still need the custom entrypoint to start the docker startup script. | ||
> See https://github.com/devcontainers/features/blob/main/src/docker-in-docker/devcontainer-feature.json#L60 | ||
|
||
Example: | ||
|
||
```console | ||
docker run -it --rm \ | ||
--privileged \ | ||
-v /tmp/envbuilder:/workspaces \ | ||
-e ENVBUILDER_GIT_URL=https://github.com/coder/envbuilder \ | ||
-e ENVBUILDER_DEVCONTAINER_DIR=/workspaces/envbuilder/examples/docker/03_dind_feature \ | ||
-e ENVBUILDER_INIT_SCRIPT=/entrypoint.sh \ | ||
ghcr.io/coder/envbuilder:latest | ||
``` | ||
|
||
## Rootless DinD | ||
|
||
**Security:** Medium | ||
**Convenience:** Medium | ||
|
||
This approach runs a Docker daemon in *rootless* mode. | ||
While this still requires a privileged container, this allows you to restrict | ||
usage of the `root` user inside the container, as the Docker daemon will be run | ||
under a "fake" root user (via `rootlesskit`). The user inside the workspace can | ||
then be a 'regular' user without root permissions. | ||
|
||
> Note: Once again, we use a custom entrypoint via `ENVBUILDER_INIT_SCRIPT` to | ||
> start the Docker daemon via `rootlesskit`. | ||
|
||
Example: | ||
|
||
```console | ||
docker run -it --rm \ | ||
--privileged \ | ||
-v /tmp/envbuilder:/workspaces \ | ||
-e ENVBUILDER_GIT_URL=https://github.com/coder/envbuilder \ | ||
-e ENVBUILDER_DEVCONTAINER_DIR=/workspaces/envbuilder/examples/docker/04_dind_rootless \ | ||
-e ENVBUILDER_INIT_SCRIPT=/entrypoint.sh \ | ||
ghcr.io/coder/envbuilder:latest | ||
``` | ||
|
||
## Docker-in-Docker using Sysbox | ||
|
||
**Security:** High | ||
**Convenience:** Low for infra admins, high for users | ||
|
||
This approach requires installing the [`sysbox-runc` container | ||
runtime](https://github.com/nestybox/sysbox/blob/master/docs/user-guide/install-package.md). | ||
This is an alternative container runtime that provides additional benefits, | ||
including transparently enabling Docker inside workspaces. Most notably, it | ||
**does not require a privileged container**, so you can allow developers root | ||
access inside their workspaces, if required. | ||
|
||
Example: | ||
```console | ||
docker run -it --rm \ | ||
-v /tmp/envbuilder:/workspaces \ | ||
-e ENVBUILDER_GIT_URL=https://github.com/coder/envbuilder \ | ||
-e ENVBUILDER_DEVCONTAINER_DIR=/workspaces/envbuilder/examples/docker/02_dind \ | ||
-e ENVBUILDER_INIT_SCRIPT=/entrypoint.sh \ | ||
--runtime sysbox-runc \ | ||
ghcr.io/coder/envbuilder:latest | ||
``` | ||
|
||
For further information on Sysbox, please consult the [Sysbox | ||
Documentation](https://github.com/nestybox/sysbox/blob/master/docs/user-guide/README.md). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
FROM ubuntu:noble | ||
RUN apt-get update && apt-get install -y docker.io |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"build": { | ||
"dockerfile": "Dockerfile" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
FROM ubuntu:noble | ||
RUN apt-get update && \ | ||
apt-get install -y curl apt-transport-https && \ | ||
curl -fsSL https://get.docker.com/ | sh -s - | ||
ADD entrypoint.sh /entrypoint.sh | ||
ENTRYPOINT ["/entrypoint.sh"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"build": { | ||
"dockerfile": "Dockerfile" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -euo pipefail | ||
|
||
nohup dockerd > /var/log/docker.log 2>&1 & | ||
|
||
exec bash --login |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
FROM ubuntu:noble | ||
ADD entrypoint.sh /entrypoint.sh | ||
ENTRYPOINT ["/entrypoint.sh"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"build": { | ||
"dockerfile": "Dockerfile" | ||
}, | ||
"features": { | ||
"ghcr.io/devcontainers/features/docker-in-docker:2": {} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -euo pipefail | ||
|
||
/usr/local/share/docker-init.sh | ||
|
||
exec bash --login |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
FROM ubuntu:noble | ||
# Based on UID of ubuntu user in container. | ||
ENV XDG_RUNTIME_DIR /run/user/1000 | ||
ENV DOCKER_HOST unix:///${XDG_RUNTIME_DIR}/docker.sock | ||
# Setup as root | ||
RUN apt-get update && \ | ||
# Install prerequisites | ||
apt-get install -y apt-transport-https curl iproute2 uidmap && \ | ||
# Install Docker | ||
curl -fsSL https://get.docker.com/ | sh -s - && \ | ||
# Add ubuntu user to docker group | ||
usermod -aG docker ubuntu && \ | ||
# Create the XDG_RUNTIME_DIR for our user and set DOCKER_HOST | ||
mkdir -p ${XDG_RUNTIME_DIR} && \ | ||
chown ubuntu:ubuntu ${XDG_RUNTIME_DIR} | ||
|
||
# Setup rootless mode as the ubuntu user. | ||
USER ubuntu | ||
RUN dockerd-rootless-setuptool.sh install && \ | ||
docker context use rootless && \ | ||
mkdir -p /home/ubuntu/.local/share/docker | ||
# Add our custom entrypoint | ||
ADD entrypoint.sh /entrypoint.sh | ||
ENTRYPOINT ["/entrypoint.sh"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"build": { | ||
"dockerfile": "Dockerfile" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -euo pipefail | ||
|
||
# Start the rootless docker daemon as a non-root user | ||
nohup rootlesskit --net=slirp4netns --mtu=1500 --disable-host-loopback --port-driver=builtin --copy-up=/etc --copy-up=/run dockerd > "/tmp/dockerd-rootless.log" 2>&1 & | ||
|
||
exec bash --login |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.