Docker learning notes sorting

1. Basic command

1.1 Docker image command

View all mirrors

#View all mirrors
[root@node1 ~] docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   5 months ago   13.3kB

#Other options
Options:
  -a, --all          #View all mirrors
  -q, --quiet        #Show only mirror id

Search image from remote warehouse

#Search for images from dockerhub
[root@node1 ~] docker search mysql
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                             MySQL is a widely used, open-source relation...   11332     [OK]       
mariadb                           MariaDB Server is a high performing open sou...   4307      [OK]       
mysql/mysql-server                Optimized MySQL Server Docker images. Create...   840                  [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   91                   

Download Image

#Download the image. The latest version is downloaded by default. What I download here is mysql5.7
[root@node1 ~] docker pull mysql:5.7
5.7: Pulling from library/mysql
e1acddbe380c: Already exists 
bed879327370: Already exists 
03285f80bafd: Already exists 
ccc17412a00a: Already exists 
1f556ecc09d1: Already exists 
adc5528e468d: Already exists 
1afc286d5d53: Already exists 
4d2d9261e3ad: Already exists 
ac609d7b31f8: Already exists 
53ee1339bc3a: Already exists 
b0c0a831a707: Already exists 
Digest: sha256:7cf2e7d7ff876f93c8601406a5aa17484e6623875e64e7acc71432ad8e0a3d7e
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

delete mirror

#Delete docker image
#docker rmi -f (forced deletion)
[root@node1 ~] docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
mysql         5.7       6c20ffa54f86   11 days ago    448MB
hello-world   latest    d1165f221234   5 months ago   13.3kB
[root@node1 ~] docker rmi d1165f221234
Untagged: hello-world:latest
Untagged: hello-world@sha256:7d91b69e04a9029b99f3585aaaccae2baa80bcf318f4a5d2165a9898cd2dc0a1
Deleted: sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726
Deleted: sha256:f22b99068db93900abe17f7f5e09ec775c2826ecfe9db961fea68293744144bd
[root@node1 ~] docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
mysql        5.7       6c20ffa54f86   11 days ago   448MB

Commit mirror

Command: docker commit -a = author name - m = remarks container id image name: tag

example:
Download the tomcat image and run it

[root@node1 ~]# docker run -d -p 8080:8080 tomcat:8.5
f4da03e7fffcdf4950c31e118074a47f632e62ea68e3673e6bb900abbd1dcb18
[root@node1 ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS         PORTS                                                  NAMES
f4da03e7fffc   tomcat:8.5     "catalina.sh run"        10 seconds ago   Up 9 seconds   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp              eager_brattain

Visit localhost:8080 and find 404

The reason is that the path cannot be found. Enter the container and find that the directory of webapps is empty

[root@node1 ~]# docker exec -it f4da03e7fffc /bin/bash
root@f4da03e7fffc:/usr/local/tomcat# ls
BUILDING.txt  CONTRIBUTING.md  LICENSE	NOTICE	README.md  RELEASE-NOTES  RUNNING.txt  bin  conf  lib  logs  native-jni-lib  temp  webapps  webapps.dist  work
root@f4da03e7fffc:/usr/local/tomcat# cd webapps
root@f4da03e7fffc:/usr/local/tomcat/webapps# ls
root@f4da03e7fffc:/usr/local/tomcat/webapps#  #This directory is empty

If you find that the file is stored in the webapps.dist directory, you only need to copy the file to the webapps directory

root@f4da03e7fffc:/usr/local/tomcat# cp -r webapps.dist/* webapps
root@f4da03e7fffc:/usr/local/tomcat# cd webapps
root@f4da03e7fffc:/usr/local/tomcat/webapps# ls
ROOT  docs  examples  host-manager  manager

Refresh again and the tomcat access is successful.

However, if we have to modify this thing every time we download tomcat, why can't we make an image of the Tomcat I'm running now and start my own image every time in the future, so we don't need to modify any parameters.

[root@node1 ~]# docker ps 
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
#tomcat container currently accessible normally
f4da03e7fffc   tomcat:8.5     "catalina.sh run"        11 minutes ago   Up 11 minutes   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp              eager_brattain
# Docker commit - a = author name - m = remarks container id image name: tag
[root@node1 ~]# docker commit -a='liu' -m='tomcatupdate' f4da03e7fffc mytomcat:1.0
sha256:f2a5b0c54783dfd52c6b6ca118292310679fef9c573eaa6c1f3f1ce2205638bb
[root@node1 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
mytomcat     1.0       f2a5b0c54783   8 seconds ago   538MB  #The image from your own commit
tomcat       8.5       dec1ddc22a20   13 days ago     533MB  #Image downloaded from dockerhub

Start your own image

#The external mapping port is 8070
[root@node1 ~]# docker run -d -p 8070:8080 --name mytomcat mytomcat:1.0
5e3f3dc7c00aa939da59c69e34aee335de0b585eef5ad7fe56396d8481d0292e
[root@node1 ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
5e3f3dc7c00a   mytomcat:1.0   "catalina.sh run"        7 seconds ago    Up 7 seconds    0.0.0.0:8070->8080/tcp, :::8070->8080/tcp              mytomcat
f4da03e7fffc   tomcat:8.5     "catalina.sh run"        18 minutes ago   Up 18 minutes   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp              eager_brattain

Access port 8070, access successful!

1.2 Docker container command

Create a new container and start the command

docker run [Optional parameters] image

Optional
--name  #Specify container name
-d      #Background operation
-p Host port:Container port
-P      #Random port
-it     #Run interactively

[root@node1 etc] docker run -p 3306:3306 --name mysql -it 6c20ffa54f86 /bin/bash
root@f79e5c6f4d37:/

##You can also use ctrl+p+q to push out the container. Such exit will make the container run in the background
root@f79e5c6f4d37:/# exit

View container

#View container (the default is to view the running container)
[root@node1 ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED       STATUS       PORTS                                                  NAMES
913ce44b5b76   6c20ffa54f86   "docker-entrypoint.s..."   9 hours ago   Up 9 hours   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql

Optional:
  -a, --all             View all containers, including those that are not running
  -q, --quiet           View container id(Can and-a Combined use)

Delete container

#Delete container: docker rm container ID (a running container cannot be deleted)
#Forced deletion: docker rm -f container ID
#Delete all containers: docker rm -f $(docker ps -aq)
[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                     PORTS                                                  NAMES
fbc84cdc58e8   6c20ffa54f86   "docker-entrypoint.s..."   11 seconds ago   Exited (0) 7 seconds ago                                                          peaceful_elgamal
913ce44b5b76   6c20ffa54f86   "docker-entrypoint.s..."   9 hours ago      Up 9 hours                 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql
[root@node1 ~]# docker rm fbc84cdc58e8
fbc84cdc58e8
[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED       STATUS       PORTS                                                  NAMES
913ce44b5b76   6c20ffa54f86   "docker-entrypoint.s..."   9 hours ago   Up 9 hours   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql

Start and stop containers

#Start container: docker start container ID
#Stop container: docker stop container ID
#Kill container: docker kill container ID

Other common commands

View log related

#view log 
#docker logs
# -TVIEW log with timestamp
# -f view all logs
# --tail 5 view the next 5 lines of logs
[root@node1 ~]# docker logs -tf --tail 5 mysql
2021-08-29T01:08:17.830487428Z 2021-08-29T01:08:17.829933Z 0 [Note] Server socket created on IP: '::'.
2021-08-29T01:08:17.841743527Z 2021-08-29T01:08:17.830919Z 0 [Warning] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2021-08-29T01:08:17.841756760Z 2021-08-29T01:08:17.841379Z 0 [Note] Event Scheduler: Loaded 0 events
2021-08-29T01:08:17.841759510Z 2021-08-29T01:08:17.841540Z 0 [Note] mysqld: ready for connections.
2021-08-29T01:08:17.841761394Z Version: '5.7.35'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server (GPL)

View the process information inside the container

#View the process information inside the container docker top container id
[root@node1 ~]# docker top 913ce44b5b76
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
polkitd             17664               17646               0                   09:08               ?                   00:00:00            mysqld

View all information about the container

#View all information of the container docker inspect container ID
[root@node1 ~]# docker inspect 913ce44b5b76
[
    {
        "Id": "913ce44b5b7670023d89f05476effdfd84cb0e3644bf2bd9b749d13a18959a44",
        "Created": "2021-08-28T15:34:15.87697844Z",
        "Path": "docker-entrypoint.sh",
        "Args": [
            "mysqld"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 17664,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-08-29T01:08:17.344704586Z",
            "FinishedAt": "2021-08-29T01:08:06.800391752Z"
        },
        "Image": "sha256:6c20ffa54f8674203e91e3225e489aa505fa04b8d482954a8b6d7414842c6de4",
        "ResolvConfPath": "/var/lib/docker/containers/913ce44b5b7670023d89f05476effdfd84cb0e3644bf2bd9b749d13a18959a44/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/913ce44b5b7670023d89f05476effdfd84cb0e3644bf2bd9b749d13a18959a44/hostname",
        "HostsPath": "/var/lib/docker/containers/913ce44b5b7670023d89f05476effdfd84cb0e3644bf2bd9b749d13a18959a44/hosts",
        "LogPath": "/var/lib/docker/containers/913ce44b5b7670023d89f05476effdfd84cb0e3644bf2bd9b749d13a18959a44/913ce44b5b7670023d89f05476effdfd84cb0e3644bf2bd9b749d13a18959a44-json.log",
        "Name": "/mysql",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {
                "3306/tcp": [
                    {
                        "HostIp": "",
                        "HostPort": "3306"
                    }
                ]
            },
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "host",
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "KernelMemory": 0,
            "KernelMemoryTCP": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/4f6a836e6cec45ef61f0baee098c4758de90f87a359317a11af8e744dcaa3260-init/diff:/var/lib/docker/overlay2/528d42fb1ee563a88b29547da1145dcfff495e64bc5643b27551a8fe6fb6756d/diff:/var/lib/docker/overlay2/558947ebdf65d6790b0a5f9718e037886dc4127c85a97e92f681196ea808a106/diff:/var/lib/docker/overlay2/81389b7661c62f67919386382d4d1973f9c734c0186a85b7c2922fdd60e76ff2/diff:/var/lib/docker/overlay2/bfe5f85707737cdf5f93874b54d3fef3fbbb12d041e19e4b10cc00a6119cb857/diff:/var/lib/docker/overlay2/4f67c98d9d93b94062051561e6e78a0ce80be772df734c48f4ec9e3eff054e9b/diff:/var/lib/docker/overlay2/21e33e808e4c9ae9d8d5ac8d3f71766505640a3d8c71ea7bab08e24ee6352c98/diff:/var/lib/docker/overlay2/2a131fa2db383f9bb245556322202fcd5bd707d393227965c48e297ed3de7725/diff:/var/lib/docker/overlay2/74d2c8705d5e8e7dd111a69567b78b465a77399e4ab084ddf3aec624d63a341d/diff:/var/lib/docker/overlay2/61b75442918ace8d70ae2759bb74cf66b8cc3bfd95aab558255a2be186a578ca/diff:/var/lib/docker/overlay2/d5693e8c316cc6c0ca74f57e42e32830a47594056d3ae1bc3b5a50eec6b75df1/diff:/var/lib/docker/overlay2/a6220ea2473c23a10e77ad8078654521b71e5d1d490f0f3045f9b631ad01a7f4/diff",
                "MergedDir": "/var/lib/docker/overlay2/4f6a836e6cec45ef61f0baee098c4758de90f87a359317a11af8e744dcaa3260/merged",
                "UpperDir": "/var/lib/docker/overlay2/4f6a836e6cec45ef61f0baee098c4758de90f87a359317a11af8e744dcaa3260/diff",
                "WorkDir": "/var/lib/docker/overlay2/4f6a836e6cec45ef61f0baee098c4758de90f87a359317a11af8e744dcaa3260/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "volume",
                "Name": "ec4c3eb8387bab381363109d22fb4e9610b0b9b3dbb7b74f38f0ec4c2eb460a6",
                "Source": "/var/lib/docker/volumes/ec4c3eb8387bab381363109d22fb4e9610b0b9b3dbb7b74f38f0ec4c2eb460a6/_data",
                "Destination": "/var/lib/mysql",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
        "Config": {
            "Hostname": "913ce44b5b76",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "3306/tcp": {},
                "33060/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "MYSQL_ROOT_PASSWORD=root",
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "GOSU_VERSION=1.12",
                "MYSQL_MAJOR=5.7",
                "MYSQL_VERSION=5.7.35-1debian10"
            ],
            "Cmd": [
                "mysqld"
            ],
            "Image": "6c20ffa54f86",
            "Volumes": {
                "/var/lib/mysql": {}
            },
            "WorkingDir": "",
            "Entrypoint": [
                "docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "dfb4b5e1bd2de535381a9a0086d9e363f9ad82c18df80dba7fc3bf597b76f4fc",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "3306/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "3306"
                    },
                    {
                        "HostIp": "::",
                        "HostPort": "3306"
                    }
                ],
                "33060/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/dfb4b5e1bd2d",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "791dc5a2e5b9dc6bbc477bdb22ec6f3edca68253eff91a127823a8c17d8221f3",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "ef70fb31e63c59dec772d32ac8e478783f574e48ae08e19b0af7a2c22aad81d8",
                    "EndpointID": "791dc5a2e5b9dc6bbc477bdb22ec6f3edca68253eff91a127823a8c17d8221f3",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]

Enter container command

#Enter container command 
# docker exec -it container ID /bin/bash (create a new container process and enter) recommended!!!
# docker attach container ID (entering the current running entry of the container)
[root@node1 ~]# docker exec -it 913ce44b5b76 /bin/bash
root@913ce44b5b76:/#  

2. Container data volume

Specify mount

Command: - v Host Directory: container directory

Suppose we need to install mysql database, but if we use the following command to start.

docker run -d -p Host port:Container port -e MYSQL_ROOT_PASSWORD=my-secret-pw mysql 

ps: -e mysql_ ROOT_ Password = my secret PW this command sets the root password of mysql

Then we will find that as long as we delete this container, we will delete the data, that is, delete the database and run away, so this is definitely not allowed.
The question is, how do we persist this data?

If we synchronize the mysql data storage files to the host, even if the container data is deleted, just start the container and rebind the directory next time.

So we have the following configuration commands

#Mount the msyql configuration file and data file to the / home/mysql directory on the host to realize data synchronization
[root@node1 home]# docker run -d -p 3306:3306 --name mysql -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
c611cb0705c3cc2a2e947ada705a674cf46f94ac47e32d7b67751e5e548261f8
[root@node1 home]# cd /home/mysql/data #This is the directory of the host
[root@node1 data]# ll
 Total consumption 188484
-rw-r-----. 1 polkitd input       56 9 January 22:06 auto.cnf
-rw-------. 1 polkitd input     1680 9 January 22:06 ca-key.pem
-rw-r--r--. 1 polkitd input     1112 9 January 22:06 ca.pem
-rw-r--r--. 1 polkitd input     1112 9 January 22:06 client-cert.pem
-rw-------. 1 polkitd input     1676 9 January 22:06 client-key.pem
-rw-r-----. 1 polkitd input     1359 9 January 22:06 ib_buffer_pool
-rw-r-----. 1 polkitd input 79691776 9 January 22:06 ibdata1
-rw-r-----. 1 polkitd input 50331648 9 January 22:06 ib_logfile0
-rw-r-----. 1 polkitd input 50331648 9 January 22:06 ib_logfile1
-rw-r-----. 1 polkitd input 12582912 9 January 22:06 ibtmp1
drwxr-x---. 2 polkitd input     4096 9 January 22:06 mysql
drwxr-x---. 2 polkitd input     8192 9 January 22:06 performance_schema
-rw-------. 1 polkitd input     1680 9 January 22:06 private_key.pem
-rw-r--r--. 1 polkitd input      452 9 January 22:06 public_key.pem
-rw-r--r--. 1 polkitd input     1112 9 January 22:06 server-cert.pem
-rw-------. 1 polkitd input     1680 9 January 22:06 server-key.pem
drwxr-x---. 2 polkitd input     8192 9 January 22:06 sys

This is the directory inside the container. You can see that the data is synchronized.

[root@node1 data]# docker exec -it mysql /bin/bash
root@c611cb0705c3:/# ls
bin  boot  dev	docker-entrypoint-initdb.d  entrypoint.sh  etc	home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@c611cb0705c3:/# cd var/lib/mysql
root@c611cb0705c3:/var/lib/mysql# ls 
auto.cnf  ca-key.pem  ca.pem  client-cert.pem  client-key.pem  ib_buffer_pool  ib_logfile0  ib_logfile1  ibdata1  ibtmp1  mysql  performance_schema  private_key.pem  public_key.pem  server-cert.pem  server-key.pem  sys

Anonymous mount

The command is as follows
-v container directory
Suppose we need to mount the nginx configuration file and use anonymous mounting. The operation is as follows:

[root@node1 _data]# docker run -d -p 80:80 --name nginx -v /etc/nginx nginx
ad3770d042c43a7f0ef474dcef19244890c3f30f4ebe43c2e37b93abb91d2b07
[root@node1 _data]# docker volume ls #View a list of data volumes
DRIVER    VOLUME NAME
local     07204037f3a84e3944a204014c26b1fd99dd3df0ef4678ae1cae0fe59808b2f0 #System generated data volume name
[root@node1 _data]# docker volume inspect  #View data volume details
07204037f3a84e3944a204014c26b1fd99dd3df0ef4678ae1cae0fe59808b2f0
[
    {
        "CreatedAt": "2021-09-01T22:25:45+08:00",
        "Driver": "local",
        "Labels": null,
        #Host mount directory
        "Mountpoint": "/var/lib/docker/volumes/07204037f3a84e3944a204014c26b1fd99dd3df0ef4678ae1cae0fe59808b2f0/_data", 
        "Name": "07204037f3a84e3944a204014c26b1fd99dd3df0ef4678ae1cae0fe59808b2f0",
        "Options": null,
        "Scope": "local"
    }
]
#Enter the mount directory to view
[root@node1 _data]# cd /var/lib/docker/volumes/07204037f3a84e3944a204014c26b1fd99dd3df0ef4678ae1cae0fe59808b2f0/_data
[root@node1 _data]# ls
#Mount succeeded
conf.d  fastcgi_params  mime.types  modules  nginx.conf  scgi_params  uwsgi_params

Named mount

The command is as follows:
-v data volume name: path in container

Suppose we need to mount the nginx configuration file and use a named mount. The operation is as follows:

[root@node1 data]# docker run -d -p 80:80 --name nginx -v nginx:/etc/nginx/nginx.conf nginx
docker: Error response from daemon: source /var/lib/docker/overlay2/ee2b86b68f0f6e36c07559b66e4b9dec31f52ab464b9d8116256c392cce82310/merged/etc/nginx/nginx.conf is not directory.
See 'docker run --help'.
[root@node1 data]# docker run -d -p 80:80 --name nginx -v nginx:/etc/nginx nginx
7be8d02510974db96e541dcd17076f6b980eb86180f30f05df00d46a5240ff2c
[root@node1 data]# docker volume ls #View mounted data volumes
DRIVER    VOLUME NAME
local     nginx #Custom data volume name
[root@node1 data]# docker volume inspect nginx #View mount data volume details
[
    {
        "CreatedAt": "2021-09-01T22:20:52+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/nginx/_data", #Host storage path
        "Name": "nginx",
        "Options": null,
        "Scope": "local"
    }
]
[root@node1 data]# cd /var/lib/docker/volumes/nginx/_data
[root@node1 _data]# ls
conf.d  fastcgi_params  mime.types  modules  nginx.conf  scgi_params  uwsgi_params

Tags: Docker

Posted on Sat, 18 Sep 2021 13:28:24 -0400 by flyersun