Answer the question
In order to leave comments, you need to log in
How do volumes work in Docker?
I came to the realization that I don’t fully understand how volumes work in docker, I started to figure it out, I brought out 4 entities about volumes:
Answer the question
In order to leave comments, you need to log in
Apart from host directories, there are 2 ways to do persistence in docker: (1) data containers and (2) volumes. On the example of a composition, we will consider both options.
The oldest and, in my opinion, crutch method. It consists in the fact that the data will be stored in a container in which the process is not running, and in fact the container is stopped. As such a container, you can use the "main" image or use a special one, like tianon/true
.
version: '2'
services:
web:
image: 'nginx'
volumes_from:
- 'data'
data:
build: './public_html'
FROM nginx
ADD index.html /var/www/public_html
CMD /bin/true
CMD
in the Dockerfile of the data container. Despite the fact that the initial image is nginx
, as in the service web
, the command /bin/true
will not start the process, but only signals the docker that the container has been successfully built. docker-compose ps
will show that the data container has a status of type Exit...
, and this is exactly the state we are looking for for this container. version: '2'
services:
web:
image: 'nginx'
volumes:
- 'web_data:/var/www/public_html'
volumes:
web_data:
web_data
and map it to a folder /var/www/public_html
inside the container web
when the compose is started. Please note that in this example, the volume is designated as a key with no values, this is a valid yaml syntax, and, in this case, means that the compose will fallback the volume options to the default ones, of which driver: 'default'
. In case you are pulling compos locally or on a remote docker machine, the default driver is fine unless you know it's not. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question