【Docker】docker日常工作总结(涉及基础命令、基础名词概念、镜像、容器、网络、数据卷)

在人生的道路上,不管是潇洒走一回,或者是千山独行,皆须是自己想走的路,虽然,有的人并不是很快就能找到自己的方向和道路,不过,只要坚持到底,我相信,就一定可以找到自己的路,只要找到路,就不必怕路途遥远了。

导读:本篇文章讲解 【Docker】docker日常工作总结(涉及基础命令、基础名词概念、镜像、容器、网络、数据卷),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

今日状态:保持学习
打卡学习 星期一 星期二 星期三 星期四 星期五 星期六 星期日
成功 成功 成功 成功 成功

前提:必须有一个docker环境,可以安装到windows、linux、Ubuntu;博主是安装到ubuntu中的。具体安装步骤可以借鉴https://blog.csdn.net/YZL40514131/article/details/124486645这篇文章

一、镜像和容器

🔥提示

在docker中把安装包叫做镜像
镜像是启动一个服务的时候所有的配置文件
容器是运行镜像后的一个状态,容器才是真正提供业务的单位

🌙1、查看ubuntu中所有的镜像

sudo docker images
sudo docker image ls

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker images
REPOSITORY                TAG         IMAGE ID       CREATED        SIZE
nginx                     latest      0e901e68141f   4 days ago     142MB
jenkins/jenkins           lts         cb535f2a7054   5 days ago     464MB
nginx                     alpine      b1c3acb28882   2 weeks ago    23.4MB
mysql                     8.0.29      96d0eae5ed60   4 weeks ago    524MB
mysql                     latest      96d0eae5ed60   4 weeks ago    524MB
hello-world               latest      feb5d9fea6a5   8 months ago   13.3kB
centos                    latest      5d0da3dc9764   8 months ago   231MB
delron/fastdfs            latest      8487e86fc6ee   4 years ago    464MB
delron/elasticsearch-ik   2.4.6-1.0   095b6487fb77   4 years ago    689MB
morunchang/fastdfs        latest      a729ac95698a   5 years ago    460MB

🌙2、删除镜像

报错的原因是:nginx正在运行中,不能删除
sudo docker image rm nginx:alpine(镜像名称 :镜像tag)
sudo docker rmi 0e901e68141f(镜像id)
sudo docker image rm 0e901e68141f(镜像id)

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker image rm nginx:alpine 
Error response from daemon: conflict: unable to remove repository reference "nginx" (must force) - container d992bc0fa330 is using its referenced image 0e901e68141f

解决:强制删除(-f:强制删除)

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker image rm -f nginx:alpine

🌙3、查看镜像的历史记录

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker history nginx

🌙4、下载镜像

1、sudo docker search redis:查看镜像相关的安装包(例如查看redis相关的安装包,建议使用官方的安装包)
2、也可以百度docker hub去获取相关的安装包https://registry.hub.docker.com/

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker search redis
NAME                                               DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
redis                                              Redis is an open source key-value store that…   10992     [OK]       
bitnami/redis                                      Bitnami Redis Docker Image                      220                  [OK]
bitnami/redis-sentinel                             Bitnami Docker Image for Redis Sentinel         37                   [OK]
bitnami/redis-cluster                                                                              32                   
rapidfort/redis                                    RapidFort optimized, hardened image for Redi…   14                   
rapidfort/redis-cluster                            RapidFort optimized, hardened image for Redi…   14                   
circleci/redis                                     CircleCI images for Redis                       13                   [OK]
ubuntu/redis                                       Redis, an open source key-value store. Long-…   10                   
bitnami/redis-exporter                                                                             7                    
clearlinux/redis                                   Redis key-value data structure server with t…   4                    
google/guestbook-python-redis                      A simple guestbook example written in Python…   4                    
ibmcom/redis-ha                                                                                    1                    
bitnami/redis-sentinel-exporter                                                                    1                    
ibmcom/ibm-cloud-databases-redis-operator          Container image for the IBM Operator for Red…   1                    
ibmcom/ibm-cloud-databases-redis-operator-bundle   Bundle image for the IBM Operator for Redis     1                    
ibmcom/ibm-cloud-databases-redis-catalog           Catalog image for the IBM Operator for Redis    1                    
ibmcom/redis-ppc64le                                                                               0                    
blackflysolutions/redis                            Redis container for Drupal and CiviCRM          0                    
ibmcom/redisearch-ppc64le                                                                          0                    
drud/redis                                         redis                                           0                    [OK]
rancher/redislabs-ssl                                                                              0                    
vmware/redis-photon                                                                                0                    
cimg/redis                                                                                         0                    
newrelic/k8s-nri-redis                             New Relic Infrastructure Redis Integration (…   0                    
astronomerinc/ap-redis                             Redis service for Airflow's celery executor …   0    

🌙5、拉取镜像

如果在拉取镜像时没有指定版本号,默认拉取的是最新的,例如sudo docker pull python
例如:拉取指定的版本:sudo docker pull mysql:5.7

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker pull python
Using default tag: latest
latest: Pulling from library/python
e756f3fdd6a3: Pull complete 
bf168a674899: Pull complete 
e604223835cc: Pull complete 
6d5c91c4cd86: Pull complete 
2cc8d8854262: Pull complete 
2767dbfeeb87: Pull complete 
e5f27d860d89: Pull complete 
98a3e4f5f5ed: Pull complete 
af265a3ad3e0: Pull complete 
Digest: sha256:bef6fd726fb8825d5cf26933d8477505b14505be1a98394405c8840c1c266bdd
Status: Downloaded newer image for python:latest
docker.io/library/python:latest

🌙6、查看镜像的详细信息

sudo docker inspect python:latest(镜像名:tag)

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker inspect python:latest

🌙7、查看容器:

sudo docker ps:查看正在运行的容器
sudo docker ps -a:查看所有的容器
sudo docker container ls:查看正在运行的容器
sudo docker container ls –all :查看所有的容器

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker container ls
CONTAINER ID   IMAGE                 COMMAND                  CREATED        STATUS        PORTS                                                                                        NAMES
bca70facba90   nginx:alpine          "/docker-entrypoint.…"   3 hours ago    Up 3 hours    0.0.0.0:49160->80/tcp, :::49160->80/tcp                                                      web4
ff2d3c951eed   nginx:alpine          "/docker-entrypoint.…"   3 hours ago    Up 3 hours    0.0.0.0:49159->80/tcp, :::49159->80/tcp                                                      web3
4011f219f403   nginx:alpine          "/docker-entrypoint.…"   3 hours ago    Up 3 hours    0.0.0.0:49158->80/tcp, :::49158->80/tcp                                                      web2
d992bc0fa330   nginx                 "/docker-entrypoint.…"   3 hours ago    Up 3 hours    80/tcp                                                                                       nginx_test
2c4108268aa9   jenkins/jenkins:lts   "/sbin/tini -- /usr/…"   25 hours ago   Up 25 hours   0.0.0.0:49154->8080/tcp, :::49154->8080/tcp, 0.0.0.0:49153->50000/tcp, :::49153->50000/tcp   jenkins
ae1fd6dd1b00   mysql                 "docker-entrypoint.s…"   3 weeks ago    Up 3 days     0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp                                         mysql

🌙8、运行容器

1、sudo docker run redis:redis是一个镜像,首先去本地查看有没有redis的镜像,如果有直接去运行,如果没有,默认去docker hub官方镜像中拉取到本地
2、如果后面没有指名版本号,等价于sudo docker run redis:latest(最新的)

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker run redis
Unable to find image 'redis:latest' locally
latest: Pulling from library/redis
42c077c10790: Already exists 
a300d83d65f9: Pull complete 
ebdc3afaab5c: Pull complete 
31eec7f8651c: Pull complete 
9c6a6b89d274: Pull complete 
5c8099a4b45c: Pull complete 
Digest: sha256:1b90dbfe6943c72a7469c134cad3f02eb810f016049a0e19ad78be07040cdb0c
Status: Downloaded newer image for redis:latest
1:C 02 Jun 2022 05:36:13.616 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 02 Jun 2022 05:36:13.616 # Redis version=7.0.0, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 02 Jun 2022 05:36:13.616 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 02 Jun 2022 05:36:13.616 * monotonic clock: POSIX clock_gettime
1:M 02 Jun 2022 05:36:13.617 * Running mode=standalone, port=6379.
1:M 02 Jun 2022 05:36:13.617 # Server initialized
1:M 02 Jun 2022 05:36:13.617 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 02 Jun 2022 05:36:13.618 * The AOF directory appendonlydir doesn't exist
1:M 02 Jun 2022 05:36:13.618 * Ready to accept connections

^C1:signal-handler (1654148179) Received SIGINT scheduling shutdown...
1:M 02 Jun 2022 05:36:19.649 # User requested shutdown...
1:M 02 Jun 2022 05:36:19.649 * Saving the final RDB snapshot before exiting.
1:M 02 Jun 2022 05:36:19.649 * DB saved on disk
1:M 02 Jun 2022 05:36:19.649 # Redis is now ready to exit, bye bye...

默认运行一个容器时容器名称是随机生成的

在这里插入图片描述

运行的时候指定容器名称,容器名称必须唯一
– -name :给容器指定名称
– -name与容器名称在一块,不能分开

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker run --name centos_t1 centos 

-it:运行容器,并且进入容器中
退出容器:exit

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker run -it --name centos_t2 centos
[root@90f0e8d75990 /]# 

✨例如:进入到centos容器中了

在这里插入图片描述

选择tag,建议使用alpine:非常小

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker run --name nginx_1 nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/06/02 05:58:04 [notice] 1#1: using the "epoll" event method
2022/06/02 05:58:04 [notice] 1#1: nginx/1.21.6
2022/06/02 05:58:04 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2022/06/02 05:58:04 [notice] 1#1: OS: Linux 5.13.0-40-generic
2022/06/02 05:58:04 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/06/02 05:58:04 [notice] 1#1: start worker processes
2022/06/02 05:58:04 [notice] 1#1: start worker process 31
2022/06/02 05:58:04 [notice] 1#1: start worker process 32
2022/06/02 05:58:04 [notice] 1#1: start worker process 33
2022/06/02 05:58:04 [notice] 1#1: start worker process 34
2022/06/02 05:58:06 [notice] 1#1: signal 28 (SIGWINCH) received

宿主机(ubuntu)的端口映射容器端口(访问宿主机中的1111端口相当于访问容器中的80端口)
-p:手动指定端口
-P:不需要手动指定端口,会随机的指定端口

✨例如启动nginx手动指定端口

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker run --name nginx_2 -p 1111:80 nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/06/02 06:06:19 [notice] 1#1: using the "epoll" event method
2022/06/02 06:06:19 [notice] 1#1: nginx/1.21.6
2022/06/02 06:06:19 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2022/06/02 06:06:19 [notice] 1#1: OS: Linux 5.13.0-40-generic
2022/06/02 06:06:19 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/06/02 06:06:19 [notice] 1#1: start worker processes
2022/06/02 06:06:19 [notice] 1#1: start worker process 32
2022/06/02 06:06:19 [notice] 1#1: start worker process 33
2022/06/02 06:06:19 [notice] 1#1: start worker process 34
2022/06/02 06:06:19 [notice] 1#1: start worker process 35
^C2022/06/02 06:07:31 [notice] 1#1: signal 2 (SIGINT) received, exiting
2022/06/02 06:07:31 [notice] 33#33: exiting
2022/06/02 06:07:31 [notice] 34#34: exiting
2022/06/02 06:07:31 [notice] 32#32: exiting
2022/06/02 06:07:31 [notice] 35#35: exiting
2022/06/02 06:07:31 [notice] 34#34: exit
2022/06/02 06:07:31 [notice] 33#33: exit
2022/06/02 06:07:31 [notice] 32#32: exit
2022/06/02 06:07:31 [notice] 35#35: exit
2022/06/02 06:07:31 [notice] 1#1: signal 17 (SIGCHLD) received from 35
2022/06/02 06:07:31 [notice] 1#1: worker process 32 exited with code 0
2022/06/02 06:07:31 [notice] 1#1: worker process 35 exited with code 0
2022/06/02 06:07:31 [notice] 1#1: signal 29 (SIGIO) received
2022/06/02 06:07:31 [notice] 1#1: signal 17 (SIGCHLD) received from 33
2022/06/02 06:07:31 [notice] 1#1: worker process 33 exited with code 0
2022/06/02 06:07:31 [notice] 1#1: signal 29 (SIGIO) received
2022/06/02 06:07:31 [notice] 1#1: signal 17 (SIGCHLD) received from 34
2022/06/02 06:07:31 [notice] 1#1: worker process 34 exited with code 0
2022/06/02 06:07:31 [notice] 1#1: exit

✨小写p:宿主机访问1111端口
在这里插入图片描述
✨大写P:宿主机访问49161端口(随机指定的)
在这里插入图片描述

🌏 目前存在的问题:1、启动nginx,终端会展示日志;2、一旦关闭终端,容器将停止工作。
解决:

🌙9、后台运行容器

-d:指定容器在后台执行
特别注意:如果是手动指定端口,需要将防火墙中的这个端口放通

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker run --name nginx_4 -P -d nginx
fa855451e519418cf4348a16dcaf52dcbd53e971bc934e196bdb3130acd72026

– -rm:退出容器后,会自动将容器删除

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker run -it --name centos_1 --rm centos
[root@f992808f12cc /]# exit
exit

🌙10、创建容器的时候指定环境变量

-e:设置全局变量

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker run --rm -it -e USERNAME=kobe -e PASSWORD=123123 centos /bin/bash
[root@0489627cb70f /]# env
LANG=en_US.UTF-8
HOSTNAME=0489627cb70f
USERNAME=kobe
PASSWORD=123123
PWD=/
HOME=/root
TERM=xterm
SHLVL=1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
LESSOPEN=||/usr/bin/lesspipe.sh %s
_=/usr/bin/env

🌙11、进入正在运行中的容器

exec:进入正在运行中的容器

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker exec -it centos_1 /bin/bash
[root@7e1cf2a4bec7 /]# 

✨当容器太多时,查看需要的容器

zhilong@zhilong-virtual-machine:~/桌面$ sudo docker ps | awk '/Up/ {print $1,$NF}'
95cf126972d5 centos_2
7e1cf2a4bec7 centos_1
d992bc0fa330 nginx_test
2c4108268aa9 jenkins
ae1fd6dd1b00 mysql

🌙12、查看某个容器的进程

zhilong@zhilong-virtual-machine:~/桌面$ sudo docker top centos_1
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                52297               52276               0                   14:31               pts/0               00:00:00            /bin/bash
zhilong@zhilong-virtual-machine:~/桌面$ 

🌙13、查看容器资源利用率

CONTAINER ID   NAME       CPU %     MEM USAGE / LIMIT     MEM %     NET I/O       BLOCK I/O     PIDS
7e1cf2a4bec7   centos_1   0.00%     2.566MiB / 3.798GiB   0.07%     3.79kB / 0B   1.43MB / 0B   1

停止容器:sudo docker stop 容器id或者容器名称
重启容器:sudo docker restart 容器id或者容器名称
暂停容器:sudo docker paush 容器id或者容器名称
取消暂停容器:sudo docker unpaush 容器id或者容器名称
删除容器:sudo docker rm 容器id或者容器名称(前提是先暂停容器)
强制删除容器:sudo docker rm -f 容器id或者容器名称

二、网络

🌙1、网络类型:

✨bridge:

  • nat网络模型

  • 虚拟交换机

✨host

  • 与宿主机共享网络

  • –net=host

✨none

  • 不配置网络

  • –net=none

✨overlay

  • 不同网络进行通信

🌙2、查看容器的ip地址

sudo docker inspect nginx_1:查看容器的详细信息
sudo docker exec -it nginx_1 ip a:进入容器查看容器的ip
172.17.0.5

✨第一种方法:

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker exec -it nginx_1 ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
112: eth0@if113: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP 
    link/ether 02:42:ac:11:00:05 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.5/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever

✨第二方法:

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker inspect nginx_1 | grep "IPAddress"
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.5",
                    "IPAddress": "172.17.0.5",
zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker inspect nginx_2 | grep "IPAddress"
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.6",
                    "IPAddress": "172.17.0.6",
zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker inspect nginx_3 | grep "IPAddress"
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.7",
                    "IPAddress": "172.17.0.7",

✨查看nginx_1、nginx_2、nginx_3三个容器的ip地址

在这里插入图片描述

🌏查看nginx_1、nginx_2、nginx_3三个容器的网关地址,得到结论:用sudo docker run创建的容器会默认挂载到docker0上面,docker0作为一个桥梁进行上网。所以可以上外网

在这里插入图片描述

🌙3、创建网络:默认指定的网关地址和子网地址

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker network create -d bridge mynet2
80a7f08272dde3d0b7dd7a91b67b823415e554dd5d8bae8bee0293697b055680

✨查看详情

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker network inspect mynet2
[
    {
        "Name": "mynet2",
        "Id": "80a7f08272dde3d0b7dd7a91b67b823415e554dd5d8bae8bee0293697b055680",
        "Created": "2022-06-02T19:44:30.465164936+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.20.0.0/16",
                    "Gateway": "172.20.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {}
    }
]

🌙4、创建网络:手动指定网关

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker network create -d bridge --subnet 172.16.200.0/24 mynet3
0e062b2fd2340178cc7e6fc72377418314236f9f2be826cb504d45e18be124da

🌏使用sudo docker run 创建的容器会默认加入到docker0桥接网络里面,这个桥接网络里面是不支持通过容器的名字去访问的;
但是我们自己创建的桥接网络,除了可以使用ip地址去访问以外,还可以通过容器名称去访问。

🌙5、删除网络

sudo docker network rm 网络
如果容器正在启动中,不能直接删除网络,需要先将容器删除,再删除网络

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker network rm mynet1
[sudo] zhilong 的密码: 
mynet1

sudo docker network prune:把无用的网络删除

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/docker$ sudo docker network prune
WARNING! This will remove all custom networks not used by at least one container.
Are you sure you want to continue? [y/N] y
Deleted Networks:
mynet
mynet2
mynet3

三、Volume

实现容器和宿主机之间的数据共享

🌙1、介绍

使用数据卷实现数据持久化
数据备份、数据共享

将宿主机中的目录和容器中的目录进行映射
将宿主机中的文件和容器中的文件进行映射
将数据卷与容器中的目录进行映射

🌙2、例如将目录“/home/zhilong/Desktop/shell_text”与容器中的目录做映射

-v:映射
如果冒号左边的目录或者文件不存在,会自动创建数据卷,通过sudo docker volume ls查看
sudo docker inspect 数据卷名称:查看数据卷的详细信息
可以查看数据卷的路径:/var/lib/docker/volumes/数据卷名称/_data

zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ sudo docker run --name nginx_02 -d -P -v /home/zhilong/Desktop/shell_text/nginx_test:/usr/share/nginx/html/ nginx:alpine
42ea2b7b22f51ff6f43de6362b1b81c4112e3ddf155e4c0f100c9b33acd09cbd

🌙3、在宿主机的nginx_test目录下创建一个index.html文件,通过容器去访问

🌏进入容器中,可以看到和宿主机中有一样的文件

zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ sudo docker exec -it nginx_02
"docker exec" requires at least 2 arguments.
See 'docker exec --help'.

Usage:  docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

Run a command in a running container
zhilong@zhilong-virtual-machine:~/Desktop/shell_text$ sudo docker exec -it nginx_02 /bin/sh
/ # cd /usr/share/nginx/html
/usr/share/nginx/html # ls
6.html      index.html
/usr/share/nginx/html # 

在这里插入图片描述

🌏在容器的/usr/share/nginx/html/目录下创建文件,也可以到宿主机中看到;容器中的目录、文件和宿主机中的文件和目录是一致的。
可以在宿主机中动态的修改文件,容器中可以动态的识别文件

🌙4、假如nginx_1容器挂掉了,去创建一个新的容器nginx_2,同样做映射,数据并不会丢

🌙5、创建数据卷

sudo docker volume create 数据卷

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/nginx_test$ sudo docker volume create v1
v1

🌙6、删除数据卷

sudo docker volume rm 数据卷

zhilong@zhilong-virtual-machine:~/Desktop/shell_text/nginx_test$ sudo docker volume rm v101
Error: No such volume: v101

🌙7、删除没用的数据卷

sudo docker volume prune

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/123262.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!