Skip to content

Commit 19486fa

Browse files
committed
feat(src/docker-outside-of-docker): add docker-outside-of-docker feature
1 parent 191d15b commit 19486fa

File tree

13 files changed

+604
-0
lines changed

13 files changed

+604
-0
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ This repository contains a _collection_ of features curated by [@bartventer](htt
3030
- [common-utils](#common-utils)
3131
- [aws-cli](#aws-cli)
3232
- [docker-in-docker](#docker-in-docker)
33+
- [docker-outside-of-docker](#docker-outside-of-docker)
3334
- [Contributing](#contributing)
3435
- [License](#license)
3536
- [Trademarks](#trademarks)
@@ -89,6 +90,25 @@ Docker in Docker installs Docker and Docker Compose in a Docker container. This
8990
}
9091
```
9192

93+
### docker-outside-of-docker
94+
95+
Docker outside of Docker re-uses the host docker socket, adding the Docker CLI to a container. This feature invokes a script to enable using a forwarded Docker socket within a container to run Docker commands.
96+
97+
#### usage
98+
99+
```json
100+
"features": {
101+
"ghcr.io/bartventer/devcontainer-features/docker-outside-of-docker:0.1.0": {
102+
"version": "latest",
103+
"dockerDashComposeVersion": "v2",
104+
"installDockerBuildx": true,
105+
"installDockerComposeSwitch": true
106+
}
107+
}
108+
```
109+
110+
Additional options can be found in the [feature documentation](src/docker-outside-of-docker/README.md).
111+
92112
## Contributing
93113

94114
All contributions are welcome! Open a pull request to request a feature or submit a bug report.

src/docker-outside-of-docker/NOTES.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
## Limitations
2+
3+
- As the name implies, the Feature is expected to work when the host is running Docker (or the OSS Moby container engine it is built on). It may be possible to get running in other container engines, but it has not been tested with them.
4+
- The host and the container must be running on the same chip architecture. You will not be able to use it with an emulated x86 image with Docker Desktop on an Apple Silicon Mac, for example.
5+
- This approach does not currently enable bind mounting the workspace folder by default, and cannot support folders outside of the workspace folder. Consider whether the [Docker-in-Docker Feature](../docker-in-docker) would better meet your needs given it does not have this limitation.
6+
7+
## Supporting bind mounts from the workspace folder
8+
9+
A common question that comes up is how you can use `bind` mounts from the Docker CLI from within the a dev container using this Feature (e.g. via `-v`). If you cannot use the [Docker-in-Docker Feature](../docker-in-docker), the only way to work around this is to use the **host**'s folder paths instead of the container's paths. There are 2 ways to do this
10+
11+
### 1. Use the `${localWorkspaceFolder}` as environment variable in your code
12+
13+
1. Add the following to `devcontainer.json`:
14+
15+
```json
16+
"remoteEnv": { "LOCAL_WORKSPACE_FOLDER": "${localWorkspaceFolder}" }
17+
```
18+
19+
2. Usage with Docker commands
20+
21+
```bash
22+
docker run -it --rm -v ${LOCAL_WORKSPACE_FOLDER}:/workspace archlinux bash
23+
```
24+
25+
3. Usage with Docker-compose
26+
27+
```yaml
28+
version: '3.9'
29+
30+
services:
31+
archlinux:
32+
image: archlinux
33+
volumes:
34+
- ${LOCAL_WORKSPACE_FOLDER:-./}:/workspace
35+
```
36+
37+
- The defaults value `./` is added so that the `docker-compose.yaml` file can work when it is run outside of the container
38+
39+
### 2. Change the workspace to `${localWorkspaceFolder}`
40+
41+
- This is useful if we don't want to edit the `docker-compose.yaml` file
42+
43+
1. Add the following to `devcontainer.json`
44+
45+
```json
46+
"workspaceFolder": "${localWorkspaceFolder}",
47+
"workspaceMount": "source=${localWorkspaceFolder},target=${localWorkspaceFolder},type=bind"
48+
```
49+
50+
2. Rebuild the container.
51+
3. When the container first started with this settings, select the Workspace with the absolute path to the working directory inside the container
52+
4. Docker commands with bind mount should work as they did outside of the devcontainer
53+
54+
> **Note:** There is no `${localWorkspaceFolder}` when using the **Clone Repository in Container Volume** command in the VS Code Dev Containers extension ([info](https://github.com/microsoft/vscode-remote-release/issues/6160#issuecomment-1014701007)).
55+
56+
## OS Support
57+
58+
This Feature should work on recent versions of Arch Linux-based distributions with the `pacman` package manager installed.
59+
60+
`bash` is required to execute the `install.sh` script.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"id": "docker-outside-of-docker",
3+
"version": "0.1.0",
4+
"name": "Docker (docker-outside-of-docker)",
5+
"documentationURL": "https://github.com/bartventer/devcontainer-features/tree/main/src/docker-outside-of-docker",
6+
"licenseURL": "https://github.com/bartventer/devcontainer-features/blob/main/LICENSE",
7+
"description": "Re-use the host docker socket, adding the Docker CLI to a container. Feature invokes a script to enable using a forwarded Docker socket within a container to run Docker commands.",
8+
"options": {
9+
"version": {
10+
"type": "string",
11+
"proposals": [
12+
"latest",
13+
"none",
14+
"20.10"
15+
],
16+
"default": "latest",
17+
"description": "Select or enter a Docker/Moby CLI version. (Availability can vary by OS version.)"
18+
},
19+
"dockerDashComposeVersion": {
20+
"type": "string",
21+
"enum": [
22+
"none",
23+
"v1",
24+
"v2"
25+
],
26+
"default": "v2",
27+
"description": "Compose version to use for docker-compose (v1 or v2 or none)"
28+
},
29+
"installDockerBuildx": {
30+
"type": "boolean",
31+
"default": true,
32+
"description": "Install Docker Buildx"
33+
},
34+
"installDockerComposeSwitch": {
35+
"type": "boolean",
36+
"default": true,
37+
"description": "Install Compose Switch (provided docker compose is available) which is a replacement to the Compose V1 docker-compose (python) executable. It translates the command line into Compose V2 docker compose then runs the latter."
38+
}
39+
},
40+
"entrypoint": "/usr/local/share/docker-init.sh",
41+
"customizations": {
42+
"vscode": {
43+
"extensions": [
44+
"ms-azuretools.vscode-docker"
45+
]
46+
}
47+
},
48+
"mounts": [
49+
{
50+
"source": "/var/run/docker.sock",
51+
"target": "/var/run/docker-host.sock",
52+
"type": "bind"
53+
}
54+
],
55+
"installsAfter": [
56+
"ghcr.io/bartventer/devcontainer-features/common-utils"
57+
]
58+
}

0 commit comments

Comments
 (0)