Skip to content

Docker volume mounting for development

Docker volume mounting is a technique used to facilitate development inside a container by synchronizing the host machine's file system with the container's file system^[400-devops-03-containerization-docker.md]. This allows developers to use the tools and environment of the container while editing files on their host machine^[400-devops-03-containerization-docker.md].

Implementation

To enable development within a container, the current working directory on the host machine is mounted into the working directory of the container^[400-devops-03-containerization-docker.md]. This is typically achieved using the -v (volume) flag with the docker run command, mapping the present working directory (${PWD}) to a specific path inside the container (e.g., /work)^[400-devops-03-containerization-docker.md].

Example

A common workflow involves building an image targeted for development and then running it with an interactive shell and a mounted volume^[400-devops-03-containerization-docker.md]:

docker build --target dev . -t yudady/golang:1.18

docker run -it -v ${PWD}:/work yudady/golang:1.18 sh

In this example, the dev target is built first^[400-devops-03-containerization-docker.md]. The subsequent docker run command launches an interactive session (-it), mounts the current directory to /work, and opens a shell, allowing the user to execute commands like go version immediately inside the container environment^[400-devops-03-containerization-docker.md].

  • [[Docker]]
  • [[Dockerfiles]]

Sources

^[400-devops-03-containerization-docker.md]