[关闭]
@yangwenbo 2023-06-27T19:01:35.000000Z 字数 43477 阅读 169

Docker

Docker基础(上)

1. Docker简介,功能特性与应用场景

1.1 Docker简介

  • Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。
  • 一个完整的Docker有以下几个部分组成:
    • Docker Client 客户端
    • Docker Daemon 守护进程
    • Docker Image 镜像
    • Docker Container 容器

1.2 Docker功能特性

  • 隔离环境(系统,网络,文件系统)与应用
  • 解决依赖与版本问题
  • 易于分发,开箱即用
  • 节点与容器快速扩容
  • 镜像制作简单便捷,管理方便

(1)隔离

  • 通过cgroup(隔离和跟踪资源的使用)&
  • namespace(组与组之间隔离)来实现轻量级的进程隔离
  • 对于容器中运行的进程来说,自己独占了一个系统
  • 容器间网络,文件及其他资源都互相隔离

(2)版本与依赖

  • 传统模式下,多个不同环境或版本的项目需要部署在不同机器上,部署与后期维护管理复杂繁琐。
  • 使用Docker,通过多个不同版本或者环境的镜像,可以同时运行在一台机器上互不干扰,部署与后期维护简单方便。

(3)分发与使用

  • 镜像可以通过导入,导出,上传到镜像仓库等多种方式进行分发
  • 在启动了Docker的系统上直接使用docker run即可启动镜像,无需特别配置。

(4)扩容

  • 容器扩容简单方便
  • 扩容节点只需安装并启动Docker即可

(5)镜像制作

  • 镜像的灵魂Dockerfile
  • 使用Dockerfile进行指令控制
  • 基于Linux命令,易于理解,快速上手
  • 易于定制与修改

1.3 Docker应用场景

  • Docker通常应用于如下场景:
    • web应用的自动化打包和发布;
    • 自动化测试和持续集成,发布;
    • 应用服务,如MySQL,Redis等,通过Docker实现快速部署;
    • k8s私有云
1.3.1 场景1:多版本多种类系统与软件

image.png-209.3kB

1.3.2 场景2: 环境

image.png-297.1kB

1.3.3 场景3: 分发

image.png-238.8kB

2. Docker的安装

2.1 安装环境

  1. #最小化安装Centos7.5
  2. [root@docker ~]# cat /etc/redhat-release
  3. CentOS Linux release 7.5.1804 (Core)
  4. [root@docker ~]# uname -r
  5. 3.10.0-862.14.4.el7.x86_64
  1. #关闭防火墙和selinux
  2. [root@docker ~]# systemctl stop firewalld
  3. [root@docker ~]# systemctl disable firewalld
  4. [root@docker ~]# setenforce 0
  5. setenforce: SELinux is disabled

2.2 版本选择

  • Docker
    • 17.03之后版本变为Docker CE
  • Docker CE
    • 社区版,Community Edition
  • Docker EE
    • 企业版,Enterprise Edition
    • 收费版本,强调安全性,提供一些高级特性及商业支持

2.3 Docker安装:标准版本

特别提示:
centos7.5在搭建本地yum仓库的时候只需要修改
CentOS-Media.repo 文件即可(不需要将其他文件仍子目录里)
确保虚拟机能正常上网

  1. [root@docker ~]# yum -y install docker
  2. [root@docker ~]# rpm -qa docker
  3. docker-1.13.1-75.git8633870.el7.centos.x86_64
  4. [root@docker ~]# systemctl start docker #启动docker进程
  5. [root@docker ~]# systemctl enable docker #加入docker开机启动
  6. Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
  7. [root@docker ~]# docker ps #查看封装在docker镜像中的正在运行的镜像进程
  8. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  9. [root@docker ~]# docker version
  10. Client:
  11. Version: 1.13.1
  12. API version: 1.26
  13. Package version: docker-1.13.1-75.git8633870.el7.centos.x86_64
  14. Go version: go1.9.4
  15. Git commit: 8633870/1.13.1
  16. Built: Fri Sep 28 19:45:08 2018
  17. OS/Arch: linux/amd64
  18. Server:
  19. Version: 1.13.1
  20. API version: 1.26 (minimum version 1.12)
  21. Package version: docker-1.13.1-75.git8633870.el7.centos.x86_64
  22. Go version: go1.9.4
  23. Git commit: 8633870/1.13.1
  24. Built: Fri Sep 28 19:45:08 2018
  25. OS/Arch: linux/amd64
  26. Experimental: false

2.4 Docker安装:CE社区版

首先清理掉yum安装的docker标准版
这里有两种方式

  1. #第一种清理docker方法
  2. [root@docker ~]# yum -y remove docker
  3. [root@docker ~]# rpm -qa docker
  4. [root@docker ~]#
  1. #第二种清理docker方法
  2. [root@docker ~]# yum history list #查看yum安装的历史列表
  3. 已加载插件:fastestmirror
  4. ID | 登录用户 | 日期和时间 | 操作 | 变更数
  5. -------------------------------------------------------------------------------
  6. 6 | root <root> | 2018-10-22 14:25 | Install | 1
  7. 5 | root <root> | 2018-10-22 14:22 | Erase | 1 EE
  8. 4 | root <root> | 2018-10-22 14:17 | Install | 27 EE
  9. 3 | root <root> | 2018-10-20 21:29 | I, U | 101
  10. 2 | root <root> | 2018-10-20 21:26 | Install | 52
  11. 1 | 系统 <空> | 2018-10-20 21:05 | Install | 415
  12. history list
  13. [root@docker ~]# yum history info 6
  14. 已加载插件:fastestmirror
  15. 事务 ID 6
  16. 起始时间 Mon Oct 22 14:25:15 2018
  17. 启动 RPM 数据库 495:1c3b8c6ad09605ca1b188673bb554b71f98fc698
  18. 结束时间 14:25:20 2018 5 秒)
  19. 结束 RPM 数据库 496:d141d348989de537000bed07bb8aa2abd39e68d2
  20. 用户 root <root>
  21. 返回码 成功
  22. 命令行 -y install docker
  23. 事务完成属主:
  24. 已安装 rpm-4.11.3-32.el7.x86_64 @anaconda
  25. 已安装 yum-3.4.3-158.el7.centos.noarch @anaconda
  26. 已安装 yum-plugin-fastestmirror-1.1.31-46.el7_5.noarch @updates
  27. 已变更的包:
  28. 安装 docker-2:1.13.1-75.git8633870.el7.centos.x86_64 @extras
  29. history info
  30. [root@docker ~]# yum -y history undo 6 #进行yum安装操作回退
  31. [root@docker ~]# rpm -qa docker
  32. [root@docker ~]#

特别提示: yum的回退安装很有用,接下来我们安装Docker的CE社区版

  1. #安装依赖包
  2. [root@docker ~]# yum -y install yum-utils device-mapper-persistent-data lvm2
  3. [root@docker ~]# rpm -qa yum-utils device-mapper-persistent-data lvm2
  4. device-mapper-persistent-data-0.7.3-3.el7.x86_64
  5. yum-utils-1.1.31-46.el7_5.noarch
  6. lvm2-2.02.177-4.el7.x86_64
  7. #添加docker的CE版本的yum源配置文件
  8. [root@docker ~]# curl https://download.docker.com/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker-ce.repo
  9. % Total % Received % Xferd Average Speed Time Time Time Current
  10. Dload Upload Total Spent Left Speed
  11. 100 2424 100 2424 0 0 282 0 0:00:08 0:00:08 --:--:-- 602
  12. [root@docker ~]# ll /etc/yum.repos.d/docker-ce.repo
  13. -rw-r--r-- 1 root root 2424 10 22 14:42 /etc/yum.repos.d/docker-ce.repo
  1. #安装CE版本的docker
  2. [root@docker ~]# yum -y install docker-ce
  3. [root@docker ~]# rpm -qa docker-ce
  4. docker-ce-18.06.1.ce-3.el7.x86_64
  5. [root@docker ~]# systemctl start docker #启动docker
  6. [root@docker ~]# systemctl enable docker #添加开机启动
  7. Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
  8. [root@docker ~]# docker version #查看docker版本
  9. Client:
  10. Version: 18.06.1-ce
  11. API version: 1.38
  12. Go version: go1.10.3
  13. Git commit: e68fc7a
  14. Built: Tue Aug 21 17:23:03 2018
  15. OS/Arch: linux/amd64
  16. Experimental: false
  17. Server:
  18. Engine:
  19. Version: 18.06.1-ce
  20. API version: 1.38 (minimum version 1.12)
  21. Go version: go1.10.3
  22. Git commit: e68fc7a
  23. Built: Tue Aug 21 17:25:29 2018
  24. OS/Arch: linux/amd64
  25. Experimental: false

3. Docker的操作命令

3.0 添加docker国内镜像源

咱们首先要更改一下docker的默认源镜像下载地址(默认是从国外下载,很慢),我们需要添加国内的源地址

  1. [root@docker ~]# vim /etc/docker/daemon.json
  2. [root@docker ~]# cat /etc/docker/daemon.json
  3. {
  4. "registry-mirrors":[ "https://registry.docker-cn.com" ]
  5. }
  6. [root@docker ~]# systemctl daemon-reload
  7. [root@docker ~]# systemctl restart docker

镜像源推荐:https://blog.csdn.net/qq_19309473/article/details/121305970

3.1 Docker命令:search

用于从docker的官方公有镜像仓库查找镜像

(1)查看Docker Hub上公开的centos镜像

  1. [root@docker ~]# docker search centos
  2. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  3. centos The official build of CentOS. 4834 [OK]
  4. #...以下省略无数行....
  5. 特别说明:
  6. Name:镜像的名字
  7. DESCRIPTION:描述
  8. STARS:星级(越高越好)
  9. OFFICIAL:是否是官方发布的
  10. AUTOMATED:是否自动化的

image.png-1515.9kB
(2)查找星级多于100的centos镜像

  1. [root@docker ~]# docker search centos -f stars=100
  2. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  3. centos The official build of CentOS. 4834 [OK]
  4. ansible/centos7-ansible Ansible on Centos7 118 [OK]
  5. 特别提示:
  6. /:符号用于分割作者名称和镜像名称
  7. ansible/centos7-ansibleansible是作者名称,centos7-ansible是镜像名称

(3)多条件查找--filter

  1. #查找官方发布的,星级大于100的centos镜像
  2. [root@docker ~]# docker search centos --filter is-official=true --filter stars=100
  3. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  4. centos The official build of CentOS. 4834 [OK]

3.2 Docker命令:pull

用于从Docker Hub上下载公有镜像

  1. #查找符合条件的hello-world镜像
  2. [root@docker ~]# docker search hello-world --filter is-official=true --filter stars=100
  3. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  4. hello-world Hello World! (an example of minimal Dockeriz 694 [OK]
  5. #下载目标hello-world镜像
  6. [root@docker ~]# docker pull hello-world
  7. Using default tag: latest
  8. latest: Pulling from library/hello-world
  9. d1725b59e92d: Pull complete
  10. Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
  11. Status: Downloaded newer image for hello-world:latest 下载成功latest是标记tag
  12. #下载目标centos:7镜像
  13. [root@docker 7]# docker pull centos:7
  14. 7: Pulling from library/centos
  15. aeb7866da422: Pull complete
  16. Digest: sha256:67dad89757a55bfdfabec8abd0e22f8c7c12a1856514726470228063ed86593b
  17. Status: Downloaded newer image for centos:7
  18. #网速不好的话,需要点时间耐心等待

3.3 Docker命令:images

  1. #用于本地镜像的查看
  2. [root@docker 7]# docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. centos 7 75835a67d134 12 days ago 200MB
  5. hello-world latest 4ab4c602aa5e 6 weeks ago 1.84kB
  6. 特别说明:
  7. REPOSITORY:镜像仓库(下边罗列的都是本地已有镜像名称)
  8. TAG:镜像的标记(为了区分同名镜像)
  9. IMAGES ID:镜像的ID
  10. CREATED:此镜像的创建时间
  11. SIZE:此镜像的大小

3.4 Docker命令:build

用于本地自定义镜像的构建,需要创建Dockerfile文件(具体写法后面会讲)

  1. #创建Dockerfile文件的存储目录
  2. [root@docker ~]# mkdir -p /root/dockerfile/lib/centos/7
  3. [root@docker ~]# cd /root/dockerfile/lib/centos/7
  4. #创建docker.sh脚本
  5. [root@docker 7]# vim docker.sh
  6. [root@docker 7]# cat docker.sh
  7. #!/bin/bash
  8. while true
  9. do
  10. echo "welcome"
  11. sleep 5
  12. done
  13. #创建Dockerfile配置文件,文件名称必须为Dockerfile,第一个字母必须大写
  14. [root@docker 7]# vim Dockerfile
  15. [root@docker 7]# cat Dockerfile
  16. FROM centos #从centos源镜像的基础上进行构建
  17. LABEL MAINTATNER="Mr.yang.com" #作者的名称
  18. RUN ln -sfv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #RUN:在镜像构建过程中运行命令
  19. ADD docker.sh /home/test/ #从本地系统中把docker.sh文件添加到构建中的镜像的/home/test/目录下
  20. RUN chmod +x /home/test/docker.sh #在镜像构建过程中运行命令
  21. CMD ["/home/test/docker.sh"] #构建镜像完成时,最后执行的命令
  1. #根据Dockfile配置文件构建一个自定义镜像
  2. [root@docker 7]# docker build -t Mr.yang/centos7:1 . #-t 指定镜像名称 :1 设定镜像的tag标记
  3. Sending build context to Docker daemon 3.072kB
  4. Step 1/6 : FROM centos
  5. latest: Pulling from library/centos
  6. Digest: sha256:67dad89757a55bfdfabec8abd0e22f8c7c12a1856514726470228063ed86593b
  7. Status: Downloaded newer image for centos:latest
  8. ---> 75835a67d134
  9. Step 2/6 : LABEL MAINTATNER="Mr.yang.com"
  10. ---> Running in fa269f986e54
  11. Removing intermediate container fa269f986e54
  12. ---> 66ad376d42cf
  13. Step 3/6 : RUN ln -sfv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  14. ---> Running in 0c91a7dd6db4
  15. '/etc/localtime' -> '/usr/share/zoneinfo/Asia/Shanghai'
  16. Removing intermediate container 0c91a7dd6db4
  17. ---> e1443698a3d2
  18. Step 4/6 : ADD docker.sh /home/test/
  19. ---> c46f0f1b3454
  20. Step 5/6 : RUN chmod +x /home/test/docker.sh
  21. ---> Running in e5ab6ec5afff
  22. Removing intermediate container e5ab6ec5afff
  23. ---> 5666c086dbf9
  24. Step 6/6 : CMD ["/home/test/docker.sh"]
  25. ---> Running in 152a935d86c9
  26. Removing intermediate container 152a935d86c9
  27. ---> f1c34c7f90da
  28. Successfully built f1c34c7f90da #构建成功
  29. Successfully tagged Mr.yang/centos7:1
  30. [root@docker 7]# docker images
  31. REPOSITORY TAG IMAGE ID CREATED SIZE
  32. Mr.yang/centos7 1 f1c34c7f90da 21 seconds ago 200MB
  33. centos 7 75835a67d134 12 days ago 200MB
  34. centos latest 75835a67d134 12 days ago 200MB
  35. hello-world latest 4ab4c602aa5e 6 weeks ago 1.84kB

3.5 Docker命令:run

  1. #运行一个本地镜像
  2. [root@docker 7]# docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. Mr.yang/centos7 1 f1c34c7f90da 5 minutes ago 200MB
  5. centos 7 75835a67d134 12 days ago 200MB
  6. centos latest 75835a67d134 12 days ago 200MB
  7. hello-world latest 4ab4c602aa5e 6 weeks ago 1.84kB
  8. [root@docker 7]# docker run -d -it f1c34c7f90da /bin/bash
  9. e23b31eb73b2cbf24470f34611f2bf6fad7080d0cbd816441a824911d99f27a0
  10. 特别提示:
  11. docker run:运行一个指定的images id
  12. -d:放在后台运行
  13. -i:可以进行命令交互
  14. -t:制作一个伪终端用于登陆
  15. f1c34c7f90da:镜像的ID,可以简写成f1c
  1. docker run -itd --privileged --name lnmp centos:7 /usr/sbin/init

3.6 Docker命令:ps

  1. #查看已经运行的镜像的进程
  2. [root@docker 7]# docker ps -a
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. e23b31eb73b2 f1c34c7f90da "/bin/bash" About a minute ago Up About a minute xenodochial_keller
  5. 特别提示:
  6. STATUS:进程的状态,UP表示正在运行中,EXIT表示已经退出了。

3.7 Docker命令:attach

  1. #从本地系统中切入到某个STATUS状态是UP的镜像进程里
  2. [root@docker 7]# docker ps -a
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. e23b31eb73b2 f1c34c7f90da "/bin/bash" 8 minutes ago Up 8 minutes xenodochial_keller
  5. [root@docker 7]# docker attach e23b31eb73b2 #切入到容器号e23b31eb73b2的镜像进程里
  6. [root@e23b31eb73b2 /]# ls #已经进入容器里了
  7. anaconda-post.log dev home lib64 mnt proc run srv tmp var
  8. bin etc lib media opt root sbin sys usr
  9. [root@e23b31eb73b2 /]# exit #退出容器
  10. exit
  11. [root@docker 7]# docker ps -a #容器的进程的STATUS已经处于EXIT状态(之前是后台运行的,切入进去后执行exit就等于手动退出了)
  12. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  13. e23b31eb73b2 f1c34c7f90da "/bin/bash" 9 minutes ago Exited (0) 22 seconds ago xenodochial_keller

3.8 Docker命令:stop

  1. #用于停止一个正在运行着的容器进程
  2. [root@docker 7]# docker ps -a
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. e23b31eb73b2 f1c34c7f90da "/bin/bash" 13 minutes ago Exited (0) 4 minutes ago xenodochial_keller
  5. #再次在后台启动一个镜像
  6. [root@docker 7]# docker run -d -it f1c34c7f90da /bin/bash
  7. 9be1dd5a15e465183fe9153569957ead7049c3c23dc92d64a0e90e8ccd610532
  8. [root@docker 7]# docker ps -a #增加了一个容器进程
  9. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  10. 9be1dd5a15e4 f1c34c7f90da "/bin/bash" 22 seconds ago Up 6 seconds hardcore_babbage
  11. e23b31eb73b2 f1c34c7f90da "/bin/bash" 15 minutes ago Exited (0) 6 minutes ago xenodochial_keller
  12. #停止一个运行着的容器进程
  13. [root@docker 7]# docker stop 9be1dd5a15e4
  14. 9be1dd5a15e4
  15. [root@docker 7]# docker ps -a
  16. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  17. 9be1dd5a15e4 f1c34c7f90da "/bin/bash" About a minute ago Exited (137) 7 seconds ago hardcore_babbage
  18. e23b31eb73b2 f1c34c7f90da "/bin/bash" 16 minutes ago Exited (0) 7 minutes ago xenodochial_keller

3.9 Docker命令:start

  1. #用于启动一个已经停止了的容器进程
  2. [root@docker 7]# docker ps -a
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. 9be1dd5a15e4 f1c34c7f90da "/bin/bash" 11 minutes ago Exited (137) 9 minutes ago hardcore_babbage
  5. e23b31eb73b2 f1c34c7f90da "/bin/bash" 26 minutes ago Exited (0) 17 minutes ago xenodochial_keller
  6. [root@docker 7]# docker start 9be1dd5a15e4
  7. 9be1dd5a15e4
  8. [root@docker 7]# docker ps -a
  9. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  10. 9be1dd5a15e4 f1c34c7f90da "/bin/bash" 11 minutes ago Up 5 seconds hardcore_babbage
  11. e23b31eb73b2 f1c34c7f90da "/bin/bash" 26 minutes ago Exited (0) 17 minutes ago xenodochial_keller

3.10 Docker命令:rm

  1. #用于删除一个已经停止了的容器进程
  2. [root@docker 7]# docker ps -a
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. 9be1dd5a15e4 f1c34c7f90da "/bin/bash" 12 minutes ago Up 48 seconds hardcore_babbage
  5. e23b31eb73b2 f1c34c7f90da "/bin/bash" 27 minutes ago Exited (0) 18 minutes ago xenodochial_keller
  6. [root@docker 7]# docker rm e23b31eb73b2
  7. e23b31eb73b2
  8. [root@docker 7]# docker ps -a
  9. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  10. 9be1dd5a15e4 f1c34c7f90da "/bin/bash" 12 minutes ago Up About a minute hardcore_babbage
  11. [root@docker 7]# docker rm 9be1dd5a15e4 #注意运行中的容器进程需要先stop,才能删除
  12. Error response from daemon: You cannot remove a running container 9be1dd5a15e465183fe9153569957ead7049c3c23dc92d64a0e90e8ccd610532. Stop the container before attempting removal or force remove

3.11 Docker命令:rmi

  1. #用于删除一个未用作容器启动的本地镜像
  2. [root@docker 7]# docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. Mr.yang/centos7 1 f1c34c7f90da 35 minutes ago 200MB
  5. centos 7 75835a67d134 12 days ago 200MB
  6. centos latest 75835a67d134 12 days ago 200MB
  7. hello-world latest 4ab4c602aa5e 6 weeks ago 1.84kB
  8. [root@docker 7]# docker rmi -f 4ab4c602aa5e #-f 强制删除(即便被占用)
  9. Untagged: hello-world:latest
  10. Untagged: hello-world@sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
  11. Deleted: sha256:4ab4c602aa5eed5528a6620ff18a1dc4faef0e1ab3a5eddeddb410714478c67f
  12. Deleted: sha256:428c97da766c4c13b19088a471de6b622b038f3ae8efa10ec5a37d6d31a2df0b
  13. [root@docker 7]# docker images
  14. REPOSITORY TAG IMAGE ID CREATED SIZE
  15. Mr.yang/centos7 1 f1c34c7f90da 38 minutes ago 200MB
  16. centos 7 75835a67d134 12 days ago 200MB
  17. centos latest 75835a67d134 12 days ago 200MB
  1. [root@docker 7]# docker rmi f1c34c7f90da #但要注意,被用作容器启动的镜像是不能删除的(需先rm删除容器进程)
  2. Error response from daemon: conflict: unable to delete f1c34c7f90da (cannot be forced) - image is being used by running container 9be1dd5a15e4
  3. [root@docker 7]# docker rmi -f f1c34c7f90da #强行删除被容器进程占用的镜像也是不行的
  4. Error response from daemon: conflict: unable to delete f1c34c7f90da (cannot be forced) - image is being used by running container 9be1dd5a15e4
  5. [root@docker 7]# docker ps -a #查看容器进程,被占用中
  6. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  7. 9be1dd5a15e4 f1c34c7f90da "/bin/bash" 19 minutes ago Up 8 minutes hardcore_babbage

3.12 Docker命令:commit

  1. #将一个更改过的容器进程的容器状态保存为一个新的镜像
  2. [root@docker ~]# docker ps -a #查看启动的容器进程
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. 9be1dd5a15e4 f1c34c7f90da "/bin/bash" 19 hours ago Up 8 seconds hardcore_babbage
  5. [root@docker ~]# docker attach 9be1dd5a15e4 #切入容器进程
  6. [root@9be1dd5a15e4 /]# ls
  7. anaconda-post.log dev home lib64 mnt proc run srv tmp var
  8. bin etc lib media opt root
  9. [root@9be1dd5a15e4 /]# mkdir yunjisuan #在容器进程里创建yunjisuan目录
  10. [root@9be1dd5a15e4 /]# ls
  11. anaconda-post.log dev home lib64 mnt proc run srv tmp var
  12. bin etc lib media opt root sbin sys usr yunjisuan
  13. [root@9be1dd5a15e4 /]# exit #退出容器进程
  14. exit
  15. [root@docker ~]# docker ps -a
  16. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  17. 9be1dd5a15e4 f1c34c7f90da "/bin/bash" 19 hours ago Exited (0) 5 seconds ago hardcore_babbage
  18. [root@docker ~]# docker commit 9be1dd5a15e4 Mr.yang/centos:2 #将更改后的容器进程保存为一个新的镜像
  19. sha256:5d878e838b0e7d461ea6d1182f77377d6e32ea2f6ba0a8698ccce6633ca8b57b
  20. [root@docker ~]# docker images
  21. REPOSITORY TAG IMAGE ID CREATED SIZE
  22. Mr.yang/centos 2 5d878e838b0e 12 seconds ago 200MB #保存成功
  23. Mr.yang/centos7 1 f1c34c7f90da 20 hours ago 200MB
  24. centos 7 75835a67d134 13 days ago 200MB
  25. centos latest 75835a67d134 13 days ago 200MB
  1. #启动新保存的镜像
  2. [root@docker ~]# docker run -d -it 5d878e838b0e /bin/bash
  3. c7cd70a9b8968b171596c4b571978aca62b30eba76028002742eca98c594e7e3
  4. [root@docker ~]# docker ps -a #查看新镜像的容器进程
  5. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  6. c7cd70a9b896 5d878e838b0e "/bin/bash" 7 seconds ago Up 6 seconds admiring_mirzakhani
  7. 9be1dd5a15e4 f1c34c7f90da "/bin/bash" 19 hours ago Exited (0) 5 minutes ago hardcore_babbage
  8. [root@docker ~]# docker attach c7cd70a9b896 #切入新镜像的容器进程
  9. [root@c7cd70a9b896 /]# ls -d yunjisuan #我们发现之前创建的目录仍旧存在
  10. yunjisuan

3.13 Docker命令:exec

  1. #用于从本地操作系统上直接向容器进程发布执行命令并返回结果
  2. [root@docker ~]# docker ps -a
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. c7cd70a9b896 5d878e838b0e "/bin/bash" About an hour ago Up 8 seconds admiring_mirzakhani
  5. 9be1dd5a15e4 f1c34c7f90da "/bin/bash" 20 hours ago Exited (0) About an hour ago hardcore_babbage
  6. [root@docker ~]# docker exec c7cd70a9b896 ls /tmp/ #查看容器进程里的/tmp目录下所有内容
  7. ks-script-7RxiSx
  8. yum.log
  9. [root@docker ~]# docker exec c7cd70a9b896 ls -d /yunjisuan #查看容器进程里/yunjisuan目录
  10. /yunjisuan

3.14 Docker命令:cp

  1. #用于在容器进程和本地系统之间复制文件
  2. [root@docker ~]# docker ps -a
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. c7cd70a9b896 5d878e838b0e "/bin/bash" About an hour ago Up 2 minutes admiring_mirzakhani
  5. 9be1dd5a15e4 f1c34c7f90da "/bin/bash" 20 hours ago Exited (0) About an hour ago hardcore_babbage
  6. [root@docker ~]# docker exec c7cd70a9b896 ls /tmp/ #向容器进程发布命令
  7. ks-script-7RxiSx
  8. yum.log
  9. [root@docker ~]# docker cp c7cd70a9b896:/tmp/yum.log . #将指定容器进程的/tmp/yum.log复制到当前目录下
  10. [root@docker ~]# ls
  11. anaconda-ks.cfg dockerfile yum.log #已经复制过来了
  12. [root@docker ~]# docker cp anaconda-ks.cfg c7cd70a9b896:/tmp/ #将本地文件复制到容器进程里
  13. [root@docker ~]# docker exec c7cd70a9b896 ls /tmp/
  14. anaconda-ks.cfg #复制成功
  15. ks-script-7RxiSx
  16. yum.log

3.15 Docker命令:create

  1. #用于创建一个容器进程,但是并不启动它
  2. [root@docker ~]# docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. Mr.yang/centos 2 5d878e838b0e About an hour ago 200MB
  5. Mr.yang/centos7 1 f1c34c7f90da 21 hours ago 200MB
  6. centos 7 75835a67d134 13 days ago 200MB
  7. centos latest 75835a67d134 13 days ago 200MB
  8. [root@docker ~]# docker create -it 5d878e838b0e #创建一个镜像的容器进程,但不直接启动
  9. 29e483bab044c79e448280393f73841a6205c41beba81016cebf9b2987831b73
  10. [root@docker ~]# docker ps -a
  11. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  12. 29e483bab044 5d878e838b0e "/bin/bash" 8 seconds ago Created elegant_boyd #创建状态并未启动
  13. c7cd70a9b896 5d878e838b0e "/bin/bash" About an hour ago Up 7 minutes admiring_mirzakhani
  14. 9be1dd5a15e4 f1c34c7f90da "/bin/bash" 20 hours ago Exited (0) About an hour ago hardcore_babbage
  15. [root@docker ~]# docker start 29e483bab044 #启动容器进程
  16. 29e483bab044
  17. [root@docker ~]# docker ps -a
  18. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  19. 29e483bab044 5d878e838b0e "/bin/bash" 41 seconds ago Up 3 seconds elegant_boyd
  20. c7cd70a9b896 5d878e838b0e "/bin/bash" About an hour ago Up 7 minutes admiring_mirzakhani
  21. 9be1dd5a15e4 f1c34c7f90da "/bin/bash" 20 hours ago Exited (0) About an hour ago hardcore_babbage

3.16 Docker命令:diff

  1. #查看容器进程与源镜像做对比,发生了改变的文件或文件夹
  2. [root@docker ~]# docker ps -a
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. 29e483bab044 5d878e838b0e "/bin/bash" 2 minutes ago Up 2 minutes elegant_boyd
  5. c7cd70a9b896 5d878e838b0e "/bin/bash" About an hour ago Up 9 minutes admiring_mirzakhani
  6. 9be1dd5a15e4 f1c34c7f90da "/bin/bash" 20 hours ago Exited (0) About an hour ago hardcore_babbage
  7. [root@docker ~]# docker images
  8. REPOSITORY TAG IMAGE ID CREATED SIZE
  9. Mr.yang/centos 2 5d878e838b0e About an hour ago 200MB
  10. Mr.yang/centos7 1 f1c34c7f90da 21 hours ago 200MB
  11. centos 7 75835a67d134 13 days ago 200MB
  12. centos latest 75835a67d134 13 days ago 200MB
  13. [root@docker ~]# docker run -d -it 5d878e838b0e #启动一个镜像的容器进程
  14. df3f9a0f26f5f693fc75567c50a1425dc621dc4e60bcc4f5b362d5235a23c8a4
  15. [root@docker ~]# docker ps -a
  16. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  17. df3f9a0f26f5 5d878e838b0e "/bin/bash" 14 seconds ago Up 13 seconds pedantic_lamarr
  18. 29e483bab044 5d878e838b0e "/bin/bash" 3 minutes ago Up 2 minutes elegant_boyd
  19. c7cd70a9b896 5d878e838b0e "/bin/bash" About an hour ago Up 10 minutes admiring_mirzakhani
  20. 9be1dd5a15e4 f1c34c7f90da "/bin/bash" 20 hours ago Exited (0) About an hour ago hardcore_babbage
  1. [root@docker ~]# docker attach df3f9a0f26f5 #切入容器进程
  2. [root@df3f9a0f26f5 /]# cd yunjisuan/
  3. [root@df3f9a0f26f5 yunjisuan]# pwd
  4. /yunjisuan
  5. [root@df3f9a0f26f5 yunjisuan]# touch {1..10} #在容器进程中创建文件
  6. [root@df3f9a0f26f5 yunjisuan]# ls
  7. 1 10 2 3 4 5 6 7 8 9
  8. [root@df3f9a0f26f5 yunjisuan]# exit #退出容器进程
  9. exit
  10. [root@docker ~]# docker diff df3f9a0f26f5 #查看容器进程的变化
  11. C /root
  12. C /root/.bash_history
  13. C /yunjisuan
  14. A /yunjisuan/6
  15. A /yunjisuan/9
  16. A /yunjisuan/1
  17. A /yunjisuan/3
  18. A /yunjisuan/4
  19. A /yunjisuan/5
  20. A /yunjisuan/10
  21. A /yunjisuan/2
  22. A /yunjisuan/7
  23. A /yunjisuan/8

3.17 Docker命令:events

  1. #时时监测容器的变化情况
  2. [root@docker ~]# docker events
  3. 2018-10-23T13:39:21.689536505+08:00 container export df3f9a0f26f5f693fc75567c50a1425dc621dc4e60bcc4f5b362d5235a23c8a4 (MAINTATNER=Mr.yang.com, image=5d878e838b0e, name=pedantic_lamarr, org.label-schema.build-date=20181006, org.label-schema.license=GPLv2, org.label-schema.name=CentOS Base Image, org.label-schema.schema-version=1.0, org.label-schema.vendor=CentOS)

前台时时监控容器的变化若要检测,需要另外再起一个窗口进行操作

3.18 Docker命令:export

  1. #将容器进程的文件系统导出到本地
  2. [root@docker ~]# docker ps -a
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. df3f9a0f26f5 5d878e838b0e "/bin/bash" 14 minutes ago Exited (0) 4 minutes ago pedantic_lamarr
  5. 29e483bab044 5d878e838b0e "/bin/bash" 17 minutes ago Up 16 minutes elegant_boyd
  6. c7cd70a9b896 5d878e838b0e "/bin/bash" About an hour ago Exited (0) 4 minutes ago admiring_mirzakhani
  7. 9be1dd5a15e4 f1c34c7f90da "/bin/bash" 21 hours ago Exited (0) About an hour ago hardcore_babbage
  8. [root@docker ~]# docker export df3f9a0f26f5 > test.tar #将容器进程导出成一个tar包
  9. [root@docker ~]# ls
  10. anaconda-ks.cfg dockerfile test.tar yum.log

3.19 Docker命令:import

  1. #用于将export导出的文件系统创建为一个镜像
  2. [root@docker ~]# ls
  3. anaconda-ks.cfg dockerfile test.tar yum.log
  4. [root@docker ~]# docker import test.tar Mr.yang/centos:3
  5. #将导出的tar包文件系统生成一个新的镜像
  6. sha256:b65d73ace4a227ff7cc908d3dc16efbdb36b266dc5c72c943ebf1dc9c643b5e6
  7. [root@docker ~]# docker images
  8. REPOSITORY TAG IMAGE ID CREATED SIZE
  9. Mr.yang/centos 3 b65d73ace4a2 About a minute ago 200MB
  10. Mr.yang/centos 2 5d878e838b0e About an hour ago 200MB
  11. Mr.yang/centos7 1 f1c34c7f90da 21 hours ago 200MB
  12. centos 7 75835a67d134 13 days ago 200MB
  13. centos latest 75835a67d134 13 days ago

3.20 Docker命令:history

  1. #用于查看一个镜像的历史修改纪录
  2. [root@docker ~]# docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. Mr.yang/centos 3 b65d73ace4a2 3 minutes ago 200MB
  5. Mr.yang/centos 2 5d878e838b0e About an hour ago 200MB
  6. Mr.yang/centos7 1 f1c34c7f90da 21 hours ago 200MB
  7. centos 7 75835a67d134 13 days ago 200MB
  8. centos latest 75835a67d134 13 days ago 200MB
  9. [root@docker ~]# docker history b65d73ace4a2
  10. IMAGE CREATED CREATED BY SIZE COMMENT
  11. b65d73ace4a2 4 minutes ago 200MB Imported from -
  12. [root@docker ~]# docker history 5d878e838b0e
  13. IMAGE CREATED CREATED BY SIZE COMMENT
  14. 5d878e838b0e About an hour ago /bin/bash 27B
  15. f1c34c7f90da 21 hours ago /bin/sh -c #(nop) CMD ["/home/test/docker.s… 0B
  16. 5666c086dbf9 21 hours ago /bin/sh -c chmod +x /home/test/docker.sh 63B
  17. c46f0f1b3454 21 hours ago /bin/sh -c #(nop) ADD file:5bd1c2d39928ac84c… 63B
  18. e1443698a3d2 21 hours ago /bin/sh -c ln -sfv /usr/share/zoneinfo/Asia/… 33B
  19. 66ad376d42cf 21 hours ago /bin/sh -c #(nop) LABEL MAINTATNER=Mr.yang.… 0B
  20. 75835a67d134 13 days ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
  21. <missing> 13 days ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
  22. <missing> 13 days ago /bin/sh -c #(nop) ADD file:fbe9badfd2790f074… 200MB

3.21 Docker命令:info

  1. #用于查看当前操作系统的docker运行信息
  2. [root@docker ~]# docker info
  3. Containers: 4 #容器进程4个
  4. Running: 1 #正在运行状态的容器1个
  5. Paused: 0
  6. Stopped: 3
  7. Images: 8 #一共有8个镜像
  8. Server Version: 18.06.1-ce
  9. Storage Driver: overlay2
  10. Backing Filesystem: xfs
  11. Supports d_type: true
  12. Native Overlay Diff: true
  13. Logging Driver: json-file
  14. Cgroup Driver: cgroupfs
  15. Plugins:
  16. Volume: local
  17. Network: bridge host macvlan null overlay
  18. Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
  19. Swarm: inactive
  20. Runtimes: runc
  21. Default Runtime: runc
  22. Init Binary: docker-init
  23. containerd version: 468a545b9edcd5932818eb9de8e72413e616e86e
  24. runc version: 69663f0bd4b60df09991c08812a60108003fa340
  25. init version: fec3683
  26. Security Options:
  27. seccomp
  28. Profile: default
  29. Kernel Version: 3.10.0-862.14.4.el7.x86_64
  30. Operating System: CentOS Linux 7 (Core)
  31. OSType: linux
  32. Architecture: x86_64
  33. CPUs: 1
  34. Total Memory: 974.5MiB
  35. Name: docker
  36. ID: IJPB:LRPV:KIZL:KV47:SAFH:4XGP:V2AQ:OHLP:GGGS:4P62:2JNW:53I4
  37. Docker Root Dir: /var/lib/docker
  38. Debug Mode (client): false
  39. Debug Mode (server): false
  40. Registry: https://index.docker.io/v1/
  41. Labels:
  42. Experimental: false
  43. Insecure Registries:
  44. 127.0.0.0/8
  45. Registry Mirrors:
  46. https://registry.docker-cn.com/
  47. Live Restore Enabled: false

3.22 Docker命令:inspect

  1. #查看某个镜像的详细信息
  2. [root@docker ~]# docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. Mr.yang/centos 3 b65d73ace4a2 7 minutes ago 200MB
  5. Mr.yang/centos 2 5d878e838b0e About an hour ago 200MB
  6. Mr.yang/centos7 1 f1c34c7f90da 21 hours ago 200MB
  7. centos 7 75835a67d134 13 days ago 200MB
  8. centos latest 75835a67d134 13 days ago 200MB
  9. [root@docker ~]# docker inspect b65d73ace4a2
  10. [
  11. {
  12. "Id": "sha256:b65d73ace4a227ff7cc908d3dc16efbdb36b266dc5c72c943ebf1dc9c643b5e6",
  13. "RepoTags": [
  14. "Mr.yang/centos:3"
  15. ],
  16. "RepoDigests": [],
  17. "Parent": "",
  18. "Comment": "Imported from -",
  19. "Created": "2018-10-23T05:42:00.363147266Z",
  20. "Container": "",
  21. "ContainerConfig": {
  22. "Hostname": "",
  23. "Domainname": "",
  24. "User": "",
  25. "AttachStdin": false,
  26. "AttachStdout": false,
  27. "AttachStderr": false,
  28. "Tty": false,
  29. "OpenStdin": false,
  30. "StdinOnce": false,
  31. "Env": null,
  32. "Cmd": null,
  33. "Image": "",
  34. "Volumes": null,
  35. "WorkingDir": "",
  36. "Entrypoint": null,
  37. "OnBuild": null,
  38. "Labels": null
  39. },
  40. "DockerVersion": "18.06.1-ce",
  41. "Author": "",
  42. "Config": {
  43. "Hostname": "",
  44. "Domainname": "",
  45. "User": "",
  46. "AttachStdin": false,
  47. "AttachStdout": false,
  48. "AttachStderr": false,
  49. "Tty": false,
  50. "OpenStdin": false,
  51. "StdinOnce": false,
  52. "Env": null,
  53. "Cmd": null,
  54. "Image": "",
  55. "Volumes": null,
  56. "WorkingDir": "",
  57. "Entrypoint": null,
  58. "OnBuild": null,
  59. "Labels": null
  60. },
  61. "Architecture": "amd64",
  62. "Os": "linux",
  63. "Size": 200375237,
  64. "VirtualSize": 200375237,
  65. "GraphDriver": {
  66. "Data": {
  67. "MergedDir": "/var/lib/docker/overlay2/d7bde3ef8531dfb1e20c8f8d3b48647888f7e6813d89879ec58d0ccc249a1124/merged",
  68. "UpperDir": "/var/lib/docker/overlay2/d7bde3ef8531dfb1e20c8f8d3b48647888f7e6813d89879ec58d0ccc249a1124/diff",
  69. "WorkDir": "/var/lib/docker/overlay2/d7bde3ef8531dfb1e20c8f8d3b48647888f7e6813d89879ec58d0ccc249a1124/work"
  70. },
  71. "Name": "overlay2"
  72. },
  73. "RootFS": {
  74. "Type": "layers",
  75. "Layers": [
  76. "sha256:2da447cadf068676e408e1ac84c720a1c6d892ae72fbcf86ba00bed0ac29790a"
  77. ]
  78. },
  79. "Metadata": {
  80. "LastTagTime": "2018-10-23T13:42:00.367989964+08:00"
  81. }
  82. }
  83. ]
  1. [root@docker ~]# docker inspect --format='{{.NetworkSettings.IPAddress}}' b65d73ace4a2

3.23 Docker命令:kill

  1. #强行停止一个或多个正在运行状态的容器进程
  2. [root@docker ~]# docker ps -a
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. df3f9a0f26f5 5d878e838b0e "/bin/bash" 28 minutes ago Up About a minute pedantic_lamarr
  5. 29e483bab044 5d878e838b0e "/bin/bash" 32 minutes ago Up 31 minutes elegant_boyd
  6. [root@docker ~]# docker kill df3f9a0f26f5
  7. df3f9a0f26f5
  8. [root@docker ~]# docker ps -a
  9. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  10. df3f9a0f26f5 5d878e838b0e "/bin/bash" 28 minutes ago Exited (137) 2 seconds ago pedantic_lamarr
  11. 29e483bab044 5d878e838b0e "/bin/bash" 32 minutes ago Up 31 minutes elegant_boyd

3.24 Docker命令:save

用于将一个镜像的文件系统导出到本地(export导出的是容器)

  1. #重新build一个镜像
  2. [root@docker ~]# cd dockerfile/lib/centos/7/
  3. [root@docker 7]# docker build -t test:1 .
  4. Sending build context to Docker daemon 3.072kB
  5. Step 1/6 : FROM centos
  6. ---> 75835a67d134
  7. Step 2/6 : LABEL MAINTATNER="Mr.yang.com"
  8. ---> Using cache
  9. ---> 66ad376d42cf
  10. Step 3/6 : RUN ln -sfv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  11. ---> Using cache
  12. ---> e1443698a3d2
  13. Step 4/6 : ADD docker.sh /home/test/
  14. ---> Using cache
  15. ---> c46f0f1b3454
  16. Step 5/6 : RUN chmod +x /home/test/docker.sh
  17. ---> Using cache
  18. ---> 5666c086dbf9
  19. Step 6/6 : CMD ["/home/test/docker.sh"]
  20. ---> Using cache
  21. ---> f1c34c7f90da
  22. Successfully built f1c34c7f90da
  23. Successfully tagged test:1
  1. [root@docker 7]# docker images #save导出test:1这个镜像(也可以用id号)
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. Mr.yang/centos 3 b65d73ace4a2 17 minutes ago 200MB
  4. Mr.yang/centos 2 5d878e838b0e 2 hours ago 200MB
  5. Mr.yang/centos7 1 f1c34c7f90da 21 hours ago 200MB
  6. test 1 f1c34c7f90da 21 hours ago 200MB
  7. centos 7 75835a67d134 13 days ago 200MB
  8. centos latest 75835a67d134 13 days ago 200MB
  9. [root@docker 7]# docker save test:1 > test.tar
  10. [root@docker 7]# ls
  11. Dockerfile docker.sh test.tar

3.25 Docker命令:load

  1. #用于将save导出到本地的tar包,重新加载为镜像(和源镜像的名字标识完全一样)
  2. [root@docker 7]# docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. Mr.yang/centos 3 b65d73ace4a2 21 minutes ago 200MB
  5. Mr.yang/centos 2 5d878e838b0e 2 hours ago 200MB
  6. Mr.yang/centos7 1 f1c34c7f90da 21 hours ago 200MB
  7. test 1 f1c34c7f90da 21 hours ago 200MB
  8. centos 7 75835a67d134 13 days ago 200MB
  9. centos latest 75835a67d134 13 days ago 200MB
  10. [root@docker 7]# docker rmi test:1
  11. Untagged: test:1
  12. [root@docker 7]# docker images
  13. REPOSITORY TAG IMAGE ID CREATED SIZE
  14. Mr.yang/centos 3 b65d73ace4a2 22 minutes ago 200MB
  15. Mr.yang/centos 2 5d878e838b0e 2 hours ago 200MB
  16. Mr.yang/centos7 1 f1c34c7f90da 21 hours ago 200MB
  17. centos 7 75835a67d134 13 days ago 200MB
  18. centos latest 75835a67d134 13 days ago 200MB
  1. [root@docker 7]# docker load < test.tar #将之前test:1这个镜像的save备份导入系统
  2. Loaded image: test:1
  3. [root@docker 7]# docker images #和原来的镜像
  4. REPOSITORY TAG IMAGE ID CREATED SIZE
  5. Mr.yang/centos 3 b65d73ace4a2 23 minutes ago 200MB
  6. Mr.yang/centos 2 5d878e838b0e 2 hours ago 200MB
  7. Mr.yang/centos7 1 f1c34c7f90da 21 hours ago 200MB
  8. test 1 f1c34c7f90da 21 hours ago 200MB
  9. centos 7 75835a67d134 13 days ago 200MB
  10. centos latest 75835a67d134 13 days ago 200MB

3.26 Docker命令:logs

  1. #用于输出一个容器进程内的操作日志
  2. [root@docker 7]# docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. Mr.yang/centos7 1 f1c34c7f90da 22 hours ago 200MB
  5. test 1 f1c34c7f90da 22 hours ago 200MB
  6. centos 7 75835a67d134 13 days ago 200MB
  7. centos latest 75835a67d134 13 days ago 200MB
  8. [root@docker 7]# docker ps -a
  9. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  10. [root@docker 7]# docker run -d -it test:1
  11. 1bf64c93e7a102f529abde72c142cd902ccc0f42067ee9fad075c905d5c9dfe4
  12. [root@docker 7]# docker ps -a
  13. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  14. 1bf64c93e7a1 test:1 "/home/test/docker.sh" 4 seconds ago Up 3 seconds elegant_blackwell
  15. [root@docker 7]# docker logs 1bf64c93e7a1
  16. welcome
  17. welcome
  18. welcome
  19. welcome
  20. welcome
  21. welcome
  22. [root@docker 7]# docker logs --tail 2 1bf64c93e7a1 #只显示容器日志的后2行
  23. welcome
  24. welcome

3.27 Docker命令:pause && unpause

  1. #用于将一个或多个容器的进程暂停和恢复
  2. [root@docker 7]# docker ps -a
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. 1bf64c93e7a1 test:1 "/home/test/docker.sh" 2 minutes ago Up 2 minutes elegant_blackwell
  5. [root@docker 7]# docker pause 1bf64c93e7a1 #暂停容器进程
  6. 1bf64c93e7a1
  7. [root@docker 7]# docker ps -a
  8. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  9. 1bf64c93e7a1 test:1 "/home/test/docker.sh" 2 minutes ago Up 2 minutes (Paused) elegant_blackwell
  10. [root@docker 7]# docker unpause 1bf64c93e7a1 #恢复容器进程
  11. 1bf64c93e7a1
  12. [root@docker 7]# docker ps -a
  13. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  14. 1bf64c93e7a1 test:1 "/home/test/docker.sh" 3 minutes ago Up 3 minutes elegant_blackwell

3.28 Docker命令:port

  1. #用于列出一个容器的端口映射及协议
  2. [root@docker 7]# docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. test 1 f1c34c7f90da 22 hours ago 200MB
  5. Mr.yang/centos7 1 f1c34c7f90da 22 hours ago 200MB
  6. centos 7 75835a67d134 13 days ago 200MB
  7. centos latest 75835a67d134 13 days ago 200MB
  8. [root@docker 7]# docker run -d -it -p 2222:22 test:1 #启动一个镜像的容器进程 -p 指定本地2222端口映射到容器的22端口
  9. a4bf8c27421dea3c5b0515bcbf088e0e549bd0d391c73885d793dfb8c45cb1f2
  10. [root@docker 7]# docker ps -a
  11. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  12. a4bf8c27421d test:1 "/home/test/docker.sh" 5 seconds ago Up 3 seconds 0.0.0.0:2222->22/tcp silly_bohr
  13. 1bf64c93e7a1 test:1 "/home/test/docker.sh" 5 minutes ago Up 5 minutes elegant_blackwell
  14. [root@docker 7]# docker port a4bf8c27421d #查看容器进程的端口映射及协议
  15. 22/tcp -> 0.0.0.0:2222

3.29 Docker命令:rename

  1. #给容器进程重命名
  2. [root@docker 7]# docker ps -a
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. a4bf8c27421d test:1 "/home/test/docker.sh" 2 minutes ago Up 2 minutes 0.0.0.0:2222->22/tcp silly_bohr
  5. 1bf64c93e7a1 test:1 "/home/test/docker.sh" 7 minutes ago Up 7 minutes elegant_blackwell
  6. [root@docker 7]# docker rename silly_bohr Mr.yang
  7. [root@docker 7]# docker ps -a
  8. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  9. a4bf8c27421d test:1 "/home/test/docker.sh" 2 minutes ago Up 2 minutes 0.0.0.0:2222->22/tcp Mr.yang
  10. 1bf64c93e7a1 test:1 "/home/test/docker.sh" 7 minutes ago Up 7 minutes elegant_blackwell

3.30 Docker命令:restart

  1. #重启一个容器进程
  2. [root@docker 7]# docker ps -a
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. a4bf8c27421d test:1 "/home/test/docker.sh" 5 minutes ago Up 5 minutes 0.0.0.0:2222->22/tcp Mr.yang
  5. 1bf64c93e7a1 test:1 "/home/test/docker.sh" 10 minutes ago Up 10 minutes elegant_blackwell
  6. [root@docker 7]# docker restart a4bf8c27421d
  7. a4bf8c27421d
  8. [root@docker 7]# docker ps -a
  9. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  10. a4bf8c27421d test:1 "/home/test/docker.sh" 6 minutes ago Up 14 seconds 0.0.0.0:2222->22/tcp Mr.yang
  11. 1bf64c93e7a1 test:1 "/home/test/docker.sh" 11 minutes ago Up 11 minutes elegant_blackwell

3.31 Docker命令:stats

  1. #用于时时输出容器的资源使用情况
  2. [root@docker 7]# docker ps -a
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. a4bf8c27421d test:1 "/home/test/docker.sh" 6 minutes ago Up 49 seconds 0.0.0.0:2222->22/tcp Mr.yang
  5. 1bf64c93e7a1 test:1 "/home/test/docker.sh" 11 minutes ago Up 11 minutes elegant_blackwell
  6. [root@docker 7]# docker stats a4bf8c27421d

image.png-9.6kB

  1. #--no-tream只输出一次
  2. [root@docker 7]# docker stats a4bf8c27421d --no-stream
  3. CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
  4. a4bf8c27421d Mr.yang 0.00% 288KiB / 974.5MiB 0.03% 648B / 0B 0B / 0B 2

3.32 Docker命令:tag

  1. #用于从一个指定的镜像创建另外一个镜像
  2. [root@docker 7]# docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. Mr.yang/centos7 1 f1c34c7f90da 23 hours ago 200MB
  5. test 1 f1c34c7f90da 23 hours ago 200MB
  6. centos 7 75835a67d134 13 days ago 200MB
  7. centos latest 75835a67d134 13 days ago 200MB
  8. [root@docker 7]# docker tag test:1 test:2 #我们可以指定名字:标志来创建
  9. [root@docker 7]# docker images
  10. REPOSITORY TAG IMAGE ID CREATED SIZE
  11. Mr.yang/centos7 1 f1c34c7f90da 23 hours ago 200MB
  12. test 1 f1c34c7f90da 23 hours ago 200MB
  13. test 2 f1c34c7f90da 23 hours ago 200MB
  14. centos 7 75835a67d134 13 days ago 200MB
  15. centos latest 75835a67d134 13 days ago 200MB
  1. [root@docker 7]# docker rmi test:2
  2. Untagged: test:2
  3. [root@docker 7]# docker images
  4. REPOSITORY TAG IMAGE ID CREATED SIZE
  5. test 1 f1c34c7f90da 23 hours ago 200MB
  6. Mr.yang/centos7 1 f1c34c7f90da 23 hours ago 200MB
  7. centos 7 75835a67d134 13 days ago 200MB
  8. centos latest 75835a67d134 13 days ago 200MB
  9. [root@docker 7]# docker tag f1c34c7f90da test:2 #我们也可以指定image id来创建
  10. [root@docker 7]# docker images
  11. REPOSITORY TAG IMAGE ID CREATED SIZE
  12. Mr.yang/centos7 1 f1c34c7f90da 23 hours ago 200MB
  13. test 1 f1c34c7f90da 23 hours ago 200MB
  14. test 2 f1c34c7f90da 23 hours ago 200MB
  15. centos 7 75835a67d134 13 days ago 200MB
  16. centos latest 75835a67d134 13 days ago 200MB

3.33 Docker命令:top

  1. #用于显示指定容器的进程信息
  2. [root@docker 7]# docker ps -a
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. a4bf8c27421d test:1 "/home/test/docker.sh" 22 minutes ago Up 17 minutes 0.0.0.0:2222->22/tcp Mr.yang
  5. 1bf64c93e7a1 test:1 "/home/test/docker.sh" 28 minutes ago Up 28 minutes elegant_blackwell
  6. [root@docker 7]# docker top a4bf8c27421d
  7. UID PID PPID C STIME TTY TIME CMD
  8. root 3446 3432 0 15:14 ? 00:00:00 /bin/bash /home/test/docker.sh
  9. root 3993 3446 0 15:31 ? 00:00:00 sleep 5

3.34 Docker命令:update

  1. #用于调整一个或多个容器的启动配置
  2. [root@docker 7]# docker update --help
  3. Usage: docker update [OPTIONS] CONTAINER [CONTAINER...]
  4. Update configuration of one or more containers
  5. Options:
  6. --blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable
  7. (default 0)
  8. --cpu-period int Limit CPU CFS (Completely Fair Scheduler) period
  9. --cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota
  10. --cpu-rt-period int Limit the CPU real-time period in microseconds
  11. --cpu-rt-runtime int Limit the CPU real-time runtime in microseconds
  12. -c, --cpu-shares int CPU shares (relative weight)
  13. --cpus decimal Number of CPUs
  14. --cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
  15. --cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
  16. --kernel-memory bytes Kernel memory limit
  17. -m, --memory bytes Memory limit
  18. --memory-reservation bytes Memory soft limit
  19. --memory-swap bytes Swap limit equal to memory plus swap: '-1' to enable unlimited swap
  20. --restart string Restart policy to apply when a container exits

3.35 Docker命令:version and wait

version用于显示docker的版本信息
wait用于捕捉一个或多个容器的退出状态,并返回退出状态码

  1. #显示docker版本信息
  2. [root@docker 7]# docker version
  3. Client:
  4. Version: 18.06.1-ce
  5. API version: 1.38
  6. Go version: go1.10.3
  7. Git commit: e68fc7a
  8. Built: Tue Aug 21 17:23:03 2018
  9. OS/Arch: linux/amd64
  10. Experimental: false
  11. Server:
  12. Engine:
  13. Version: 18.06.1-ce
  14. API version: 1.38 (minimum version 1.12)
  15. Go version: go1.10.3
  16. Git commit: e68fc7a
  17. Built: Tue Aug 21 17:25:29 2018
  18. OS/Arch: linux/amd64
  19. Experimental: false
  1. #监听容器的退出状态并返回状态码
  2. [root@docker 7]# docker ps -a
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. a4bf8c27421d test:1 "/home/test/docker.sh" 25 minutes ago Up 19 minutes 0.0.0.0:2222->22/tcp Mr.yang
  5. 1bf64c93e7a1 test:1 "/home/test/docker.sh" 30 minutes ago Up 30 minutes elegant_blackwell
  6. [root@docker 7]# docker wait a4bf8c27421d #需要在开一个窗口stop这个容器进程再查看
  7. 137
  8. [root@docker ~]# docker stop a4bf8c27421d
  9. a4bf8c27421d

3.36 Docker命令:login && logout && push

  • login用于登陆docker hub官方公有仓库
  • logout用于登出docker hub官方公有仓库
  • push用于将本地镜像提交到docker hub

由于docker hub国内已经无法注册,因此无法演示上述内容

DockerHub官方公有镜像仓库:https://hub.docker.com/

4.管理应用程序数据

4.1 Volume和Bind Mount

将Docker主机数据挂载到容器


  • Docker提供三种不同方式将数据从宿主机挂载到容器中:volumes,bind
    • mounts和tmpfs。
    • volumes:Docker管理宿主机文件系统的一部分(/var/lib/docker/volumes)
    • bind mounts:可以存储在宿主机系统的任意位置
    • tmpfs:挂载存储在宿主机系统的内存中,而不会写入宿主机的文件系统

image.png-41.9kB

4.1.1 Volume
  1. #创建一个卷
  2. [root@docker 7]# docker volume create nginx-vol
  3. nginx-vol
  4. [root@docker 7]# docker volume ls
  5. DRIVER VOLUME NAME
  6. local nginx-vol
  7. [root@docker 7]# docker volume inspect nginx-vol
  8. [
  9. {
  10. "CreatedAt": "2018-10-24T15:18:01+08:00", #创建时间
  11. "Driver": "local", #驱动
  12. "Labels": {},
  13. "Mountpoint": "/var/lib/docker/volumes/nginx-vol/_data", #挂载点
  14. "Name": "nginx-vol", #卷名
  15. "Options": {},
  16. "Scope": "local"
  17. }
  18. ]
  1. #下载一个nginx官方镜像
  2. [root@docker 7]# docker pull nginx
  3. Using default tag: latest
  4. latest: Pulling from library/nginx
  5. f17d81b4b692: Pull complete
  6. d5c237920c39: Pull complete
  7. a381f92f36de: Pull complete
  8. Digest: sha256:b73f527d86e3461fd652f62cf47e7b375196063bbbd503e853af5be16597cb2e
  9. Status: Downloaded newer image for nginx:latest
  10. [root@docker 7]# docker images
  11. REPOSITORY TAG IMAGE ID CREATED SIZE
  12. nginx latest dbfc48660aeb 8 days ago 109MB
  13. centos latest 75835a67d134 2 weeks ago 200MB
  14. [root@docker 7]# docker run -dit --name=nginx-test --mount src=nginx-vol,dst=/usr/share/nginx/html nginx
  15. 89bfd1e8ae758f1b30b248210afe7230ad0a6d1cfad1a291cf90f226418957ed
  16. [root@docker 7]# docker ps -a
  17. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  18. 89bfd1e8ae75 nginx "nginx -g 'daemon of…" About a minute ago Up About a minute 80/tcp nginx-test
  19. 特别说明:
  20. --name:容器的名字
  21. --mount:挂载
  22. src:源卷的名字
  23. dst:挂载到容器中的路径
  1. #向容器中的挂载目录创建文件,查看是否挂载成功
  2. [root@docker 7]# docker exec nginx-test touch /usr/share/nginx/html/test.txt
  3. [root@docker 7]# docker exec nginx-test ls /usr/share/nginx/html/
  4. 50x.html
  5. index.html
  6. test.txt #有了
  7. [root@docker 7]# ls /var/lib/docker/volumes/nginx-vol/_data/
  8. 50x.html index.html test.txt #成功
  1. #清理容器进程
  2. [root@docker 7]# docker ps -a
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. 89bfd1e8ae75 nginx "nginx -g 'daemon of…" About an hour ago Up About an hour 80/tcp nginx-test
  5. [root@docker 7]# docker stop nginx-test
  6. nginx-test
  7. [root@docker 7]# docker rm nginx-test
  8. nginx-test
  9. [root@docker 7]# docker ps -a
  10. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  11. [root@dcker 7]# ls /var/lib/docker/volumes/nginx-vol/_data/
  12. 50x.html index.html test.txt #清理容器后,挂载的卷的数据仍旧存在
  1. #重新启动镜像nginx的容器进程
  2. [root@docker 7]# docker run -dit --name nginx-test -p 88:80 --mount src=nginx-vol,dst=/usr/share/nginx/html nginx
  3. 3fa5a3ce380e3fd582774d8202ffcbf7eba8b6e695528402dc00702a348585f8
  4. [root@docker 7]# docker ps -a
  5. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  6. 3fa5a3ce380e nginx "nginx -g 'daemon of…" 7 seconds ago Up 6 seconds 0.0.0.0:88->80/tcp nginx-test
  7. 特别提示:docker run的-p参数:指定端口的映射,8880的意思就是将宿主机88端口的访问映射到容器进程的80端口

现在我们通过浏览器访问宿主机的88端口,进而访问容器进程的80端口
http://192.168.200.63:88

image.png-25.2kB

  1. #在数据卷nginx-vol里换一个网页在访问
  2. [root@docker 7]# docker exec nginx-test echo "welcome to yunjisuan" > /var/lib/docker/volumes/nginx-vol/_data/index.html
  3. [root@docker 7]# docker exec nginx-test cat /usr/share/nginx/html/index.html
  4. welcome to yunjisuan

image.png-6.7kB
现在我们在启动一个镜像nginx的进程,让两个nginx的容器进程公用一个数据卷nginx-vol

  1. [root@docker 7]# docker run -dit --name nginx-test2 -p 89:80 --mount src=nginx-vol,dst=/usr/share/nginx/html nginx
  2. d4315737080f58107cc3e12ad443c3ea9f56be5421e054f1cf5a42ee3063c7a2
  3. [root@docker 7]# docker ps -a
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. d4315737080f nginx "nginx -g 'daemon of…" 3 seconds ago Up 1 second 0.0.0.0:89->80/tcp nginx-test2
  6. 756185ec0f0c nginx "nginx -g 'daemon of…" 5 minutes ago Up 5 minutes 0.0.0.0:88->80/tcp nginx-test

我们用浏览器访问docker宿主机的89端口
http://192.168.200.63:89

image.png-6.8kB

4.1.2 Bind Mounts
  1. [root@docker 7]# docker ps -a
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. d4315737080f nginx "nginx -g 'daemon of…" 5 minutes ago Up 5 minutes 0.0.0.0:89->80/tcp nginx-test2
  4. 756185ec0f0c nginx "nginx -g 'daemon of…" 10 minutes ago Up 10 minutes 0.0.0.0:88->80/tcp nginx-test
  5. [root@docker 7]# docker run -dit --name nginx-test3 -p 90:80 --mount type=bind,src=/var/lib/docker/volumes/nginx-vol/_data,dst=/usr/share/nginx/html nginx
  6. f735972634827d41a58e7139514d9c8443abd76b557e49141e0e6acbe7bf2292
  7. [root@docker 7]# docker ps -a
  8. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  9. f73597263482 nginx "nginx -g 'daemon of…" 3 seconds ago Up 1 second 0.0.0.0:90->80/tcp nginx-test3
  10. d4315737080f nginx "nginx -g 'daemon of…" 5 minutes ago Up 5 minutes 0.0.0.0:89->80/tcp nginx-test2
  11. 756185ec0f0c nginx "nginx -g 'daemon of…" 10 minutes ago Up 10 minutes 0.0.0.0:88->80/tcp nginx-test

我们用浏览器访问docker宿主机的90端口
http://192.168.200.63:90

image.png-6.8kB

特别提示:
bind mounts可以挂载宿主机上的任意目录,而volume先得创建后才能挂载

4.2 实战容器部署LNMP网站平台

首先我们下载一个wordpress博客wget https://cn.wordpress.org/wordpress-4.7.4-zh_CN.tar.gz

4.2.1 创建MySQL数据库容器
  1. #下载MySQL5.6版本镜像
  2. [root@docker 7]# docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. nginx latest dbfc48660aeb 9 days ago 109MB
  5. centos latest 75835a67d134 2 weeks ago 200MB
  6. [root@docker 7]# docker pull mysql:5.6
  7. 5.6: Pulling from library/mysql
  8. f17d81b4b692: Already exists
  9. c691115e6ae9: Pull complete
  10. 41544cb19235: Pull complete
  11. 254d04f5f66d: Pull complete
  12. 4fe240edfdc9: Pull complete
  13. b79c3b745cc6: Pull complete
  14. c6ebcbee59b1: Pull complete
  15. f07692414bc1: Pull complete
  16. 0d34369684f4: Pull complete
  17. b6c15fa1d9de: Pull complete
  18. db940c78ebb4: Pull complete
  19. Digest: sha256:0ae5d57a7b349384bb46bc7f6b73c21d2806b02bbb99554684e78b700094ab62
  20. Status: Downloaded newer image for mysql:5.6
  21. [root@docker 7]# docker images
  22. REPOSITORY TAG IMAGE ID CREATED SIZE
  23. mysql 5.6 085b9b6ec8a9 9 days ago 256MB
  24. nginx latest dbfc48660aeb 9 days ago 109MB
  25. centos latest 75835a67d134 2 weeks ago 200MB
  1. #创建一个自定义网络
  2. [root@docker 7]# docker network create lnmp
  3. 5a08a644a2cf04227ea8e618f79bc5a961fc95a9b442dac36e5d6b52101530e8
  4. [root@docker 7]# docker network ls
  5. NETWORK ID NAME DRIVER SCOPE
  6. e2fa9dde793d bridge bridge local
  7. 0e3d0cd4f911 host host local
  8. 5a08a644a2cf lnmp bridge local #有了(默认驱动为网桥)
  9. c7527cab10af none null local
  10. #启动MySQL数据库容器
  11. [root@docker 7]# docker run -dit --name lnmp_mysql --network lnmp -p 3306:3306 --mount src=mysql-vol,dst=/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123123 mysql:5.6 --character-set-server=utf8
  12. 9cc9fa151a745409f8ef89612644a37f618196c63b3767d5e2e08d191242fed6
  13. [root@docker 7]# docker ps -a
  14. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  15. 9cc9fa151a74 mysql:5.6 "docker-entrypoint.s…" 4 seconds ago Up 3 seconds 0.0.0.0:3306->3306/tcp lnmp_mysql

特别提示:

  • 自定义网络lnmp如果不提前创建的话,在启动容器进程时会报错
  • 如果没有提前pull好mysql:5.6那么容器在启动时会自动下载对应镜像
  • 如果没有提前docker volume create mysql-vol,那么容器启动时会自动创建
  1. #查看容器lnmp_mysql的日志输出
  2. [root@docker 7]# docker logs lnmp_mysql
  3. #以下省略若干。。。
  4. #查看容器里启动的进程
  5. [root@docker 7]# docker top lnmp_mysql
  6. UID PID PPID C STIME TTY TIME CMD
  7. polkitd 2142 2128 0 09:07 pts/0 00:00:00 mysqld --character-set-server=utf8
4.2.2 向容器里的Mysql创建一个库
  1. [root@docker 7]# docker exec lnmp_mysql sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -e"create database wp"'
  2. Warning: Using a password on the command line interface can be insecure.
4.2.3 在docker宿主机上安装mysql的客户端通过端口映射访问容器内的mysql

因为已经将mysql容器的3306端口映射到了docker宿主机的3306,因此我们访问本地即可

  1. #安装mysql客户端
  2. [root@docker 7]# yum -y install mysql
  3. [root@docker 7]# which mysql
  4. /usr/bin/mysql
  5. #查看本机IP地址
  6. [root@docker 7]# hostname -I | xargs -n1 | head -1
  7. 192.168.200.63
  8. #远程方式连接docker宿主机的3306端口
  9. [root@docker 7]# mysql -h192.168.200.63 -P3306 -uroot -p123123
  10. Welcome to the MariaDB monitor. Commands end with ; or \g.
  11. Your MySQL connection id is 2
  12. Server version: 5.6.41 MySQL Community Server (GPL)
  13. Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
  14. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  15. MySQL [(none)]> show databases;
  16. +--------------------+
  17. | Database |
  18. +--------------------+
  19. | information_schema |
  20. | mysql |
  21. | performance_schema |
  22. | wp | #已经创建wp库了
  23. +--------------------+
  24. 4 rows in set (0.30 sec)
4.2.4 创建nginx+PHP环境容器
  1. #创建一个网页目录
  2. [root@docker ~]# mkdir -p /app/wwwroot
  3. [root@docker ~]# ll -d /app/wwwroot
  4. drwxr-xr-x 2 root root 6 10 26 10:26 /app/wwwroot
  5. #下载richarvey/nginx-php-fpm镜像
  6. [root@docker ~]# docker images
  7. REPOSITORY TAG IMAGE ID CREATED SIZE
  8. mysql 5.6 085b9b6ec8a9 9 days ago 256MB
  9. nginx latest dbfc48660aeb 9 days ago 109MB
  10. centos latest 75835a67d134 2 weeks ago 200MB
  11. [root@docker ~]# docker pull richarvey/nginx-php-fpm
  12. #以下省略若干。。。
  13. [root@docker ~]# docker images
  14. REPOSITORY TAG IMAGE ID CREATED SIZE
  15. mysql 5.6 085b9b6ec8a9 9 days ago 256MB
  16. nginx latest dbfc48660aeb 9 days ago 109MB
  17. centos latest 75835a67d134 2 weeks ago 200MB
  18. richarvey/nginx-php-fpm latest de1554d0b081 4 weeks ago 302MB
  1. #启动richarvey/nginx-php-fpm镜像的容器
  2. [root@docker ~]# docker ps -a
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. 9cc9fa151a74 mysql:5.6 "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:3306->3306/tcp lnmp_mysql
  5. [root@docker ~]# docker run -dit --name lnmp_web --network lnmp -p 88:80 --mount type=bind,src=/app/wwwroot,dst=/var/www/html richarvey/nginx-php-fpm
  6. 332c45fd0d12fbe379ae9ea527894276f70ba5c6e57f291f2513032b0afd342f
  7. [root@docker ~]# docker ps -a
  8. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  9. 332c45fd0d12 richarvey/nginx-php-fpm "docker-php-entrypoi…" 7 seconds ago Up 3 seconds 443/tcp, 9000/tcp, 0.0.0.0:88->80/tcp lnmp_web
  10. 9cc9fa151a74 mysql:5.6 "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:3306->3306/tcp lnmp_mysql
4.2.5 解压wordpress到网页目录/app/wwwroot下
  1. [root@docker ~]# ls
  2. anaconda-ks.cfg dockerfile wordpress-4.7.4-zh_CN.tar.gz
  3. [root@docker ~]# tar xf wordpress-4.7.4-zh_CN.tar.gz -C /app/wwwroot/
  4. [root@docker ~]# cd /app/wwwroot/
  5. [root@docker wwwroot]# ls
  6. wordpress
4.2.6 博客wordpress访问测试

通过浏览器进行docker宿主机的88端口的访问测试 http://192.168.200.63:88/wordpress


特别提示:如果在访问时,出现以下情况

image.png-29.8kB

  1. #如果出现连接不上的情况,那么请按顺序执行以下命令一遍
  2. [root@docker wwwroot]# systemctl stop firewalld
  3. [root@docker wwwroot]# systemctl stop iptables.service
  4. Failed to stop iptables.service: Unit iptables.service not loaded.
  5. [root@docker wwwroot]# iptables -F
  6. [root@docker wwwroot]# iptables -P FORWARD ACCEPT
  7. [root@docker wwwroot]# iptables -P INPUT ACCEPT
  8. [root@docker wwwroot]# iptables -P OUTPUT ACCEPT

如果没有意外此时访问浏览器,就可出现下述内容

image.png-68.9kB

特别提示:
如果多次连续访问同一网页,那么浏览器有可能默认去掉指定的端口
因此,若还是访问不到,请查看是否指定了88端口

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注