| 3 min read
By default, Docker containers are entirely stateless. The data stored on the container is ephemeral, and will be wiped when the container shuts down. This is obviously not ideal for many applications, so Docker provides a few ways of dealing with state.
What Are Volumes?
The simplest way of making Docker data persistent is bind mounts, which literally bind a location on the host’s disk to a location on the container’s disk. These are simple to create and use, but are a little janky as you need to set up the directories and manage them yourself.
Volumes are like virtual hard drives managed by Docker. Docker handles storing them on disk (usually in
/var/lib/docker/volumes/), and gives them an easily memorable single name rather than a directory path. It’s easy to create and remove them using the Docker CLI.
These have a few other benefits besides being managed by Docker. They work on both Linux and Windows containers, they can more safely shared among multiple containers, and the volume drivers allow cloud providers to provide remote hosting for the underlying data. Overall, volumes are easier to use than bind mounts, and are recommended by Docker for managing state.
Of course, you should consider if you really need Docker data to be stored on the server. For many applications, using an external remote data store like Amazon S3 or an external database is enough to store the data they use without tying it up on the frontend server.
How Do You Use Them?
You can create a new volume from the command line with the following command:
docker volume create nginx-config
And then, when you go to run your Docker container, link it to the target in the container with the
docker run -d --name devtest --mount source=nginx-config,target=/etc/nginx nginx:latest
If you run
docker inspect , you’ll see the volume listed under the
If you’re using Docker Compose, the setup is easy as well. Simply add a
volumes entry for each container service you have, then map a volume name to a location in the guest. You’ll also need to provide a list of volumes in a top-level
volumes key for Compose to provision.
version: "3.0"services: web: image: nginx:latest ports: - "80: 80" volumes: - nginx-config: /etc/nginx/volumes: nginx-config:
This will create the volume automatically for this Compose. If you’d like to use a premade volume from outside Compose, specify
external: true in the volume configuration:
volumes: cms-content: external: true
If you’d like to instead simply do a bind mount and not bother with volumes, simply enter in a path name in place of the volume name, and forego defining the volume names.
version: "3.0"services: web: image: nginx:latest ports: - "80: 80" volumes: - /docker/nginx-config/:/etc/nginx/
You can read Docker’s full documentation on using volumes with Compose if your use case requires something more specific than this.
Transfering Docker Volumes
Docker volumes are just folders created automatically and stored at