一、帮助命令

  1. docker version #查看docker版本(有client和server,看到server起来了才说明docker起来了)
  2. docker info #显示docker的系统信息,包括镜像和容器的数量
  3. docker 命令 --help #万能命令

帮助文档的地址:https://docs.docker.com/reference/
image.png

二、镜像命令

1、查看镜像

  • **docker images** 列出本地镜像
  • 语法:**docker images [OPTIONS] [REPOSITORY[:TAG]]**
  • OPTIONS说明: | -a,—all | 列出本地所有的镜像(含中间映像层,默认情况下,隐藏中间映像层) | | —- | —- | | —digests | 显示镜像的摘要信息(签名) | | -f,—fliter | 根据条件对输出结果进行过滤 | | —format | 格式化输出结果 | | —no-trunc | 不截断输出,即显示完整的镜像ID信息 | | -q | 只显示镜像ID |
  1. #列出最近创建的镜像
  2. [root@VM-0-15-centos ~]# docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. mysql latest 5a4e492065c7 11 days ago 514MB
  5. hello-world latest d1165f221234 5 months ago 13.3kB
  6. centos latest 300e315adb2f 8 months ago 209MB
  7. 80x86/typecho latest 3437f7346b4c 2 years ago 53.9MB
  8. #列出指定镜像名hello-world:latest的信息
  9. [root@VM-0-15-centos ~]# docker images hello-world:latest
  10. REPOSITORY TAG IMAGE ID CREATED SIZE
  11. hello-world latest d1165f221234 5 months ago 13.3kB
  12. #列出镜像名为hello-world的所有镜像信息
  13. [root@VM-0-15-centos ~]# docker images hello-world
  14. REPOSITORY TAG IMAGE ID CREATED SIZE
  15. hello-world latest d1165f221234 5 months ago 13.3kB
  16. #也可以使用模糊查找的方式:
  17. [root@VM-0-15-centos ~]# docker images hello-world:*
  18. REPOSITORY TAG IMAGE ID CREATED SIZE
  19. hello-world latest d1165f221234 5 months ago 13.3kB
  20. #显示镜像完整的ID
  21. [root@VM-0-15-centos ~]# docker images --no-trunc
  22. REPOSITORY TAG IMAGE ID CREATED SIZE
  23. mysql latest sha256:5a4e492065c722ec8cc7413552bafc6fd5434c5ad90797e898ccc4e347e21aa5 11 days ago 514MB
  24. hello-world latest sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726 5 months ago 13.3kB
  25. centos latest sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55 8 months ago 209MB
  26. 80x86/typecho latest sha256:3437f7346b4c58c7451ff6e398f9b8e4019a7a34186af61aba54d0f00a71a398 2 years ago 53.9MB
  27. #显示镜像的摘要(签名)
  28. [root@VM-0-15-centos ~]# docker images --digests
  29. REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE
  30. mysql latest sha256:d45561a65aba6edac77be36e0a53f0c1fba67b951cb728348522b671ad63f926 5a4e492065c7 11 days ago 514MB
  31. hello-world latest sha256:7d91b69e04a9029b99f3585aaaccae2baa80bcf318f4a5d2165a9898cd2dc0a1 d1165f221234 5 months ago 13.3kB
  32. centos latest sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1 300e315adb2f 8 months ago 209MB
  33. 80x86/typecho latest sha256:4af25c76919efa862d59aeb3a2d9f2394ad3ef4b338037e8a2499c55011dbbf7 3437f7346b4c 2 years ago 53.9MB
  34. #筛选在hello-world:latest镜像之前创建的镜像
  35. [root@VM-0-15-centos ~]# docker images -f "before=hello-world:latest"
  36. REPOSITORY TAG IMAGE ID CREATED SIZE
  37. centos latest 300e315adb2f 8 months ago 209MB
  38. 80x86/typecho latest 3437f7346b4c 2 years ago 53.9MB
  39. #筛选在hello-world:latest镜像之后创建的镜像
  40. [root@VM-0-15-centos ~]# docker images -f "since=hello-world:latest"
  41. REPOSITORY TAG IMAGE ID CREATED SIZE
  42. mysql latest 5a4e492065c7 11 days ago 514MB
  43. #列出虚悬镜像
  44. [root@VM-0-15-centos ~]# docker images -f "dangling=true"
  45. REPOSITORY TAG IMAGE ID CREATED SIZE
  46. #显示镜像ID和镜像名
  47. [root@VM-0-15-centos ~]# docker images --format "{{.ID}}: {{.Repository}}"
  48. 5a4e492065c7: mysql
  49. d1165f221234: hello-world
  50. 300e315adb2f: centos
  51. 3437f7346b4c: 80x86/typecho
  52. #以表格的形式显示镜像ID、镜像名和镜像标签
  53. [root@VM-0-15-centos ~]# docker images --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}"
  54. IMAGE ID REPOSITORY TAG
  55. 5a4e492065c7 mysql latest
  56. d1165f221234 hello-world latest
  57. 300e315adb2f centos latest
  58. 3437f7346b4c 80x86/typecho latest
  59. #解释
  60. REPOSITORY 镜像的仓库源
  61. TAG 镜像的标签
  62. IMAGE ID 镜像的id
  63. CREATED 镜像的创建时间
  64. SIZE 镜像的大小

2、搜索镜像

  • **docker search** 搜索镜像
  • 语法:**docker search [OPTIONS] TERM**
  • OPTIONS说明: | —automated | 只列出 automated build类型的镜像 | | —- | —- | | -f,—filter | 列出收藏数不小于指定值的镜像 | | —no-trunc | 不截断输出,即显示完整的镜像ID信息 |
  1. [root@VM-0-15-centos ~]# docker search mysql
  2. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  3. mysql MySQL is a widely used, open-source relation 11332 [OK]
  4. mariadb MariaDB Server is a high performing open sou 4307 [OK]
  5. mysql/mysql-server Optimized MySQL Server Docker images. Create 840 [OK]
  6. phpmyadmin phpMyAdmin - A web interface for MySQL and M 305 [OK]
  7. #从 Docker Hub 查找所有镜像名包含mysql,并且收藏数大于等于 3000 的镜像
  8. [root@VM-0-15-centos ~]# docker search mysql -f stars=3000
  9. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  10. mysql MySQL is a widely used, open-source relation 11334 [OK]
  11. mariadb MariaDB Server is a high performing open sou 4307 [OK]
  12. #参数说明
  13. NAME #镜像仓库源的名称
  14. DESCRIPTION #镜像的描述
  15. OFFICIAL #是否 docker 官方发布
  16. STARS #类似 Github 里面的 star,表示点赞、喜欢的意思。
  17. AUTOMATED #自动构建

3、下载镜像

  • **docker pull** 下载镜像
  • 语法:**docker pull [OPTIONS] NAME[:TAG|@DIGEST]**
  • OPTIONS说明: | -a | 拉取所有 tagged 镜像 | | —- | —- | | —disable-content-trust | 忽略镜像的校验,默认开启 |
  1. 下载镜像 docker pull 镜像名[:tag]
  2. [root@VM-0-15-centos ~]# docker pull mysql
  3. Using default tag: latest #如果不写tag,默认就是latest
  4. latest: Pulling from library/mysql
  5. e1acddbe380c: Pull complete #分层下载,docker image的核心 联合文件系统
  6. bed879327370: Pull complete
  7. 03285f80bafd: Pull complete
  8. ccc17412a00a: Pull complete
  9. 1f556ecc09d1: Pull complete
  10. adc5528e468d: Pull complete
  11. 1afc286d5d53: Pull complete
  12. 6c724a59adff: Pull complete
  13. 0f2345f8b0a3: Pull complete
  14. c8461a25b23b: Pull complete
  15. 3adb49279bed: Pull complete
  16. 77f22cd6c363: Pull complete
  17. Digest: sha256:d45561a65aba6edac77be36e0a53f0c1fba67b951cb728348522b671ad63f926 #签名
  18. Status: Downloaded newer image for mysql:latest
  19. docker.io/library/mysql:latest #真实地址
  20. #等价关系
  21. docker pull mysql
  22. docker pull docker.io/library/mysql:latest
  23. #指定版本下载
  24. [root@VM-0-15-centos ~]# docker pull mysql:5.7
  25. 5.7: Pulling from library/mysql
  26. e1acddbe380c: Already exists
  27. bed879327370: Already exists
  28. 03285f80bafd: Already exists
  29. ccc17412a00a: Already exists
  30. 1f556ecc09d1: Already exists
  31. adc5528e468d: Already exists
  32. 1afc286d5d53: Already exists
  33. 4d2d9261e3ad: Pull complete
  34. ac609d7b31f8: Pull complete
  35. 53ee1339bc3a: Pull complete
  36. b0c0a831a707: Pull complete
  37. Digest: sha256:7cf2e7d7ff876f93c8601406a5aa17484e6623875e64e7acc71432ad8e0a3d7e
  38. Status: Downloaded newer image for mysql:5.7
  39. docker.io/library/mysql:5.7
  40. #下载REPOSITORY为mysql的所有镜像
  41. docker pull -a mysql

image.png

4、删除镜像

  • **docker rmi** 删除本地一个或多个镜像
  • 语法:**docker rmi [OPTIONS] IMAGE [IMAGE...]**
  • OPTIONS说明: | -f | 强制删除 | | —- | —- | | —no-prune | 不移除该镜像的过程镜像,默认移除 |
  1. [root@VM-0-15-centos ~]# docker rmi -f 镜像id #强制删除指定的镜像
  2. [root@VM-0-15-centos ~]# docker rmi -f 镜像id 镜像id 镜像id #强制删除多个指定的镜像
  3. [root@VM-0-15-centos ~]# docker rmi -f $(docker images -aq) #强制删除所有的镜像

三、容器命令

说明:我们有了镜像才可以创建容器,Linux系统下下载一个CentOS镜像来测试学习

  • **docker pull centos**

image.png

1、新建容器并运行命令

  • **docker run** 创建一个新的容器并运行一个命令
  • 语法:**docker run [OPTIONS] IMAGE [COMMAND] [ARG...]**
  • OPTIONS说明: | -d, —detach=false | **指定容器运行于前台还是后台,默认为false,为true则后台运行 | | —- | —- | | -i, —interactive=false | **打开STDIN,用于控制台交互 | | -t, —tty=false | 分配tty设备,该可以支持终端登录,默认为false | | -u, —user=”” | 指定容器的用户 | | -a, —attach=[] | 登录容器(必须是以docker run -d启动的容器) | | -w, —workdir=”” | 指定容器的工作目录 | | -c, —cpu-shares=0 | 设置容器CPU权重,在CPU共享场景使用 | | -e, —env=[] | 指定环境变量,容器中可以使用该环境变量 | | -m, —memory=”” | 指定容器的内存上限 | | -P, —publish-all=false | 随机指定容器暴露的端口 | | -p, —publish=[] | 指定容器暴露的端口 | | -h, —hostname=”” | 指定容器的主机名 | | -v, —volume=[] | 给容器挂载存储卷,挂载到容器的某个目录 | | —volumes-from=[] | 给容器挂载其他容器上的卷,挂载到容器的某个目录 | | —cap-add=[] | 添加权限,权限清单详见:http://linux.die.net/man/7/capabilities | | —cap-drop=[] | 删除权限,权限清单详见:http://linux.die.net/man/7/capabilities | | —cidfile=”” | 运行容器后,在指定文件中写入容器PID值,一种典型的监控系统用法 | | —cpuset=”” | 设置容器可以使用哪些CPU,此参数可以用来容器独占CPU | | —device=[] | 添加主机设备给容器,相当于设备直通 | | —dns=[] | 指定容器的dns服务器 | | —dns-search=[] | 指定容器的dns搜索域名,写入到容器的/etc/resolv.conf文件 | | —entrypoint=”” | 覆盖image的入口点 | | —env-file=[] | 指定环境变量文件,文件格式为每行一个环境变量 | | —expose=[] | 指定容器暴露的端口,即修改镜像的暴露端口 | | —link=[] | 指定容器间的关联,使用其他容器的IP、env等信息 | | —lxc-conf=[] | 指定容器的配置文件,只有在指定—exec-driver=lxc时使用 | | —name=”” | 指定容器名字,后续可以通过名字进行容器管理,links特性需要使用名字 | | —net=”bridge” | 容器网络设置: 使用docker daemon指定的网桥 | | —net=”host” | 容器网络设置: 容器使用主机的网络 | | —net=”container” | 容器网络设置: 使用其他容器的网路,共享IP和PORT等网络资源 | | —net=”none” | 容器网络设置: 容器使用自己的网络(类似—net=bridge),但是不进行配置 | | —privileged=false | 指定容器是否为特权容器,特权容器拥有所有的capabilities | | —restart=”no” | 容器退出时不重启 | | —restart=”on-failure” | 容器故障退出(返回值非零)时重启 | | —restart=”always” | 容器退出时总是重启 | | —rm=false | 指定容器停止后自动删除容器(不支持以docker run -d启动的容器) | | —sig-proxy=true | 设置由代理接受并处理信号,但是SIGCHLD、SIGSTOP和SIGKILL不能被代理 |

  • docker run 镜像名 命令可选() #运行docker容器镜像

    • -d #让容器后台运行
    • -it #保持标准输入打开(默认为false),让容器保持交互状态,不把阻塞/等待输入当成是任务结束
    • -p 主机端口:容器端口 #映射容器的端口号
    • -P #随机映射镜像所需要的端口号
    • -v 主机路径:容器路径 #映射容器的存储卷

Docker常用命令 - 图4

  • —rm #容器运行结束后自动销毁(连尸体都不会有)
  • —name #设置容器名称
  • —hostname #设置容器的主机名
  • —restart=always #设置容器开机自动启动(systemctl start docker,systemctl enable docker搭配) ```shell

    运行一个在后台执行的容器,同时,还能用控制台管理

    [root@VM-0-15-centos ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@VM-0-15-centos ~]# docker run -i -t -d centos:latest e599b223e7e00bda6dd1af7d02248ea83c167e62e97d6a9ad821dc0b8e4c4a53 [root@VM-0-15-centos ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e599b223e7e0 centos:latest “/bin/bash” 5 seconds ago Up 4 seconds lucid_banach

运行一个带命令在后台不断执行的容器,不直接展示容器内部信息

[root@VM-0-15-centos ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e599b223e7e0 centos:latest “/bin/bash” 5 seconds ago Up 4 seconds lucid_banach [root@VM-0-15-centos ~]# docker run -d centos:latest ping www.docker.com 626b539eeba4c0fe45770d7db8782bf4f20a9c207fbdc726663700ac150cdd4e [root@VM-0-15-centos ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 626b539eeba4 centos:latest “ping www.docker.com” 5 seconds ago Up 4 seconds nifty_solomon e599b223e7e0 centos:latest “/bin/bash” 2 minutes ago Up 2 minutes lucid_banach

为容器指定一个名字。

容器暴露80端口,并指定宿主机2021端口与其通信(: 之前是宿主机端口,之后是容器需暴露的端口)

指定容器内目录与宿主机目录共享(: 之前是宿主机文件夹,之后是容器需共享的文件夹),

[root@VM-0-15-centos ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@VM-0-15-centos ~]# docker run -it -d —name=centos_server -p 2021:80 -v /etc/www:/var/www centos:latest 1e605144b3c4dc66112c7d8fba0b2dabada1d897523c56dc159705335dba5ae0 [root@VM-0-15-centos ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1e605144b3c4 centos:latest “/bin/bash” 3 seconds ago Up 2 seconds 0.0.0.0:2021->80/tcp, :::2021->80/tcp centos_server

测试,启动并进入容器

[root@VM-0-15-centos ~]# docker run -it centos /bin/bash [root@286621faf844 /]# ls #查看容器内的centos,基础版本,很多命令都不完善 bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var

  1. <a name="Egn4b"></a>
  2. ## 2、查看运行中的容器
  3. - `**docker ps**`
  4. - 语法:`**docker ps [OPTIONS]**`
  5. | **-a** | ** #查看所有容器,包括未运行的** |
  6. | --- | --- |
  7. | **-l** | ** #查看最后一个运行的容器** |
  8. | **-n** | ** #列出最近创建的n个容器** |
  9. | **--no-trunc** | **#不截断输出** |
  10. | **-q** | ** #静默模式,只显示容器ID** |
  11. | **-s** | **#显示总的文件大小** |
  12. ```shell
  13. [root@VM-0-15-centos ~]# docker ps
  14. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  15. [root@VM-0-15-centos ~]# docker ps -a
  16. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  17. 286621faf844 centos "/bin/bash" 6 minutes ago Exited (0) 4 minutes ago eloquent_chaplygin
  18. c3740c67c633 hello-world "/hello" 2 hours ago Exited (0) 2 hours ago agitated_lamarr
  19. dea8fbdd52bb 80x86/typecho "/entrypoint.sh /app…" 3 months ago Exited (137) 2 months ago infallible_blackwell
  20. 1663d813cab4 80x86/typecho "/entrypoint.sh /app…" 3 months ago Exited (137) 3 months ago cranky_dirac
  21. #列出最近创建的2个容器信息
  22. [root@VM-0-15-centos ~]# docker ps -n 2
  23. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  24. 286621faf844 centos "/bin/bash" 6 minutes ago Exited (0) 4 minutes ago eloquent_chaplygin
  25. c3740c67c633 hello-world "/hello" 2 hours ago Exited (0) 2 hours ago agitated_lamarr

3、退出容器

(1)容器里面

  • 当我们在容器里面的时候,退出容器的方式有多个

image.png

(2)容器外面

  • docker stop 容器名/id
  • docker start 容器名/id(已经停止的容器)

    4、进入当前正在运行的容器

  • docker attach 容器id #从前台进入容器(进入后exit退出了就会杀死容器)

  • docker exec -it 容器id bash #从后门进入容器(进入后exit退出了只是退出了bash,并不会杀死容器) ```shell

    我们通常容器都是使用后台方式运行的,需要进入容器,修改一些配置

命令

docker exec -it 容器id bashshell

方法一

[root@VM-0-15-centos ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4519755902f9 centos “/bin/bash” 3 minutes ago Up 3 minutes charming_mendeleev [root@VM-0-15-centos ~]# docker exec -it 4519755902f9 bash [root@4519755902f9 /]# ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 12:49 pts/0 00:00:00 /bin/bash root 15 0 0 12:54 pts/1 00:00:00 bash root 29 15 0 12:54 pts/1 00:00:00 ps -ef [root@4519755902f9 /]# exit exit [root@VM-0-15-centos ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4519755902f9 centos “/bin/bash” 6 minutes ago Up 6 minutes charming_mendeleev

方法二

[root@VM-0-15-centos ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4519755902f9 centos “/bin/bash” 6 minutes ago Up 6 minutes charming_mendeleev [root@VM-0-15-centos ~]# docker attach 4519755902f9 [root@4519755902f9 /]# exit exit [root@VM-0-15-centos ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

docker exec #进入容器后开启一个新的终端,可以在里面操作(常用)

docker attach #进入容器正在执行的终端,不会启动新的终端

  1. <a name="RRP1J"></a>
  2. ## 5、启动和停止容器的操作
  3. ```shell
  4. docker start 容器id #启动容器
  5. docker restart 容器id #重启容器
  6. docker stop 容器id #停止当前正在运行的容器
  7. docker kill 容器id #强制停止当前容器

6、删除容器

  1. docker rm 容器id #删除指定的容器,不能删除正在运行的容器,如果要强制删除rm -f
  2. docker rm -f $(docker ps -aq) #删除所有的容器
  3. docker ps -a -q|xargs docker rm #删除所有的容器。xargs 可以将管道或标准输入(stdin)数据转 换成命令行参数,也能够从文件的输出中读取数据。

四、常用其他命令

1、后台启动容器

  1. [root@VM-0-15-centos ~]# docker ps -a
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. [root@VM-0-15-centos ~]# docker run -d centos
  4. 629a55e2dcbd2c225fa93496c2c3afdda4fe3ecea978178f638cfc246c573021
  5. [root@VM-0-15-centos ~]# docker ps
  6. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  7. [root@VM-0-15-centos ~]# docker run -it -d centos
  8. 4519755902f94979002e9188463dc3684f34f52ee15ce7fc098a1819f618287b
  9. [root@VM-0-15-centos ~]# docker ps
  10. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  11. 4519755902f9 centos "/bin/bash" 3 seconds ago Up 2 seconds charming_mendeleev
  12. [root@VM-0-15-centos ~]# docker ps -a
  13. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  14. 4519755902f9 centos "/bin/bash" 9 seconds ago Up 8 seconds charming_mendeleev
  15. 629a55e2dcbd centos "/bin/bash" 25 seconds ago Exited (0) 24 seconds ago elastic_shockley
  16. #问题:docker ps发现centos停止了
  17. #常见的坑:docker容器使用后台运行,就必须要有一个前台进程,docker容器启动后,如果发现自己没有提供服务,就会立即停止

2、查看日志

  • **docker logs**
  • 语法:**docker logs [OPTIONS] CONTAINER** | -f | # 跟踪日志输出 | | —- | —- | | —since | # 显示某个开始时间的所有日志 | | -t | # 显示时间戳 | | —tail | # 仅列出最新N条容器日志 |
  1. #跟踪查看容器的日志输出
  2. [root@VM-0-15-centos ~]# docker ps
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. [root@VM-0-15-centos ~]# docker run -it -d centos ping www.docker.com
  5. 436d7e51cf42977ed7683169d0277fa50719e3b74b379e691324c1f9b65b7d2e
  6. [root@VM-0-15-centos ~]# docker ps
  7. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  8. 436d7e51cf42 centos "ping www.docker.com" 6 seconds ago Up 5 seconds musing_hypatia
  9. [root@VM-0-15-centos ~]# docker logs -f -t --tail 10 436d7e51cf42
  10. 2021-08-29T13:03:43.358471450Z 64 bytes from server-65-9-42-78.nrt12.r.cloudfront.net (65.9.42.78): icmp_seq=25 ttl=231 time=130 ms
  11. 2021-08-29T13:03:44.357885604Z 64 bytes from server-65-9-42-78.nrt12.r.cloudfront.net (65.9.42.78): icmp_seq=26 ttl=231 time=128 ms
  12. 2021-08-29T13:03:45.360448310Z 64 bytes from server-65-9-42-78.nrt12.r.cloudfront.net (65.9.42.78): icmp_seq=27 ttl=231 time=129 ms
  13. 2021-08-29T13:03:46.362365414Z 64 bytes from server-65-9-42-78.nrt12.r.cloudfront.net (65.9.42.78): icmp_seq=28 ttl=231 time=129 ms
  14. 2021-08-29T13:03:49.362508740Z 64 bytes from server-65-9-42-78.nrt12.r.cloudfront.net (65.9.42.78): icmp_seq=31 ttl=231 time=129 ms
  15. 2021-08-29T13:03:51.364265785Z 64 bytes from server-65-9-42-78.nrt12.r.cloudfront.net (65.9.42.78): icmp_seq=33 ttl=231 time=131 ms
  16. 2021-08-29T13:03:54.365170137Z 64 bytes from server-65-9-42-78.nrt12.r.cloudfront.net (65.9.42.78): icmp_seq=36 ttl=231 time=131 ms
  17. 2021-08-29T13:03:56.366807947Z 64 bytes from server-65-9-42-78.nrt12.r.cloudfront.net (65.9.42.78): icmp_seq=38 ttl=231 time=131 ms
  18. 2021-08-29T13:03:57.366455842Z 64 bytes from server-65-9-42-78.nrt12.r.cloudfront.net (65.9.42.78): icmp_seq=39 ttl=231 time=131 ms
  19. 2021-08-29T13:03:58.334405609Z 64 bytes from server-65-9-42-78.nrt12.r.cloudfront.net (65.9.42.78): icmp_seq=40 ttl=231 time=97.5 ms

3、查看容器中进程信息

  • **docker top 容器ID**

    1. [root@VM-0-15-centos ~]# docker top 436d7e51cf42
    2. UID PID PPID C STIME TTY TIME CMD
    3. root 5266 5248 0 21:03 pts/0 00:00:00 ping www.docker.com

    4、查看镜像的元数据

  • **docker inspect 容器ID** ```shell

    测试

    [root@VM-0-15-centos ~]# docker inspect ec6e3fdec8c7 [ {

    1. "Id": "ec6e3fdec8c7c01895d7d47a2ab08c93129c9237a7763f295d37b73f4e327095",
    2. "Created": "2021-08-29T01:53:05.435416978Z",
    3. "Path": "/bin/bash",
    4. "Args": [],
    5. "State": {
    6. "Status": "exited",
    7. "Running": false,
    8. "Paused": false,
    9. "Restarting": false,
    10. "OOMKilled": false,
    11. "Dead": false,
    12. "Pid": 0,
    13. "ExitCode": 0,
    14. "Error": "",
    15. "StartedAt": "2021-08-29T01:53:05.774289777Z",
    16. "FinishedAt": "2021-08-29T01:53:05.773591821Z"
    17. },
    18. "Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
    19. "ResolvConfPath": "/var/lib/docker/containers/ec6e3fdec8c7c01895d7d47a2ab08c93129c9237a7763f295d37b73f4e327095/resolv.conf",
    20. "HostnamePath": "/var/lib/docker/containers/ec6e3fdec8c7c01895d7d47a2ab08c93129c9237a7763f295d37b73f4e327095/hostname",
    21. "HostsPath": "/var/lib/docker/containers/ec6e3fdec8c7c01895d7d47a2ab08c93129c9237a7763f295d37b73f4e327095/hosts",
    22. "LogPath": "/var/lib/docker/containers/ec6e3fdec8c7c01895d7d47a2ab08c93129c9237a7763f295d37b73f4e327095/ec6e3fdec8c7c01895d7d47a2ab08c93129c9237a7763f295d37b73f4e327095-json.log",
    23. "Name": "/xenodochial_cartwright",
    24. "RestartCount": 0,
    25. "Driver": "overlay2",
    26. "Platform": "linux",
    27. "MountLabel": "",
    28. "ProcessLabel": "",
    29. "AppArmorProfile": "",
    30. "ExecIDs": null,
    31. "HostConfig": {
    32. "Binds": null,
    33. "ContainerIDFile": "",
    34. "LogConfig": {
    35. "Type": "json-file",
    36. "Config": {}
    37. },
    38. "NetworkMode": "default",
    39. "PortBindings": {},
    40. "RestartPolicy": {
    41. "Name": "no",
    42. "MaximumRetryCount": 0
    43. },
    44. "AutoRemove": false,
    45. "VolumeDriver": "",
    46. "VolumesFrom": null,
    47. "CapAdd": null,
    48. "CapDrop": null,
    49. "CgroupnsMode": "host",
    50. "Dns": [],
    51. "DnsOptions": [],
    52. "DnsSearch": [],
    53. "ExtraHosts": null,
    54. "GroupAdd": null,
    55. "IpcMode": "private",
    56. "Cgroup": "",
    57. "Links": null,
    58. "OomScoreAdj": 0,
    59. "PidMode": "",
    60. "Privileged": false,
    61. "PublishAllPorts": false,
    62. "ReadonlyRootfs": false,
    63. "SecurityOpt": null,
    64. "UTSMode": "",
    65. "UsernsMode": "",
    66. "ShmSize": 67108864,
    67. "Runtime": "runc",
    68. "ConsoleSize": [
    69. 0,
    70. 0
    71. ],
    72. "Isolation": "",
    73. "CpuShares": 0,
    74. "Memory": 0,
    75. "NanoCpus": 0,
    76. "CgroupParent": "",
    77. "BlkioWeight": 0,
    78. "BlkioWeightDevice": [],
    79. "BlkioDeviceReadBps": null,
    80. "BlkioDeviceWriteBps": null,
    81. "BlkioDeviceReadIOps": null,
    82. "BlkioDeviceWriteIOps": null,
    83. "CpuPeriod": 0,
    84. "CpuQuota": 0,
    85. "CpuRealtimePeriod": 0,
    86. "CpuRealtimeRuntime": 0,
    87. "CpusetCpus": "",
    88. "CpusetMems": "",
    89. "Devices": [],
    90. "DeviceCgroupRules": null,
    91. "DeviceRequests": null,
    92. "KernelMemory": 0,
    93. "KernelMemoryTCP": 0,
    94. "MemoryReservation": 0,
    95. "MemorySwap": 0,
    96. "MemorySwappiness": null,
    97. "OomKillDisable": false,
    98. "PidsLimit": null,
    99. "Ulimits": null,
    100. "CpuCount": 0,
    101. "CpuPercent": 0,
    102. "IOMaximumIOps": 0,
    103. "IOMaximumBandwidth": 0,
    104. "MaskedPaths": [
    105. "/proc/asound",
    106. "/proc/acpi",
    107. "/proc/kcore",
    108. "/proc/keys",
    109. "/proc/latency_stats",
    110. "/proc/timer_list",
    111. "/proc/timer_stats",
    112. "/proc/sched_debug",
    113. "/proc/scsi",
    114. "/sys/firmware"
    115. ],
    116. "ReadonlyPaths": [
    117. "/proc/bus",
    118. "/proc/fs",
    119. "/proc/irq",
    120. "/proc/sys",
    121. "/proc/sysrq-trigger"
    122. ]
    123. },
    124. "GraphDriver": {
    125. "Data": {
    126. "LowerDir": "/var/lib/docker/overlay2/9f58829d0c22ab90bee08aa58ddf072e689f795d72c4dfd3b5a95759a11357da-init/diff:/var/lib/docker/overlay2/b298ad9ced6d87c18f01408e96b63e31dbc7ef41c7b5d6a749e54636a7730b15/diff",
    127. "MergedDir": "/var/lib/docker/overlay2/9f58829d0c22ab90bee08aa58ddf072e689f795d72c4dfd3b5a95759a11357da/merged",
    128. "UpperDir": "/var/lib/docker/overlay2/9f58829d0c22ab90bee08aa58ddf072e689f795d72c4dfd3b5a95759a11357da/diff",
    129. "WorkDir": "/var/lib/docker/overlay2/9f58829d0c22ab90bee08aa58ddf072e689f795d72c4dfd3b5a95759a11357da/work"
    130. },
    131. "Name": "overlay2"
    132. },
    133. "Mounts": [],
    134. "Config": {
    135. "Hostname": "ec6e3fdec8c7",
    136. "Domainname": "",
    137. "User": "",
    138. "AttachStdin": false,
    139. "AttachStdout": false,
    140. "AttachStderr": false,
    141. "Tty": false,
    142. "OpenStdin": false,
    143. "StdinOnce": false,
    144. "Env": [
    145. "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
    146. ],
    147. "Cmd": [
    148. "/bin/bash"
    149. ],
    150. "Image": "centos",
    151. "Volumes": null,
    152. "WorkingDir": "",
    153. "Entrypoint": null,
    154. "OnBuild": null,
    155. "Labels": {
    156. "org.label-schema.build-date": "20201204",
    157. "org.label-schema.license": "GPLv2",
    158. "org.label-schema.name": "CentOS Base Image",
    159. "org.label-schema.schema-version": "1.0",
    160. "org.label-schema.vendor": "CentOS"
    161. }
    162. },
    163. "NetworkSettings": {
    164. "Bridge": "",
    165. "SandboxID": "d3fedfdee8e6f0d6aa83a793c4e3e56ef29086f8e2cb6ecafc6c280c3d103b8f",
    166. "HairpinMode": false,
    167. "LinkLocalIPv6Address": "",
    168. "LinkLocalIPv6PrefixLen": 0,
    169. "Ports": {},
    170. "SandboxKey": "/var/run/docker/netns/d3fedfdee8e6",
    171. "SecondaryIPAddresses": null,
    172. "SecondaryIPv6Addresses": null,
    173. "EndpointID": "",
    174. "Gateway": "",
    175. "GlobalIPv6Address": "",
    176. "GlobalIPv6PrefixLen": 0,
    177. "IPAddress": "",
    178. "IPPrefixLen": 0,
    179. "IPv6Gateway": "",
    180. "MacAddress": "",
    181. "Networks": {
    182. "bridge": {
    183. "IPAMConfig": null,
    184. "Links": null,
    185. "Aliases": null,
    186. "NetworkID": "115a2351052d4f81bc4d8f1c20f87f29da1d4bb50ae3543e2d8a1675b1630dc1",
    187. "EndpointID": "",
    188. "Gateway": "",
    189. "IPAddress": "",
    190. "IPPrefixLen": 0,
    191. "IPv6Gateway": "",
    192. "GlobalIPv6Address": "",
    193. "GlobalIPv6PrefixLen": 0,
    194. "MacAddress": "",
    195. "DriverOpts": null
    196. }
    197. }
    198. }

    } ]

  1. <a name="iuNUF"></a>
  2. ## 5、从容器内拷贝文件到主机上
  3. - `**docker cp 容器ID:容器内路径 主机路径**`
  4. - 拷贝是一个手动过程,未来我们使用-v卷的技术,可以实现自动同步
  5. <a name="mCuiB"></a>
  6. # 五、小结
  7. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/2893488/1630204563384-b3dea05b-fe93-49f5-bc12-bac9fba31374.png#clientId=u209bddaf-5fae-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=529&id=u4eccca8c&margin=%5Bobject%20Object%5D&name=image.png&originHeight=713&originWidth=1028&originalType=binary&ratio=1&rotation=0&showTitle=false&size=368274&status=done&style=none&taskId=ub56abf63-b5a6-4a82-bb8d-82701d105ae&title=&width=762)
  8. <a name="HuTKl"></a>
  9. # 六、练习
  10. <a name="Le3f5"></a>
  11. ## 1、安装部署nginx
  12. ```shell
  13. [root@VM-0-15-centos ~]# docker ps
  14. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  15. [root@VM-0-15-centos ~]# docker search nginx
  16. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  17. nginx Official build of Nginx. 15385 [OK]
  18. jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 2061 [OK]
  19. richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 816 [OK]
  20. jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 237
  21. linuxserver/nginx An Nginx container, brought to you by LinuxS… 152
  22. tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 140 [OK]
  23. jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 135 [OK]
  24. alfg/nginx-rtmp NGINX, nginx-rtmp-module and FFmpeg from sou… 105 [OK]
  25. jasonrivers/nginx-rtmp Docker images to host RTMP streams using NGI… 92 [OK]
  26. nginxdemos/hello NGINX webserver that serves a simple page co… 71 [OK]
  27. privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al… 56 [OK]
  28. nginx/nginx-ingress NGINX and NGINX Plus Ingress Controllers fo… 55
  29. nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 46
  30. staticfloat/nginx-certbot Opinionated setup for automatic TLS certs lo… 24 [OK]
  31. nginx/nginx-prometheus-exporter NGINX Prometheus Exporter for NGINX and NGIN… 19
  32. schmunk42/nginx-redirect A very simple container to redirect HTTP tra… 19 [OK]
  33. nginxproxy/nginx-proxy Automated Nginx reverse proxy for docker con… 18
  34. centos/nginx-112-centos7 Platform for running nginx 1.12 or building … 15
  35. centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 13
  36. raulr/nginx-wordpress Nginx front-end for the official wordpress:f… 13 [OK]
  37. flashspys/nginx-static Super Lightweight Nginx Image 10 [OK]
  38. mailu/nginx Mailu nginx frontend 9 [OK]
  39. sophos/nginx-vts-exporter Simple server that scrapes Nginx vts stats a… 7 [OK]
  40. ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 2 [OK]
  41. wodby/nginx Generic nginx 1 [OK]
  42. [root@VM-0-15-centos ~]# docker pull nginx
  43. Using default tag: latest
  44. latest: Pulling from library/nginx
  45. e1acddbe380c: Already exists
  46. e21006f71c6f: Pull complete
  47. f3341cc17e58: Pull complete
  48. 2a53fa598ee2: Pull complete
  49. 12455f71a9b5: Pull complete
  50. b86f2ba62d17: Pull complete
  51. Digest: sha256:4d4d96ac750af48c6a551d757c1cbfc071692309b491b70b2b8976e102dd3fef
  52. Status: Downloaded newer image for nginx:latest
  53. docker.io/library/nginx:latest
  54. [root@VM-0-15-centos ~]# docker images
  55. REPOSITORY TAG IMAGE ID CREATED SIZE
  56. nginx latest dd34e67e3371 13 days ago 133MB
  57. mysql latest 5a4e492065c7 13 days ago 514MB
  58. hello-world latest d1165f221234 5 months ago 13.3kB
  59. centos latest 300e315adb2f 8 months ago 209MB
  60. 80x86/typecho latest 3437f7346b4c 2 years ago 53.9MB
  61. [root@VM-0-15-centos ~]# docker run -d --name nginx01 -p 3344:80 nginx
  62. d0850b4943fcd70f14dd895826012a77bdb219b992060fc31fea07d8f5536803
  63. [root@VM-0-15-centos ~]# docker ps
  64. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  65. d0850b4943fc nginx "/docker-entrypoint.…" 5 seconds ago Up 4 seconds 0.0.0.0:3344->80/tcp, :::3344->80/tcp nginx01
  66. [root@VM-0-15-centos ~]# curl localhost:3344
  67. <!DOCTYPE html>
  68. <html>
  69. <head>
  70. <title>Welcome to nginx!</title>
  71. <style>
  72. body {
  73. width: 35em;
  74. margin: 0 auto;
  75. font-family: Tahoma, Verdana, Arial, sans-serif;
  76. }
  77. </style>
  78. </head>
  79. <body>
  80. <h1>Welcome to nginx!</h1>
  81. <p>If you see this page, the nginx web server is successfully installed and
  82. working. Further configuration is required.</p>
  83. <p>For online documentation and support please refer to
  84. <a href="http://nginx.org/">nginx.org</a>.<br/>
  85. Commercial support is available at
  86. <a href="http://nginx.com/">nginx.com</a>.</p>
  87. <p><em>Thank you for using nginx.</em></p>
  88. </body>
  89. </html>

image.png
image.png

2、安装部署Tomcat

  1. #官方的使用
  2. docker run -it --rm tomcat:9.0
  3. #我们之前的启动都是后台,停止了容器之后,容器还是可以查到 而--rm一般用来测试,用完就删除容器
  4. #下载再运行
  5. [root@VM-0-15-centos ~]# docker pull tomcat
  6. [root@VM-0-15-centos ~]# docker run -d -p 3355:8080 --name tomcat01 tomcat
  7. 64dc6975f6e47c040b7c476f7a4aa8af43fff314cfc3bcc539da5524b7b1d2c5
  8. #测试访问发现找不到
  9. #进入容器
  10. [root@VM-0-15-centos ~]# docker exec -it tomcat01 /bin/bash
  11. root@64dc6975f6e4:/usr/local/tomcat# ls
  12. BUILDING.txt LICENSE README.md RUNNING.txt conf logs temp webapps.dist
  13. CONTRIBUTING.md NOTICE RELEASE-NOTES bin lib native-jni-lib webapps work
  14. root@64dc6975f6e4:/usr/local/tomcat# cd webapps
  15. root@64dc6975f6e4:/usr/local/tomcat/webapps# ls
  16. root@64dc6975f6e4:/usr/local/tomcat/webapps#
  17. #发现问题:1、Linux命令少了 2、没有webapps
  18. #阿里云镜像的原因。默认是最小的镜像,所有不必要的都剔除掉
  19. #保证最小的可运行环境!

image.png

  1. #ROOT在webapps.dist目录下
  2. root@64dc6975f6e4:/usr/local/tomcat# cd webapps.dist
  3. root@64dc6975f6e4:/usr/local/tomcat/webapps.dist# ls
  4. ROOT docs examples host-manager manager
  5. root@64dc6975f6e4:/usr/local/tomcat/webapps.dist# cd ..
  6. root@64dc6975f6e4:/usr/local/tomcat# cp -r webapps.dist/* webapps
  7. root@64dc6975f6e4:/usr/local/tomcat#
  8. #再次测试,成功

image.png