When using Docker, users often need to be able to view the data generated in the container, or need to back up the data in the container, or even share data among multiple containers. Therefore, it is necessary to understand the data management operation of the container.
There are two main ways to manipulate data in a container:
- Data Volume
- Data Volume Containers
The container is only in the running state, that is, all data will be reset at the end of the container's life cycle. If you want to save data in it without affecting the image, you usually use the data volume.
volume will not be packaged with commit and submitted to a new image. The official document says that
The commit operation will not include any data contained in volumes mounted inside the container. It can be useful to commit a container's file changes or settings into a new image.
Create a data volume
When using the docker run command, use - v to create a data volume in the container, and use - v multiple times to use multiple data volumes
# Create containers using training/webapp images and mount data volumes to/webapp catalogue $ sudo docker run -d -p --name web -v /webapp training/webapp python app.py
If you need to mount a host directory to the container, mount the host's / src/webapp to the / opt/webapp directory in the container as follows:
$ sudo docker run -d -p --name web -v /src/webapp:/opt/webapp training/webapp python app.py
The data volume can also set its read-write permission (rw). ro identifies the specified read-only, so that the data of this data volume cannot be modified after the container is mounted
$ sudo docker run -d -p --name web -v /src/webapp:/opt/webapp:ro training/webapp python app.py
Viewing the volume mounted by the container in the host machine, in fact, the principle of data volume is not so complex, and it is also implemented by the file system of the host machine.
For example, let's start a mysql container. Here we will explain the differences between two different data volume types: volume and bindmount
## Default volume $ docker run -d --name mysql-local1 -e MYSQL_ROOT_PASSWORD=root mysql:5.7 ## Such sub mounts will be stored in the volume directory of docker by default [vagrant@docker-node3 ~]$ sudo ls /var/lib/docker/volumes c5020bd3e7253cd1fdfce10243424074846642d6db933bf66a911f6bdd0858a8 metadata.db ## If you don't want to use a long list of names, you can also name them separately ## mysql here is the name of volume ## docker volume ls Next name $ docker run -d -v mysql:/var/lib/mysql --name mysql-local1 -e MYSQL_ROOT_PASSWORD=root mysql:5.7 ## If using bind-mount To mount, that is, the directory of the local host, not docker of volume catalogue $ docker run -v /var/own/mysqldata:/var/lib/mysql --name mysqlnew -d mysql -e MYSQL_ROOT_PASSWORD=root mysql:5.7
View the container and its running configuration
[vagrant@docker-node3 ~]$ docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 13bcca305b36 mysql:5.7 "docker-entrypoint.s..." 37 minutes ago Up 37 minutes 3306/tcp, 33060/tcp mysql-local1 [vagrant@docker-node3 ~]$ docker inspect 13bcca305b36 ==== "Mounts": [ { "Type": "volume", "Name": "c5020bd3e7253cd1fdfce10243424074846642d6db933bf66a911f6bdd0858a8", "Source": "/var/lib/docker/volumes/c5020bd3e7253cd1fdfce10243424074846642d6db933bf66a911f6bdd0858a8/_data", "Destination": "/var/lib/mysql", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ], ====
View the directory of the data volume associated with the container mirrored by mysql
[vagrant@docker-node3 ~]$ sudo ls /var/lib/docker/volumes/c5020bd3e7253cd1fdfce10243424074846642d6db933bf66a911f6bdd0858a8/_data auto.cnf ca.pem client-key.pem ib_logfile0 ibdata1 performance_schema public_key.pem server-key.pem ca-key.pem client-cert.pem ib_buffer_pool ib_logfile1 mysql private_key.pem server-cert.pem sys
You can see that the data volume of a specific container is saved in the following directory of docker: / var/lib/docker/volumes/XXX
Development environment docker+bindmount create your own development environment
In the data volume, you can mount the code in the container to develop a python web application of flash. You can mount the code in the data volume, develop it in the host computer, and then run it in the container
For example, in the current directory, the controller logic related to the route with flash:
./dockerLib/flask-skeleton/ |-- __init__.py |-- base.py |-- test_config.py |-- test_main.py `-- test_user.py
test_user.py contains routing related logic
Start a container to mount the current directory
$ docker run -d -p 80:5000 -v $(pwd):/skeleton --name flask blackbinbin/flask-skeleton
Then you can edit the files in the external host directory and debug them in the container.