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

前提:必须有一个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 imagesREPOSITORYTAG IMAGE ID CREATEDSIZEnginx latest0e901e68141f 4 days ago 142MBjenkins/jenkins lts cb535f2a7054 5 days ago 464MBnginx alpineb1c3acb28882 2 weeks ago23.4MBmysql 8.0.2996d0eae5ed60 4 weeks ago524MBmysql latest96d0eae5ed60 4 weeks ago524MBhello-world latestfeb5d9fea6a5 8 months ago 13.3kBcentoslatest5d0da3dc9764 8 months ago 231MBdelron/fastdfslatest8487e86fc6ee 4 years ago464MBdelron/elasticsearch-ik 2.4.6-1.0 095b6487fb77 4 years ago689MBmorunchang/fastdfslatesta729ac95698a 5 years ago460MB

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 redisNAME DESCRIPTION STARS OFFICIAL AUTOMATEDredisRedis is an open source key-value store that… 10992 [OK] bitnami/redisBitnami Redis Docker Image220[OK]bitnami/redis-sentinel Bitnami Docker Image for Redis Sentinel 37 [OK]bitnami/redis-cluster32 rapidfort/redisRapidFort optimized, hardened image for Redi… 14 rapidfort/redis-clusterRapidFort 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 7clearlinux/redis Redis key-value data structure server with t… 4google/guestbook-python-redisA simple guestbook example written in Python… 4ibmcom/redis-ha1bitnami/redis-sentinel-exporter1ibmcom/ibm-cloud-databases-redis-operatorContainer image for the IBM Operator for Red… 1ibmcom/ibm-cloud-databases-redis-operator-bundle Bundle image for the IBM Operator for Redis 1ibmcom/ibm-cloud-databases-redis-catalog Catalog image for the IBM Operator for Redis1ibmcom/redis-ppc64le 0blackflysolutions/redisRedis container for Drupal and CiviCRM0ibmcom/redisearch-ppc64le0drud/redis redis 0[OK]rancher/redislabs-ssl0vmware/redis-photon0cimg/redis 0newrelic/k8s-nri-redis New Relic Infrastructure Redis Integration (… 0astronomerinc/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 pythonUsing default tag: latestlatest: Pulling from library/pythone756f3fdd6a3: 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:bef6fd726fb8825d5cf26933d8477505b14505be1a98394405c8840c1c266bddStatus: Downloaded newer image for python:latestdocker.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 lsCONTAINER ID IMAGE COMMANDCREATEDSTATUSPORTSNAMESbca70facba90 nginx:alpine"/docker-entrypoint.…" 3 hours agoUp 3 hours0.0.0.0:49160->80/tcp, :::49160->80/tcpweb4ff2d3c951eed nginx:alpine"/docker-entrypoint.…" 3 hours agoUp 3 hours0.0.0.0:49159->80/tcp, :::49159->80/tcpweb34011f219f403 nginx:alpine"/docker-entrypoint.…" 3 hours agoUp 3 hours0.0.0.0:49158->80/tcp, :::49158->80/tcpweb2d992bc0fa330 nginx "/docker-entrypoint.…" 3 hours agoUp 3 hours80/tcp nginx_test2c4108268aa9 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 jenkinsae1fd6dd1b00 mysql "docker-entrypoint.s…" 3 weeks agoUp 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 redisUnable to find image 'redis:latest' locallylatest: Pulling from library/redis42c077c10790: Already exists a300d83d65f9: Pull complete ebdc3afaab5c: Pull complete 31eec7f8651c: Pull complete 9c6a6b89d274: Pull complete 5c8099a4b45c: Pull complete Digest: sha256:1b90dbfe6943c72a7469c134cad3f02eb810f016049a0e19ad78be07040cdb0cStatus: Downloaded newer image for redis:latest1:C 02 Jun 2022 05:36:13.616 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo1:C 02 Jun 2022 05:36:13.616 # Redis version=7.0.0, bits=64, commit=00000000, modified=0, pid=1, just started1: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.conf1:M 02 Jun 2022 05:36:13.616 * monotonic clock: POSIX clock_gettime1:M 02 Jun 2022 05:36:13.617 * Running mode=standalone, port=6379.1:M 02 Jun 2022 05:36:13.617 # Server initialized1: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 exist1: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 disk1: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.sh10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf10-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 up2022/06/02 05:58:04 [notice] 1#1: using the "epoll" event method2022/06/02 05:58:04 [notice] 1#1: nginx/1.21.62022/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-generic2022/06/02 05:58:04 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:10485762022/06/02 05:58:04 [notice] 1#1: start worker processes2022/06/02 05:58:04 [notice] 1#1: start worker process 312022/06/02 05:58:04 [notice] 1#1: start worker process 322022/06/02 05:58:04 [notice] 1#1: start worker process 332022/06/02 05:58:04 [notice] 1#1: start worker process 342022/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.sh10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf10-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 up2022/06/02 06:06:19 [notice] 1#1: using the "epoll" event method2022/06/02 06:06:19 [notice] 1#1: nginx/1.21.62022/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-generic2022/06/02 06:06:19 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:10485762022/06/02 06:06:19 [notice] 1#1: start worker processes2022/06/02 06:06:19 [notice] 1#1: start worker process 322022/06/02 06:06:19 [notice] 1#1: start worker process 332022/06/02 06:06:19 [notice] 1#1: start worker process 342022/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, exiting2022/06/02 06:07:31 [notice] 33#33: exiting2022/06/02 06:07:31 [notice] 34#34: exiting2022/06/02 06:07:31 [notice] 32#32: exiting2022/06/02 06:07:31 [notice] 35#35: exiting2022/06/02 06:07:31 [notice] 34#34: exit2022/06/02 06:07:31 [notice] 33#33: exit2022/06/02 06:07:31 [notice] 32#32: exit2022/06/02 06:07:31 [notice] 35#35: exit2022/06/02 06:07:31 [notice] 1#1: signal 17 (SIGCHLD) received from 352022/06/02 06:07:31 [notice] 1#1: worker process 32 exited with code 02022/06/02 06:07:31 [notice] 1#1: worker process 35 exited with code 02022/06/02 06:07:31 [notice] 1#1: signal 29 (SIGIO) received2022/06/02 06:07:31 [notice] 1#1: signal 17 (SIGCHLD) received from 332022/06/02 06:07:31 [notice] 1#1: worker process 33 exited with code 02022/06/02 06:07:31 [notice] 1#1: signal 29 (SIGIO) received2022/06/02 06:07:31 [notice] 1#1: signal 17 (SIGCHLD) received from 342022/06/02 06:07:31 [notice] 1#1: worker process 34 exited with code 02022/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 nginxfa855451e519418cf4348a16dcaf52dcbd53e971bc934e196bdb3130acd72026

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

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

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 /]# envLANG=en_US.UTF-8HOSTNAME=0489627cb70fUSERNAME=kobePASSWORD=123123PWD=/HOME=/rootTERM=xtermSHLVL=1PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binLESSOPEN=||/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_27e1cf2a4bec7 centos_1d992bc0fa330 nginx_test2c4108268aa9 jenkinsae1fd6dd1b00 mysql

12、查看某个容器的进程

zhilong@zhilong-virtual-machine:~/桌面$ sudo docker top centos_1UID PID PPIDC STIME TTY TIMECMDroot52297 52276 0 14:31 pts/0 00:00:00/bin/bashzhilong@zhilong-virtual-machine:~/桌面$ 

13、查看容器资源利用率

CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS7e1cf2a4bec7 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 a1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever112: 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:ffinet 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 mynet280a7f08272dde3d0b7dd7a91b67b823415e554dd5d8bae8bee0293697b055680

✨查看详情

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 mynet30e062b2fd2340178cc7e6fc72377418314236f9f2be826cb504d45e18be124da

使用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 pruneWARNING! This will remove all custom networks not used by at least one container.Are you sure you want to continue" />[y/N] yDeleted Networks:mynetmynet2mynet3

三、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:alpine42ea2b7b22f51ff6f43de6362b1b81c4112e3ddf155e4c0f100c9b33acd09cbd

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 containerzhilong@zhilong-virtual-machine:~/Desktop/shell_text$ sudo docker exec -it nginx_02 /bin/sh/ # cd /usr/share/nginx/html/usr/share/nginx/html # ls6.htmlindex.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 v1v1

6、删除数据卷

sudo docker volume rm 数据卷

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

7、删除没用的数据卷

sudo docker volume prune