Docker Volumes can help you code smoothly

Justgiveacar
3 min readAug 22, 2021

Hello there, today I want to introduce Docker Volume to you if you are recently using Docker and feeling very painful while synchronizing your local code inside the Docker container.

Maybe you have already heard that we can use volumes to maintain and persist state for the containers in production. But do you know that volumes can also be an excellent tool for speeding up your development workflow?

What is a Volume?

Docker’s official documentation: Volumes are the preferred mechanism for persisting data generated by and used by Docker containers. While bind-mounts are dependent on the directory structure and OS of the host machine, volumes are completely managed by Docker.

My unofficial documentation: Volumes are a special type of folder that is accessible by a Docker container. Unlike regular folders, volumes have a life cycle independent of the containers in which they’re mounted. Because they are stored outside of a container. As a result, volumes can persist data even when containers die to and share data between containers and the host.

In this article, we’re going to focus on a type of volume known as host volumes, which can be used to make it easier and faster to work with code inside of the container.

Host Volumes

Host volumes allow you to mount a folder on your own computer into a container. It synchronizes file changes between your local folder and a container’s folder. So if you use a host volume to mount code you are working on into a container, any edits you make to code on your local folder will automatically appear in the container.

Without this approach, every time you want to test your new code, you have to rebuild the container’s image. Conversely, if you mount your code as a host volume, you can configure that container to synchronize code changes as you make them.

Let's take a look at a Docker-compose.

This file tells Docker to boot a container and the application. It also tells Docker to mount a host volume:

As a result, Docker will mount the ./app directory on local machine, which contains your code, into the container at /usr/src/app/app.

Now what we need to do is ensure that the node process will restart whenever you’ve edited your code.

How to configure your container to sync your code automatically.

  1. Find the folder in your Docker container that has your code. The easiest way to figure out where your code is located in your container is to look at the COPY commands in your Dockerfile.
COPY . /user/src/app

2. Find the path to the folder on your local machine that has the same code.

3. Add a host volume to your Docker-compose file. Find the container in your Docker-compose file that you want to sync with, and add a ‘volume’ instruction underneath that container.

4. Run Docker Compose

docker-compose up

Docker will overwrite the code that was built into the container with the code you’ve got running locally on your local machine.

And you’re all set. By modifying your project so it uses a host volume, any changes you make to the code on your local machine will now automatically appear in the container.

Conclusion

Docker volumes were originally designed to make it easier to work with local files. But as we’ve seen in this article, they’re also a great way to make development easier and faster. It may take you a little time to get comfortable using volumes as part of your development process.

--

--