Skip to content

Commit

Permalink
adr for bind volume
Browse files Browse the repository at this point in the history
  • Loading branch information
flemay committed Jan 25, 2024
1 parent 0185f4a commit 3dca799
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 35 deletions.
2 changes: 1 addition & 1 deletion demo/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ COMPOSE_RUN_VHS = ENV_HOST_SRC_DIR=$(HOST_SRC_DIR) docker compose run --rm vhs
COMPOSE_RUN_VHS_BASH = ENV_HOST_SRC_DIR=$(HOST_SRC_DIR) docker compose run --rm --entrypoint bash vhs
ENVFILE ?= env.template

all: envfile deps record prune
all: envfile deps test record prune

envfile:
cp -f $(ENVFILE) .env
Expand Down
37 changes: 19 additions & 18 deletions demo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,26 +70,27 @@ graph TB

Flow:

1. `make record` sends the command `docker compose run vhs demo.tape` with the Docker client
2. The Docker client sends it to the Docker daemon
3. The Docker daemon creates a service `vhs`
- The details of the service is defined in `docker-compose.yml`
- The container is based on Docker image `flemay/3musketeers-vhs:local`
1. `make record` sends the command `docker compose run vhs demo.tape` with the Docker client.
2. The Docker client sends it to the Docker daemon.
3. The Docker daemon creates a service `vhs`.
- The details of the service is defined in `docker-compose.yml`.
- The container is based on Docker image `flemay/3musketeers-vhs:local`.
- The image `flemay/3musketeers-vhs:local` definition comes from `Dockerfile`. It is based on `ghcr.io/charmbracelet/vhs` and adds required tools for the demo such as: `nvim`, `make`, `docker`, and `compose`.
- A volume is created which maps the host directory `./` to container directory `/opt/demo/`. This makes the file `demo.tape` accessible to `vhs` inside the container.
- `vhs demo.tape` is then executed
4. `vhs demo.tape` calls the commands `cd src/` and `make run`
5. `make run` executes the command `docker compose golang go run main.go` with the Docker client (inside the container)
6. The Docker client (inside the container) passes the command to Docker daemon (on the host)
- This is possible because the service `vhs` (defined in `docker-compose.yml`) mounts the host `/var/run/docker.sock`
7. The Docker daemon creates a service `golang`
- The container is based on the official Go Docker image
- The details of the service is in `src/docker-compose.yml`
- The service `golang` defines a volume that maps the host directory `./src/` to the container directory `/opt/app/`. That directory contains the source file `main.go`
- It is important to note that the full path to the host directory `./src/` is passed to the service (using environment variable `ENV_HOST_SRC_DIR`) and not the container path `/opt/demo/src/` even if the command originated from the container `vhs`. This is because the Docker daemon (being outside of the container) would not know the location of `/opt/demo/src/`
- `go run main.go` is executed inside the container
8. `Hello, World!` is printed out
9. `vhs` saves the record `demo.gif` into directory `/opt/demo/output/` which is also accessible from the host directory `./output/`
- `vhs demo.tape` is then executed.
4. `vhs demo.tape` calls the commands `cd src/` and `make run`.
5. `make run` executes the command `docker compose golang go run main.go` with the Docker client (inside the container).
6. The Docker client (inside the container) passes the command to Docker daemon (on the host).
- This is possible because the service `vhs` (defined in `docker-compose.yml`) mounts the host `/var/run/docker.sock`.
7. The Docker daemon creates a service `golang`.
- The container is based on the official Go Docker image.
- The details of the service is in `src/docker-compose.yml`.
- The service `golang` defines a volume that maps the host directory `./src/` to the container directory `/opt/app/`. That directory contains the source file `main.go`.
- It is important to note that the full path to the host directory `./src/` is passed to the service (using environment variable `ENV_HOST_SRC_DIR`) and not the container path `/opt/demo/src/` even if the command originated from the container `vhs`. This is because the Docker daemon (being outside of the container) would not know the location of `/opt/demo/src/`.
- The variable substitution `${ENV_HOST_SRC_DIR:-.}` sets the `source` to the value of the environment variable `ENV_HOST_SRC_DIR` if present, otherwise it sets it to `.` which means current directory. This allows the Go application example to work with and without Docker-outside-of-Docker (DooD).
- `go run main.go` is executed inside the container.
8. `Hello, World!` is printed out.
9. `vhs` saves the record `demo.gif` into directory `/opt/demo/output/` which is also accessible from the host directory `./output/`.

## References

Expand Down
21 changes: 10 additions & 11 deletions demo/demo.tape
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,13 @@ Enter
Ctrl+L
Show

Type "# Let's create 'Hello, World!' application in Go using \"
Sleep 500ms
Enter
Type "# the 3 Musketeers!"
Type "# Let's create 'Hello, World!' application in Go using the 3 Musketeers!"
Sleep 1s
Ctrl+C
Ctrl+L

# Prerequisites
Type "# [1/4] Prerequisites: first, verify the host has \"
Sleep 500ms
Enter
Type "# Make, Docker, and Compose"
Type "# [1/4] Prerequisites: first, verify the host has Make, Docker and Compose"
Sleep 1s
Ctrl+C
Type "make --version"
Expand All @@ -47,7 +41,9 @@ Type "docker compose version"
Sleep 500ms
Enter
Sleep 1s
Hide
Ctrl+L
Show

# main.go
Type "# [2/4] Time to define our Go application"
Expand All @@ -60,7 +56,6 @@ Sleep 3s
Hide
Type ":wq"
Enter
#Ctrl+L
Show
Type "# Run it with Go"
Sleep 1s
Expand All @@ -71,14 +66,16 @@ Enter
Sleep 3s
Type "# It failed because the host does not have Go installed"
Sleep 1s
Hide
Ctrl+C
Ctrl+L
Show

# docker-compose.yml
Type "# [3/4] Compose: define our Go environment using an \"
Type "# [3/4] Compose: define our Go environment using an offical"
Sleep 500ms
Enter
Type "# offical Go Docker image where our application will be executed"
Type "# Go Docker image where our application will be executed"
Sleep 1s
Ctrl+C
Type "vim docker-compose.yml"
Expand All @@ -96,7 +93,9 @@ Type "docker compose run --rm golang go run main.go"
Sleep 500ms
Enter
Sleep 4s
Hide
Ctrl+L
Show

# Makefile
Type "# [4/4] Makefile: typing \"
Expand Down
5 changes: 4 additions & 1 deletion demo/src/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ services:
golang:
image: golang:alpine
volumes:
- ${ENV_HOST_SRC_DIR:-.}:/opt/app:ro
- type: bind
source: ${ENV_HOST_SRC_DIR:-.}
target: /opt/app
read_only: true
working_dir: /opt/app
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# node:alpine is not used because the plugin-last-updated uses `git` and alpine
# does not have it.
version: '3.8'
services:
node: &node
image: node
Expand Down
6 changes: 3 additions & 3 deletions docs/guide/patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ The [3 Musketeers repository][link3MusketeersGitHub] applies the patterns:

- Compose: [this website development][link3MusketeersWebsiteDevelopment]
- Docker: section [Getting started][link3MusketeersGettingStarted]
- Docker-out-of-Docker: [demo generated with VHS][link3MusketeersDemoCode]
- Docker-outside-of-Docker: [demo generated with VHS][link3MusketeersDemoCode]
:::

## Compose
Expand Down Expand Up @@ -230,9 +230,9 @@ echo:
make echo
```

## Docker-in/out-of-Docker (DinD/DooD)
## Docker-in/outside-of-Docker (DinD/DooD)

There are many articles and videos talking about Docker-in-Docker (DinD) and Docker-out-of-Docker (DooD) with pros and cons. This section describes a pattern that can be applied to both. A Docker container contains Make, Docker, and Compose which communicates to a Docker daemon, whether it is on a host (DooD) or inside another container (DinD). In such case, an image like [flemay/musketeers][linkMusketeersImage] can be used.
There are many articles and videos talking about Docker-in-Docker (DinD) and Docker-outside-of-Docker (DooD) with pros and cons. This section describes a pattern that can be applied to both. A Docker container contains Make, Docker, and Compose which communicates to a Docker daemon, whether it is on a host (DooD) or inside another container (DinD). In such case, an image like [flemay/musketeers][linkMusketeersImage] can be used.

![pattern-dind](./assets/pattern-dind.mmd.svg)

Expand Down

0 comments on commit 3dca799

Please sign in to comment.