Skip to content

Commit

Permalink
feat(src/docker-outside-of-docker): add docker-outside-of-docker feature
Browse files Browse the repository at this point in the history
  • Loading branch information
bartventer committed Mar 24, 2024
1 parent 191d15b commit 19486fa
Show file tree
Hide file tree
Showing 13 changed files with 604 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ This repository contains a _collection_ of features curated by [@bartventer](htt
- [common-utils](#common-utils)
- [aws-cli](#aws-cli)
- [docker-in-docker](#docker-in-docker)
- [docker-outside-of-docker](#docker-outside-of-docker)
- [Contributing](#contributing)
- [License](#license)
- [Trademarks](#trademarks)
Expand Down Expand Up @@ -89,6 +90,25 @@ Docker in Docker installs Docker and Docker Compose in a Docker container. This
}
```

### docker-outside-of-docker

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.

#### usage

```json
"features": {
"ghcr.io/bartventer/devcontainer-features/docker-outside-of-docker:0.1.0": {
"version": "latest",
"dockerDashComposeVersion": "v2",
"installDockerBuildx": true,
"installDockerComposeSwitch": true
}
}
```

Additional options can be found in the [feature documentation](src/docker-outside-of-docker/README.md).

## Contributing

All contributions are welcome! Open a pull request to request a feature or submit a bug report.
Expand Down
60 changes: 60 additions & 0 deletions src/docker-outside-of-docker/NOTES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
## Limitations

- 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.
- 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.
- 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.

## Supporting bind mounts from the workspace folder

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

### 1. Use the `${localWorkspaceFolder}` as environment variable in your code

1. Add the following to `devcontainer.json`:

```json
"remoteEnv": { "LOCAL_WORKSPACE_FOLDER": "${localWorkspaceFolder}" }
```

2. Usage with Docker commands

```bash
docker run -it --rm -v ${LOCAL_WORKSPACE_FOLDER}:/workspace archlinux bash
```

3. Usage with Docker-compose

```yaml
version: '3.9'

services:
archlinux:
image: archlinux
volumes:
- ${LOCAL_WORKSPACE_FOLDER:-./}:/workspace
```
- The defaults value `./` is added so that the `docker-compose.yaml` file can work when it is run outside of the container

### 2. Change the workspace to `${localWorkspaceFolder}`

- This is useful if we don't want to edit the `docker-compose.yaml` file

1. Add the following to `devcontainer.json`

```json
"workspaceFolder": "${localWorkspaceFolder}",
"workspaceMount": "source=${localWorkspaceFolder},target=${localWorkspaceFolder},type=bind"
```

2. Rebuild the container.
3. When the container first started with this settings, select the Workspace with the absolute path to the working directory inside the container
4. Docker commands with bind mount should work as they did outside of the devcontainer

> **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)).

## OS Support

This Feature should work on recent versions of Arch Linux-based distributions with the `pacman` package manager installed.

`bash` is required to execute the `install.sh` script.
58 changes: 58 additions & 0 deletions src/docker-outside-of-docker/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"id": "docker-outside-of-docker",
"version": "0.1.0",
"name": "Docker (docker-outside-of-docker)",
"documentationURL": "https://github.com/bartventer/devcontainer-features/tree/main/src/docker-outside-of-docker",
"licenseURL": "https://github.com/bartventer/devcontainer-features/blob/main/LICENSE",
"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.",
"options": {
"version": {
"type": "string",
"proposals": [
"latest",
"none",
"20.10"
],
"default": "latest",
"description": "Select or enter a Docker/Moby CLI version. (Availability can vary by OS version.)"
},
"dockerDashComposeVersion": {
"type": "string",
"enum": [
"none",
"v1",
"v2"
],
"default": "v2",
"description": "Compose version to use for docker-compose (v1 or v2 or none)"
},
"installDockerBuildx": {
"type": "boolean",
"default": true,
"description": "Install Docker Buildx"
},
"installDockerComposeSwitch": {
"type": "boolean",
"default": true,
"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."
}
},
"entrypoint": "/usr/local/share/docker-init.sh",
"customizations": {
"vscode": {
"extensions": [
"ms-azuretools.vscode-docker"
]
}
},
"mounts": [
{
"source": "/var/run/docker.sock",
"target": "/var/run/docker-host.sock",
"type": "bind"
}
],
"installsAfter": [
"ghcr.io/bartventer/devcontainer-features/common-utils"
]
}
Loading

0 comments on commit 19486fa

Please sign in to comment.