This is the 5th day of my participation in the August More Text Challenge

Docker command in front of learning and mirroring principle, docker is a simple entry, today can do something slightly more complex, learn the next container data volume

What is a container data volume? If the data is in the Container, the Container deleted data is lost, in the Docker, to implement the data persistence (the so-called Docker data persistence at the end of the data is not with the end of the Container), you need to data from the host machine mounted to the Container, behind we will see through the concrete operation data volume is what

Using a Data Volume

Mount it directly by command

Docker run -v Host directory: container directoryCopy the code

Mount the /home directory to the /Users/cb/test directory on the host.

$ docker run -it -v  /Users/cb/test:/home centos /bin/bash
[root@78c0a6fa144b /]#
Copy the code

After executing the above command, let’s check with the Docker inspect command

Docker inspect Container IDCopy the code

The container mount relationship can be seen in the following output, that is, we mounted successfully

  • Source: indicates the internal address of the host
  • Destination: specifies the address in the docker container
"Mounts": [
            {
                "Type": "bind",
                "Source": "/Users/cb/test",
                "Destination": "/home",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
        ]
Copy the code

Once the binding is done, or the mount is done, the data can be synchronized, so let’s see what happens

Both folders are empty at the beginning

Let’s add a file inside the container and let’s see

You can see that the files in the container were successfully synchronized to the host

Then we stop the container, modify the file on the host and start the container again to see if the data can be synchronized

I’ll give you a list. You can do it yourself

Docker ps -a docker run docker run docker run dockerCopy the code

This example is the most intuitive data volume, data volume also has another use, for some configurations we can also use the mapping to map frequently modified configurations to the local, local modification automatically synchronized to the container

Mysql installation

If you install mysql in docker, the data must not be placed in the container. Once the container is deleted, the data in the container will also be deleted. We can synchronize data through data volumes

Run the following command to synchronize the mysql configuration and data in the container to the host

docker run -d -p 3310:3306 -v /Users/chenbang/home/mysql/conf:/etc/mysql/conf.d -v / Users/chenbang/home/mysql/data: / var/lib/mysql - e MYSQL_ROOT_PASSWORD = 123456 - name mysql01 mysql: 5.7Copy the code
  • -d Background running
  • -p Port mapping
  • -v Mount the data volume
  • -e Environment configuration (the password must be set when starting mysql)

After the successful startup in the local use of a three-party client connection to see, it is normal to connect

Conf and data are in the target path of the host

There is corresponding data in data, look at the data is synchronized…

Let’s create a new library in the visualization tool. The corresponding data should be a file

Let’s take a look at the contents of the host target path data folder, sure enough, we just added a new ajTest library

And then let’s delete the container and look at it

After operation, the data is still alive… Data is not lost

This enables in-container persistence!

Anonymous mount

Run the following command to start the container

docker run -d -P --name nginx01 -v /etc/nginx nginx 
Copy the code

Let’s take a look at all the data volumes

docker volume ls
Copy the code

The VOLUME NAME is a randomly generated string, which is an anonymous mount, because -v only writes paths inside the container, not outside the container

A named mount

Run the following command to start the container

docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx nginx
Copy the code

You can see the data volume with the specified name, so named mount can be done by “-v volume name: Container path “.

Let’s look at the path of this volume

➜ ~ docker volume inspect juming-nginx [{"CreatedAt": "2021-08-17t12:18:04z ", "Driver": "local", "Labels": null, "Mountpoint": "/var/lib/docker/volumes/juming-nginx/_data", "Name": "juming-nginx", "Options": null, "Scope": "local" } ]Copy the code

All docker container volume, without a specified directory in/var/lib/docker/volumes/XXX

We can look in this directory

/var/lib/docker can’t be found if the host is MAC… The reason is that docker under MAC actually adds another layer in VM, so operation can only be carried out by entering VM.

Solutions found on the Internet are as follows:

screen ~/Library/Containers/com.docker.docker/Data/vms/0/tty
Copy the code

After trying, it is found to flash back directly and the following message is displayed:

[screen is terminating]
Copy the code

Therefore, this problem needs to be solved. The solution is as follows:

docker run -it --privileged --pid=host justincormack/nsenter1

Unable to find image 'justincormack/nsenter1:latest' locally
latest: Pulling from justincormack/nsenter1
5bc638ae6f98: Pull complete 
Digest: sha256:e876f694a4cb6ff9e6861197ea3680fe2e3c5ab773a1e37ca1f13171f7f5798e
Status: Downloaded newer image for justincormack/nsenter1:latest
/ # cd /var/lib/docker/
Copy the code

You can see the contents of the docker working directory

Go to Volumes and you can see all the data volumes

Look at the juming – nginx

We can easily find one of our volumes by named mount. Most of the cases are in use. Anonymous mount is not recommended

How do I determine whether to mount a named or anonymous mount, or a specified path?

-v Container path # Mount anonymously

-v Volume name: Container path # Named Mount

-v Host path: container path # Specify a path to mount

Development:

You can change the read and write permissions by running the -v internal path: ro/rw

Ro readonly # Read-only

Rw readWrite # readwrite

docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx:ro nginx
docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx:rw nginx
Copy the code

Once the container permissions are set, the container has restrictions on what we can mount out of it!

The use of docker data volume will stop here today, later will continue to accept Dockerfile, as complete as possible to learn the use of docker this fast, everyone come on!!

Ten steps to kill a man, a thousand miles to walk. – Li Bai