Docker 基础 (一)
为什么要使用 Docker?
作为一种新兴的虚拟化方式,Docker 跟传统的虚拟化方式相比具有众多的优势。
首先,Docker 容器的启动可以在秒级实现,这相比传统的虚拟机方式要快得多。 其次,Docker 对系统资 源的利用率很高,一台主机上可以同时运行数千个 Docker 容器。
容器除了运行其中应用外,基本不消耗额外的系统资源,使得应用的性能很高,同时系统的开销尽量小。 传统虚拟机方式运行 10 个不同的应用就要起 10 个虚拟机,而Docker 只需要启动 10 个隔离的应用即可。
具体说来,Docker 在如下几个方面具有较大的优势。
更快速的交付和部署
对开发和运维(devop)人员来说,最希望的就是一次创建或配置,可以在任意地方正常运行。
开发者可以使用一个标准的镜像来构建一套开发容器,开发完成之后,运维人员可以直接使用这个容器来 部署代码。 Docker 可以快速创建容器,快速迭代应用程序,并让整个过程全程可见,使团队中的其他成员 更容易理解应用程序是如何创建和工作的。 Docker 容器很轻很快!容器的启动时间是秒级的,大量地节约 开发、测试、部署的时间。
更轻松的迁移和扩展
Docker 容器几乎可以在任意的平台上运行,包括物理机、虚拟机、公有云、私有云、个人电脑、服务器 等。 这种兼容性可以让用户把一个应用程序从一个平台直接迁移到另外一个。
更简单的管理
使用 Docker,只需要小小的修改,就可以替代以往大量的更新工作。所有的修改都以增量的方式被分发和 更新,从而实现自动化并且高效的管理。
对比传统虚拟机总结
特性 |
容量 | 虚拟机 |
启动 | 秒级 | 分钟级 |
硬盘使用 | 一般为 MB | 一般为 GB |
性能 | 接近原生 | 弱于 |
系统支持量 | 单机支持上千个容器 | 一般几十个 |
Docker 包括三个基本概念
镜像(Image) 容器(Container) 仓库(Repository)
理解了这三个概念,就理解了 Docker 的整个生命周期。
Docker 镜像
Docker 镜像就是一个只读的模板。
例如:一个镜像可以包含一个完整的 ubuntu 操作系统环境,里面仅安装了 Apache 或用户需要的其它应用 程序。
镜像可以用来创建 Docker 容器。
Docker 提供了一个很简单的机制来创建镜像或者更新现有的镜像,用户甚至可以直接从其他人那里下载一 个已经做好的镜像来直接使用。
Docker 容器
Docker 利用容器来运行应用。
容器是从镜像创建的运行实例。它可以被启动、开始、停止、删除。每个容器都是相互隔离的、保证安全 的平台。
可以把容器看做是一个简易版的 Linux 环境(包括root用户权限、进程空间、用户空间和网络空间等)和运 行在其中的应用程序。
*注:镜像是只读的,容器在启动的时候创建一层可写层作为最上层。
Docker 仓库
仓库是集中存放镜像文件的场所。有时候会把仓库和仓库注册服务器(Registry)混为一谈,并不严格区 分。实际上,仓库注册服务器上往往存放着多个仓库,每个仓库中又包含了多个镜像,每个镜像有不同的 标签(tag)。
仓库分为公开仓库(Public)和私有仓库(Private)两种形式。
最大的公开仓库是 Docker Hub,存放了数量庞大的镜像供用户下载。 国内的公开仓库包括 Docker Pool 等,可以提供大陆用户更稳定快速的访问。
当然,用户也可以在本地网络内创建一个私有仓库。
当用户创建了自己的镜像之后就可以使用 push 命令将它上传到公有或者私有仓库,这样下次在另外一台 机器上使用这个镜像时候,只需要从仓库上 pull 下来就可以了。
*注:Docker 仓库的概念跟 Git 类似,注册服务器可以理解为 GitHub 这样的托管服务。
CentOS 系列安装 Docker
最小化安装Centos7.5
[root@Docker ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) [root@Docker ~]# uname -r 3.10.0-862.3.3.el7.x86_64
关闭防火墙和selinux
[root@Docker ~]# systemctl stop firewalld [root@Docker ~]# systemctl disable firewalld [root@Docker ~]# setenforce 0 setenforce: SELinux is disabled
版本选择
- Docker
- 17.03之后版本变为Docker CE
- Docker CE
- 社区版,Community Edition
- Docker EE
- 企业版,Enterprise Edition
- 收费版本,强调安全性,提供一些高级特性及商业支持
Docker安装:标准版本
[root@Docker ~]# yum -y install docker [root@Docker ~]# systemctl start docker #启动docker进程 [root@Docker ~]# systemctl enable docker #加入docker开机启动 Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service. [root@Docker ~]# docker ps #查看封装在docker镜像中的正在运行的镜像进程 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@Docker ~]# docker version Client: Version: 1.13.1 API version: 1.26 Package version: docker-1.13.1-63.git94f4240.el7.centos.x86_64 Go version: go1.9.4 Git commit: 94f4240/1.13.1 Built: Fri May 18 15:44:33 2018 OS/Arch: linux/amd64 Server: Version: 1.13.1 API version: 1.26 (minimum version 1.12) Package version: docker-1.13.1-63.git94f4240.el7.centos.x86_64 Go version: go1.9.4 Git commit: 94f4240/1.13.1 Built: Fri May 18 15:44:33 2018 OS/Arch: linux/amd64 Experimental: false
Docker安装:CE社区版
首先清理掉yum安装的docker标准版
这里有两种方式
#第一种清理docker方法 [root@Docker ~]# yum remove docker-latest-logrotate docker-logrotate docker-selinux dockdocker-engine
#第二种清理docker方法
[root@Docker ~]# yum history list #查看yum安装的历史列表
已加载插件:fastestmirror
ID | 登录用户 | 日期和时间 | 操作 | 变更数
-------------------------------------------------------------------------------
31 | 系统 <空> | 2018-07-16 17:16 | Install | 9 E< #这次就是安装
30 | root <root> | 2018-07-09 11:04 | Install | 9 >
29 | root <root> | 2018-07-08 10:38 | Install | 1
28 | root <root> | 2018-07-08 10:33 | Install | 3
27 | root <root> | 2018-06-26 20:27 | Install | 1
26 | root <root> | 2018-06-26 18:20 | Install | 5
25 | root <root> | 2018-06-26 17:13 | Install | 1
24 | root <root> | 2018-06-26 17:06 | Install | 1
23 | root <root> | 2018-06-21 14:50 | Install | 3
22 | root <root> | 2018-06-18 23:11 | Install | 50
21 | root <root> | 2018-06-18 23:04 | Install | 2
20 | root <root> | 2018-06-17 02:59 | I, U | 50
19 | root <root> | 2018-06-16 20:33 | I, U | 30
18 | root <root> | 2018-06-16 20:23 | Install | 1
17 | root <root> | 2018-06-16 20:20 | Install | 2
16 | root <root> | 2018-06-16 13:14 | Install | 1
history list
[root@Docker ~]# yum history info 31 #查看yum历史安装ID为31的安装信息
已加载插件:fastestmirror
事务 ID: 31
起始时间 : Mon Jul 16 17:16:46 2018
启动 RPM 数据库 : 971:bb0b16847e8bd5bbed6649c9045ceb3ea74c0b8d
结束时间 : 17:17:11 2018 (25 秒)
结束 RPM 数据库 : 980:1158b800059814346da9aca5bc32093cb8af18b5
用户 : 系统 <空>
返回码 : 成功
命令行 : install docker-io
事务完成属主:
已安装 rpm-4.11.3-32.el7.x86_64 @anaconda
已安装 yum-3.4.3-158.el7.centos.noarch @anaconda
已安装 yum-metadata-parser-1.1.4-10.el7.x86_64 @anaconda
已安装 yum-plugin-fastestmirror-1.1.31-45.el7.noarch @anaconda
已变更的包:
依赖安装 container-selinux-2:2.55-1.el7.noarch @extras
依赖安装 container-storage-setup-0.9.0-1.rhel75.gite0997c3.el7.noarch @extras
安装 docker-2:1.13.1-63.git94f4240.el7.centos.x86_64 @extras
依赖安装 docker-client-2:1.13.1-63.git94f4240.el7.centos.x86_64 @extras
依赖安装 docker-common-2:1.13.1-63.git94f4240.el7.centos.x86_64 @extras
依赖安装 oci-register-machine-1:0-6.git2b44233.el7.x86_64 @extras
依赖安装 oci-systemd-hook-1:0.1.15-2.gitc04483d.el7.x86_64 @extras
依赖安装 oci-umount-2:2.3.3-3.gite3c9055.el7.x86_64 @extras
依赖安装 skopeo-containers-1:0.1.29-3.dev.git7add6fc.el7.0.x86_64 @extras
Scriptlet 输出:
1 setsebool: SELinux is disabled.
history info
[root@Docker ~]# yum -y history undo 31 #进行yum安装操作回退
#安装依赖包 [root@Docker ~]# yum -y install yum-utils device-mapper-persistent-data lvm2 #添加docker的CE版本的yum源配置文件 [root@Docker ~]# curl https://download.docker.com/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker-ce.repo % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 2424 100 2424 0 0 2544 0 --:--:-- --:--:-- --:--:-- 2543 [root@Docker ~]# ll /etc/yum.repos.d/docker-ce.repo -rw-r--r-- 1 root root 2424 7月 16 17:36 /etc/yum.repos.d/docker-ce.repo #安装CE版本的docker [root@Docker ~]# yum -y install docker-ce [root@Docker ~]# systemctl start docker #启动docker [root@Docker ~]# systemctl enable docker #添加开机启动 [root@Docker ~]# docker version #查看docker版本 Client: Version: 18.03.1-ce API version: 1.37 Go version: go1.9.5 Git commit: 9ee9f40 Built: Thu Apr 26 07:20:16 2018 OS/Arch: linux/amd64 Experimental: false Orchestrator: swarm Server: Engine: Version: 18.03.1-ce API version: 1.37 (minimum version 1.12) Go version: go1.9.5 Git commit: 9ee9f40 Built: Thu Apr 26 07:23:58 2018 OS/Arch: linux/amd64 Experimental: false
# 国内部分用户可能无法连接docker官网提供的源,这里提供阿里云的镜像节点供测试使用 [root@Docker ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo [root@Docker ~]# rpm --import http://mirrors.aliyun.com/docker-ce/linux/centos/gpg [root@Docker ~]# yum makecache fast [root@Docker ~]# yum -y install docker-ce
Docker的操作命令
添加docker国内镜像源
容器Hub服务 https://cr.console.aliyun.com的控制台 提升pull的速度
可以用支付宝登录
cat /etc/docker/daemon.json { "registry-mirrors":[ "https://registry.docker-cn.com" ] } [root@docker ~]# systemctl daemon-reload [root@docker ~]# systemctl restart docker
Docker命令:search
用于从docker的官方公有镜像仓库查找镜像
1.查看Docker Hub上公开的centos镜像
[root@docker ~]# docker search centos NAME DESCRIPTION STARS OFFICIAL AUTOMATED centos The official build of CentOS. 4419 [OK] ...以下省略无数行.... Name:镜像的名字 DESCRIPTION:描述 STARS:星级(越高越好) OFFICIAL:是否是官方发布的 AUTOMATED:是否自动化的
2.查找星级多于100的centos镜像
[root@docker ~]# docker search centos -f stars=100 NAME DESCRIPTION STARS OFFICIAL AUTOMATED centos The official build of CentOS. 4419 [OK] ansible/centos7-ansible Ansible on Centos7 114 [OK] /:符号用于分割作者名称和镜像名称 ansible/centos7-ansible:ansible是作者名称,centos7-ansible是镜像名称
3.多条件查找--filter
#查找官方发布的,星级大于100的centos镜像 [root@docker ~]# docker search centos --filter is-official=true --filter stars=100 NAME DESCRIPTION STARS OFFICIAL AUTOMATED centos The official build of CentOS. 4419 [OK]
Docker命令:pull
用于从Docker Hub上下载公有镜像
#查找符合条件的hello-world镜像 [root@docker ~]# docker search hello-world --filter is-official=true --filter stars=100 NAME DESCRIPTION STARS OFFICIAL AUTOMATED hello-world Hello World! (an example of minimal Dockeriz… 572 [OK] #下载目标hello-world镜像 [root@docker ~]# docker pull hello-world Using default tag: latest latest: Pulling from library/hello-world 9bb5a5d4561a: Pull complete Digest: sha256:3e1764d0f546ceac4565547df2ac4907fe46f007ea229fd7ef2718514bcec35d Status: Downloaded newer image for hello-world:latest #下载成功latest是标记tag #下载目标centos:7镜像 [root@docker 7]# docker pull centos:7 #网速不好的话,需要点时间耐心等待
Docker命令:images
用于本地镜像的查看
[root@Docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest 2cb0d9787c4d 5 days ago 1.85kB centos 7 49f7960eb7e4 5 weeks ago 200MB
REPOSITORY:镜像仓库(下边罗列的都是本地已有镜像名称)
TAG:镜像的标记(为了区分同名镜像)
IMAGES ID:镜像的ID号
CREATED:此镜像的创建时间
SIZE:此镜像的大小
Docker命令:build
用于本地自定义镜像的构建,需要创建Dockerfile文件
#创建Dockerfile文件的存储目录 [root@docker ~]# mkdir -p /root/dockerfile/lib/centos/7 [root@docker ~]# cd /root/dockerfile/lib/centos/7 #创建docker.sh脚本 [root@docker 7]# cat docker.sh #!/bin/bash while true do echo "welcome" sleep 5 done #创建Dockerfile配置文件,文件名称必须为Dockerfile,第一个字母必须大写 [root@docker 7]# vim Dockerfile [root@docker 7]# cat Dockerfile FROM centos #从centos源镜像的基础上进行构建 LABEL MAINTATNER="DaMoWang.com" #作者的名称 RUN ln -sfv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #RUN:在镜像构建过程中运行命令 ADD docker.sh /home/test/ #从本地系统中把docker.sh文件添加到构建中的镜像的/home/test/目录下 RUN chmod +x /home/test/docker.sh #在镜像构建过程中运行命令 CMD ["/home/test/docker.sh"] #构建镜像完成时,最后执行的命令 #根据Dockfile配置文件构建一个自定义镜像 docker build -t damowang/centos7:1 . #-t 指定镜像名称 :1 设定镜像的tag标记 Sending build context to Docker daemon 3.072kB Step 1/6 : FROM centos ---> 49f7960eb7e4 Step 2/6 : LABEL MAINTATNER="damowang" ---> Using cache ---> 79ac7152beab Step 3/6 : RUN ln -sfv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime ---> Using cache ---> eb1f30c3f53f Step 4/6 : ADD docker.sh /home/test/ ---> Using cache ---> ffbac1722ac7 Step 5/6 : RUN chmod +x /home/test/docker.sh ---> Using cache ---> 002a6fb94e60 Step 6/6 : CMD ["/home/test/docker.sh"] ---> Using cache ---> 6e709ee70391 Successfully built 6e709ee70391 #构建成功 Successfully tagged damowang/centos7:1 [root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE damowang/centos7 1 6e709ee70391 4 minutes ago 200MB hello-world latest 2cb0d9787c4d 5 days ago 1.85kB centos 7 49f7960eb7e4 5 weeks ago 200MB
Docker命令:run
运行一个本地镜像
[root@docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE damowang/centos7 1 6e709ee70391 4 minutes ago 200MB hello-world latest 2cb0d9787c4d 5 days ago 1.85kB centos 7 49f7960eb7e4 5 weeks ago 200MB [root@Docker 7]# docker run -d -it 6e709ee70391 /bin/bash 7f76e680570f6e3047acdff116cf5253a840bb8719a492e1f6c20e34bd343f11 #docker run:运行一个指定的images id -d:放在后台运行 -i:可以进行命令交互 -t:制作一个伪终端用于登陆 6e709ee70391:镜像的ID
Docker命令:ps
查看已经运行的镜像的进程
[root@docker ~]# docker ps -a #查看所有运行的镜像进程(包含退出的exit) CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7f76e680570f 6e709ee70391 "/bin/bash" 2 minutes ago Up 2 minutes hungry_beaver #STATUS:进程的状态,UP表示正在运行中,EXIT表示已经退出了。
Docker命令:attach
从本地系统中切入到某个STATUS状态是UP的镜像进程里
root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7f76e680570f 6e709ee70391 "/bin/bash" 7 minutes ago Up 7 minutes hungry_beaver [root@Docker 7]# docker attach 7f76e680570f #切入到容器号为093e4fb8b09c的镜像进程里 [root@7f76e680570f /]# ls #已经进入容器里了 bin etc lib media opt root sbin sys usr dev home lib64 mnt proc run srv tmp var [root@7f76e680570f /]# exit #退出容器 exit [root@Docker 7]# docker ps -a #容器的进程的STATUS已经处于EXIT状态(之前是后台运行的,切入进去后执行exit就等于手动退出了) CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7f76e680570f 6e709ee70391 "/bin/bash" 8 minutes ago Exited (0) 13 seconds ago hungry_beaver
Docker命令:stop
用于停止一个正在运行着的容器进程
[root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7f76e680570f 6e709ee70391 "/bin/bash" 11 minutes ago Exited (0) 2 minutes ago hungry_beaver #再次在后台启动一个镜像 [root@Docker 7]# docker run -d -it 6e709ee70391 /bin/bash a8dd8894050cca65230e70b1d1be40b620fc2bd3b67a642d68efdf7f2a38d42c [root@Docker 7]# docker ps -a #增加了一个容器进程 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a8dd8894050c 6e709ee70391 "/bin/bash" 59 seconds ago Up 58 seconds priceless_shockley 7f76e680570f 6e709ee70391 "/bin/bash" 12 minutes ago Exited (0) 4 minutes ago hungry_beaver #停止一个运行着的容器进程 [root@Docker 7]# docker stop a8dd8894050c a8dd8894050c [root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a8dd8894050c 6e709ee70391 "/bin/bash" 2 minutes ago Exited (137) 36 seconds ago priceless_shockley 7f76e680570f 6e709ee70391 "/bin/bash" 14 minutes ago Exited (0) 6 minutes ago hungry_beaver
Docker命令:start
用于启动一个已经停止了的容器进程
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a8dd8894050c 6e709ee70391 "/bin/bash" 3 minutes ago Exited (137) About a minute ago priceless_shockley 7f76e680570f 6e709ee70391 "/bin/bash" 15 minutes ago Exited (0) 7 minutes ago hungry_beaver [root@Docker 7]# docker start a8dd8894050c a8dd8894050c [root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a8dd8894050c 6e709ee70391 "/bin/bash" 5 minutes ago Up 14 seconds priceless_shockley 7f76e680570f 6e709ee70391 "/bin/bash" 17 minutes ago Exited (0) 8 minutes ago hungry_beaver
Docker命令:rm
用于删除一个已经停止了的容器进程
[root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a8dd8894050c 6e709ee70391 "/bin/bash" 5 minutes ago Up 14 seconds priceless_shockley 7f76e680570f 6e709ee70391 "/bin/bash" 17 minutes ago Exited (0) 8 minutes ago hungry_beaver [root@Docker 7]# docker rm 7f76e680570f #注意运行中的容器进程需要先stop,才能删除 7f76e680570f [root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a8dd8894050c 6e709ee70391 "/bin/bash" 7 minutes ago Up 3 minutes priceless_shockley
Docker命令:rmi
用于删除一个未用作容器启动的本地镜像
[root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE damowang/centos7 1 6e709ee70391 31 minutes ago 200MB hello-world latest 2cb0d9787c4d 5 days ago 1.85kB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB [root@Docker 7]# docker rmi -f 2cb0d9787c4d #-f 强制删除(即便被占用) Untagged: hello-world:latest Untagged: hello-world@sha256:4b8ff392a12ed9ea17784bd3c9a8b1fa3299cac44aca35a85c90c5e3c7afacdc Deleted: sha256:2cb0d9787c4dd17ef9eb03e512923bc4db10add190d3f84af63b744e353a9b34 Deleted: sha256:ee83fc5847cb872324b8a1f5dbfd754255367f4280122b4e2d5aee17818e31f5 [root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE damowang/centos7 1 6e709ee70391 33 minutes ago 200MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB [root@Docker 7]# docker rmi 6e709ee70391 #但要注意,被用作容器启动的镜像是不能删除的(需先rm删除容器进程) Error response from daemon: conflict: unable to delete 6e709ee70391 (cannot be forced) - image is being used by running container a8dd8894050c docker rmi -f 6e709ee70391 Error response from daemon: conflict: unable to delete 6e709ee70391 (cannot be forced) - image is being used by running container a8dd8894050c [root@docker ~]# docker ps -a #查看容器进程,被占用中 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a8dd8894050c 6e709ee70391 "/bin/bash" 17 minutes ago Up 12 minutes priceless_shockley
Docker命令:commit
将一个更改过的容器进程的容器状态保存为一个新的镜像
[root@Docker 7]# docker ps -a #查看启动的容器进程 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a8dd8894050c 6e709ee70391 "/bin/bash" 18 minutes ago Up 13 minutes priceless_shockley [root@Docker 7]# docker attach a8dd8894050c #切入容器进程 [root@a8dd8894050c /]# ls bin etc lib media opt root sbin sys usr dev home lib64 mnt proc run srv tmp var [root@a8dd8894050c /]# mkdir damowang #在容器进程里创建damowang目录 [root@a8dd8894050c /]# ls bin dev home lib64 mnt proc run srv tmp var damowang etc lib media opt root sbin sys usr [root@a8dd8894050c /]# exit #退出容器进程 exit [root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a8dd8894050c 6e709ee70391 "/bin/bash" 20 minutes ago Exited (0) 8 seconds ago priceless_shockley [root@Docker 7]# docker commit a8dd8894050c damowang/centos:2 #将更改后的容器进程保存为一个新的镜像 sha256:0bfe626371890c061cbce5ac5b694b7223d512cb63a9386468ae6613f520c94f [root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE damowang/centos 2 0bfe62637189 27 seconds ago 200MB #保存成功 damowang/centos7 1 6e709ee70391 41 minutes ago 200MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB #启动新保存的镜像 [root@Docker 7]# docker run -d -it 0bfe62637189 /bin/bash 711c165d7e4a030ce464ca43aee87afd9eab8e0359ae4cb591617c797b87da69 [root@Docker 7]# docker ps -a #查看新镜像的容器进程 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 711c165d7e4a 0bfe62637189 "/bin/bash" 8 seconds ago Up 7 seconds heuristic_tereshkova a8dd8894050c 6e709ee70391 "/bin/bash" 22 minutes ago Exited (0) 2 minutes ago priceless_shockley [root@Docker 7]# docker attach 711c165d7e4a #切入新镜像的容器进程 [root@711c165d7e4a /]# ls -d damowang #我们发现之前创建的目录仍旧存在 damowang
Docker命令:exec
用于从本地操作系统上直接向容器进程发布执行命令并返回结果
docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 711c165d7e4a 0bfe62637189 "/bin/bash" 5 minutes ago Up 3 seconds heuristic_tereshkova a8dd8894050c 6e709ee70391 "/bin/bash" 28 minutes ago Exited (0) 7 minutes ago priceless_shockley [root@Docker 7]# docker exec 711c165d7e4a ls /tmp/ #查看容器进程里的/tmp目录下所有内容 ks-script-3QMvMi yum.log [root@Docker 7]# docker exec 711c165d7e4a ls -d /damowang #查看容器进程里/yunjisuan目录 /damowang
Docker命令:cp
用于在容器进程和本地系统之间复制文件
[root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 711c165d7e4a 0bfe62637189 "/bin/bash" 7 minutes ago Up About a minute heuristic_tereshkova a8dd8894050c 6e709ee70391 "/bin/bash" 29 minutes ago Exited (0) 9 minutes ago priceless_shockley [root@Docker 7]# docker exec 711c165d7e4a ls /tmp/ #向容器进程发布命令 ks-script-3QMvMi yum.log [root@Docker 7]# docker cp 711c165d7e4a:/tmp/yum.log . [root@Docker 7]# ls Dockerfile docker.sh yum.log #已经复制过来了 [root@Docker 7]# docker cp docker.sh 711c165d7e4a:/tmp #将本地文件复制到容器进程里 [root@Docker 7]# docker exec 711c165d7e4a ls /tmp/ docker.sh #复制成功 ks-script-3QMvMi yum.log
Docker命令:create
用于创建一个容器进程,但是并不启动它
[root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE damowang/centos 2 0bfe62637189 15 minutes ago 200MB damowang/centos7 1 6e709ee70391 About an hour ago 200MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB [root@Docker 7]# docker create -it 0bfe62637189 #创建一个镜像的容器进程,但不直接启动 84d6659c1dd832f1a94e66e03d8f1c018afa24e7d558a2c5a61e1a624194e62b [root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 84d6659c1dd8 0bfe62637189 "/bin/bash" 12 seconds ago Created gallant_hoover #创建状态并未启动 711c165d7e4a 0bfe62637189 "/bin/bash" 16 minutes ago Up 10 minutes heuristic_tereshkova a8dd8894050c 6e709ee70391 "/bin/bash" 39 minutes ago Exited (0) 18 minutes ago priceless_shockley [root@Docker 7]# docker start 84d6659c1dd8 84d6659c1dd8 [root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 84d6659c1dd8 0bfe62637189 "/bin/bash" 2 minutes ago Up 34 seconds gallant_hoover 711c165d7e4a 0bfe62637189 "/bin/bash" 18 minutes ago Up 12 minutes heuristic_tereshkova a8dd8894050c 6e709ee70391 "/bin/bash" 41 minutes ago Exited (0) 20 minutes ago priceless_shockley
Docker命令:diff
查看容器进程与源镜像做对比,发生了改变的文件或文件夹
[root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 84d6659c1dd8 0bfe62637189 "/bin/bash" 3 minutes ago Up 2 minutes gallant_hoover 711c165d7e4a 0bfe62637189 "/bin/bash" 20 minutes ago Up 14 minutes heuristic_tereshkova a8dd8894050c 6e709ee70391 "/bin/bash" 42 minutes ago Exited (0) 22 minutes ago priceless_shockley [root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE damowang/centos 2 0bfe62637189 21 minutes ago 200MB damowang/centos7 1 6e709ee70391 About an hour ago 200MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB [root@Docker 7]# docker run -d -it 0bfe62637189 #启动一个镜像的容器进程f4b9ce8842f3f123f4d65dfc9192946c82562ebd683fcf9070005f8d186002a0 [root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f4b9ce8842f3 0bfe62637189 "/bin/bash" 17 seconds ago Up 16 seconds wonderful_aryabhata 84d6659c1dd8 0bfe62637189 "/bin/bash" 5 minutes ago Up 3 minutes gallant_hoover 711c165d7e4a 0bfe62637189 "/bin/bash" 21 minutes ago Up 15 minutes heuristic_tereshkova a8dd8894050c 6e709ee70391 "/bin/bash" 44 minutes ago Exited (0) 23 minutes ago priceless_shockley [root@Docker 7]# docker attach f4b9ce8842f3 #切入容器进程 [root@f4b9ce8842f3 /]# cd /damowang/ [root@f4b9ce8842f3 damowang]# pwd /damowang [root@f4b9ce8842f3 damowang]# touch {1..10} #在容器进程中创建文件 [root@f4b9ce8842f3 damowang]# ls 1 10 2 3 4 5 6 7 8 9 [root@f4b9ce8842f3 damowang]# exit #退出容器进程 exit [root@Docker 7]# docker diff f4b9ce8842f3 #查看容器进程的变化 C /damowang A /damowang/1 A /damowang/10 A /damowang/2 A /damowang/3 A /damowang/4 A /damowang/5 A /damowang/6 A /damowang/7 A /damowang/8 A /damowang/9 C /root/.bash_history
Docker命令:events
时时监测容器的变化情况
[root@Docker ~]# docker events 2018-07-16T21:56:25.442638653+08:00 container export f4b9ce8842f3f123f4d65dfc9192946c82562ebd683fcf9070005f8d186002a0 (MAINTATNER=damowang, image=0bfe62637189, name=wonderful_aryabhata, org.label-schema.schema-version== 1.0 org.label-schema.name=CentOS Base Image org.label-schema.vendor=CentOS org.label-schema.license=GPLv2 org.label-schema.build-date=20180531)
#前台时时监控容器的变化若要检测,需要另外再起一个窗口进行操作
Docker命令:export
将容器进程的文件系统导出到本地
[root@Docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f4b9ce8842f3 0bfe62637189 "/bin/bash" About an hour ago Up 2 minutes wonderful_aryabhata 84d6659c1dd8 0bfe62637189 "/bin/bash" 2 hours ago Exited (137) 6 minutes ago gallant_hoover 711c165d7e4a 0bfe62637189 "/bin/bash" 2 hours ago Exited (137) 6 minutes ago heuristic_tereshkova a8dd8894050c 6e709ee70391 "/bin/bash" 2 hours ago Exited (0) 2 hours ago priceless_shockley [root@Docker ~]# docker export f4b9ce8842f3 > test.tar #将容器进程导出成一个tar包 [root@Docker ~]# ls anaconda-ks.cfg dockerfile test.tar
Docker命令:import
用于将export导出的文件系统创建为一个镜像
[root@Docker ~]# ls [root@Docker ~]# docker import test.tar damowang/centos:3 #将导出的tar包文件系统生成一个新的镜像 sha256:a46ed9ea14cb371ddd16ede6a137a7063794da27a0d0641a0bf441e29b0aa909 [root@Docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE damowang/centos 3 a46ed9ea14cb 40 seconds ago 200MB damowang/centos 2 0bfe62637189 4 hours ago 200MB damowang/centos7 1 6e709ee70391 4 hours ago 200MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB
Docker命令:history
用于查看一个镜像的历史修改纪录
[root@Docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE damowang/centos 3 a46ed9ea14cb 2 minutes ago 200MB damowang/centos 2 0bfe62637189 4 hours ago 200MB damowang/centos7 1 6e709ee70391 4 hours ago 200MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB [root@Docker ~]# docker history a46ed9ea14cb IMAGE CREATED CREATED BY SIZE COMMENT a46ed9ea14cb 2 minutes ago 200MB Imported from - [root@Docker ~]# docker history 0bfe62637189 IMAGE CREATED CREATED BY SIZE COMMENT 0bfe62637189 4 hours ago /bin/bash 80B 6e709ee70391 5 hours ago /bin/sh -c #(nop) CMD ["/home/test/docker.s… 0B 002a6fb94e60 5 hours ago /bin/sh -c chmod +x /home/test/docker.sh 88B ffbac1722ac7 5 hours ago /bin/sh -c #(nop) ADD file:5b33dd15349e0d728… 88B eb1f30c3f53f 5 hours ago /bin/sh -c ln -sfv /usr/share/zoneinfo/Asia/… 33B 79ac7152beab 5 hours ago /bin/sh -c #(nop) LABEL MAINTATNER=damowang 0B 49f7960eb7e4 5 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B <missing> 5 weeks ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B <missing> 5 weeks ago /bin/sh -c #(nop) ADD file:8f4b3be0c1427b158… 200MB
Docker命令:info
用于查看当前操作系统的docker运行信息
[root@Docker ~]# [root@Docker ~]# docker info Containers: 2 #容器进程2个 Running: 1 #正在运行状态的容器1个 Paused: 0 Stopped: 1 Images: 8 #一共有8个镜像 Server Version: 18.03.1-ce Storage Driver: overlay2 Backing Filesystem: xfs Supports d_type: true Native Overlay Diff: true Logging Driver: json-file Cgroup Driver: cgroupfs Plugins: Volume: local Network: bridge host macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog Swarm: inactive Runtimes: runc Default Runtime: runc Init Binary: docker-init containerd version: 773c489c9c1b21a6d78b5c538cd395416ec50f88 runc version: 4fc53a81fb7c994640722ac585fa9ca548971871 init version: 949e6fa Security Options: seccomp Profile: default Kernel Version: 3.10.0-862.3.3.el7.x86_64 Operating System: CentOS Linux 7 (Core) OSType: linux Architecture: x86_64 CPUs: 1 Total Memory: 974.6MiB Name: Docker ID: L23S:WCPW:NHU5:UK46:5MRA:OJMB:TOKW:OMYH:CYB6:P5XA:VL2Q:65TL Docker Root Dir: /var/lib/docker Debug Mode (client): false Debug Mode (server): false Registry: https://index.docker.io/v1/ Labels: Experimental: false Insecure Registries: 127.0.0.0/8 Registry Mirrors: https://registry.docker-cn.com/ Live Restore Enabled: false
Docker命令:inspect
查看某个镜像的详细信息
[root@Docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE damowang/centos 3 a46ed9ea14cb 21 minutes ago 200MB damowang/centos 2 0bfe62637189 4 hours ago 200MB damowang/centos7 1 6e709ee70391 5 hours ago 200MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB [root@Docker ~]# docker inspect a46ed9ea14cb [ { "Id": "sha256:a46ed9ea14cb371ddd16ede6a137a7063794da27a0d0641a0bf441e29b0aa909", "RepoTags": [ "damowang/centos:3" ], "RepoDigests": [], "Parent": "", "Comment": "Imported from -", "Created": "2018-07-16T15:52:27.076612643Z", "Container": "", "ContainerConfig": { "Hostname": "", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": null, "Cmd": null, "Image": "", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": null }, "DockerVersion": "18.03.1-ce", "Author": "", "Config": { "Hostname": "", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": null, "Cmd": null, "Image": "", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": null }, "Architecture": "amd64", "Os": "linux", "Size": 199678522, "VirtualSize": 199678522, "GraphDriver": { "Data": { "MergedDir": "/var/lib/docker/overlay2/1541e5357c7ddae19f6655a0cf5a7f002fe80e082abcff749b43c90fba1c7020/merged", "UpperDir": "/var/lib/docker/overlay2/1541e5357c7ddae19f6655a0cf5a7f002fe80e082abcff749b43c90fba1c7020/diff", "WorkDir": "/var/lib/docker/overlay2/1541e5357c7ddae19f6655a0cf5a7f002fe80e082abcff749b43c90fba1c7020/work" }, "Name": "overlay2" }, "RootFS": { "Type": "layers", "Layers": [ "sha256:e54921b330060eb66addbb19163b4bb489584248689f405999cadd2693e99ee6" ] }, "Metadata": { "LastTagTime": "2018-07-16T23:52:27.099320952+08:00" } } ]
Docker命令:kill
强行停止一个或多个正在运行状态的容器进程
[root@Docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3cc1ab69dc5a a46ed9ea14cb "/bin/bash" 11 seconds ago Up 10 seconds mystifying_mirzakhani [root@Docker ~]# docker kill 3cc1ab69dc5a 3cc1ab69dc5a [root@Docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3cc1ab69dc5a a46ed9ea14cb "/bin/bash" 47 seconds ago Exited (137) 4 seconds ago mystifying_mirzakhani
Docker命令:save
用于将一个镜像的文件系统导出到本地(export导出的是容器)
#重新build一个镜像 [root@docker ~]# cd dockerfile/lib/centos/7/ [root@Docker 7]# [root@Docker 7]# docker build -t test:1 . Sending build context to Docker daemon 3.584kB Step 1/6 : FROM centos ---> 49f7960eb7e4 Step 2/6 : LABEL MAINTATNER="damowang" ---> Running in 30d86a376293 Removing intermediate container 30d86a376293 ---> 1e840728b7e7 Step 3/6 : RUN ln -sfv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime ---> Running in 016410b91ccc '/etc/localtime' -> '/usr/share/zoneinfo/Asia/Shanghai' Removing intermediate container 016410b91ccc ---> 35d117846318 Step 4/6 : ADD docker.sh /home/test/ ---> 9cd9a8848ed0 Step 5/6 : RUN chmod +x /home/test/docker.sh ---> Running in 9c1494f9ba1e Removing intermediate container 9c1494f9ba1e ---> 176685b39742 Step 6/6 : CMD ["/home/test/docker.sh"] ---> Running in 3da643e31136 Removing intermediate container 3da643e31136 ---> abc35d57c192 Successfully built abc35d57c192 Successfully tagged test:1 [root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE test 1 abc35d57c192 2 seconds ago 200MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB #save导出test:1这个镜像(也可以用id号) [root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE test 1 abc35d57c192 About a minute ago 200MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB [root@Docker 7]# docker save test:1 > test.tar [root@Docker 7]# ls Dockerfile docker.sh test.tar yum.log
Docker命令:load
用于将save导出到本地的tar包,重新加载为镜像(和源镜像的名字标识完全一样)
[root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE test 1 abc35d57c192 2 minutes ago 200MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB [root@Docker 7]# docker rmi -f test:1 #删除镜像test:1 Untagged: test:1 Deleted: sha256:abc35d57c19219bc97806d54382a60745276b50b12d967c80afd334a944e6be4 Deleted: sha256:176685b397420e52b6024fce07991a7157bdd1c3e68be7035e156c91fd6f6b61 Deleted: sha256:3ba486b5820d4edb5a41bb3355d50307c768712ed2e6afce6455b90a977b0a93 Deleted: sha256:9cd9a8848ed022998eb1a51fceaabdf3d49e4ba8f7ff782b5ffda0bee6503db3 Deleted: sha256:b72e52a36c1a3d07366d5b28079ff9eb7f306ad023c1d0432f99f86441ff18fa Deleted: sha256:35d1178463185fac4647212533e83ce88efb4f5f4612cc3b4422f9be1d3b1ed8 Deleted: sha256:6334246fd055cb644551e76a872e4119df37f96b1d51e36eb545cfc78d480086 Deleted: sha256:1e840728b7e7a26279fcc059f493dab49f73a068ad5aebf08d4ecdc8d6ec0062 [root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB [root@Docker 7]# docker load < test.tar 18991d1930a2: Loading layer 2.048kB/2.048kB 756520987930: Loading layer 3.584kB/3.584kB 4a48d0dbed96: Loading layer 3.072kB/3.072kB Loaded image: test:1 [root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE test 1 abc35d57c192 4 minutes ago 200MB #和原来的镜像完全一样
centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB
Docker命令:logs
用于输出一个容器进程内的操作日志
[root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE test 1 abc35d57c192 5 minutes ago 200MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB [root@Docker 7]# docker run -d -it test:1 e01ec5ecc6927707619a8737e89d5ae30167a17188dc582672519dd0f3057e7b [root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e01ec5ecc692 test:1 "/home/test/docker.sh" 2 seconds ago Up 1 seconds distracted_curran [root@Docker 7]# docker logs e01ec5ecc692 welcome welcome welcome welcome welcome welcome welcome welcome welcome welcome welcome welcome welcome [root@Docker 7]# docker logs --tail 5 e01ec5ecc692 #只显示容器日志的后5行 welcome welcome welcome welcome welcome
Docker命令:pause && unpause
用于将一个或多个容器的进程暂停和恢复
[root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e01ec5ecc692 test:1 "/home/test/docker.sh" 2 minutes ago Up 2 minutes distracted_curran [root@Docker 7]# docker pause e01ec5ecc692 #暂停容器进程 e01ec5ecc692 [root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e01ec5ecc692 test:1 "/home/test/docker.sh" 3 minutes ago Up 3 minutes (Paused) distracted_curran [root@Docker 7]# docker unpause e01ec5ecc692 #恢复容器进程 e01ec5ecc692 [root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e01ec5ecc692 test:1 "/home/test/docker.sh" 3 minutes ago Up 3 minutes distracted_curran
Docker命令:port
用于列出一个容器的端口映射及协议
[root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE test 1 abc35d57c192 10 minutes ago 200MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB [root@Docker 7]# docker run -d -it -p 2222:22 test:1 #启动一个镜像的容器进程 -p 指定本地2222端口映射到容器的22端口 376d22e0957cf8216a9aabb417870384e896934566a20d8858374c0bf6002d9c [root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 376d22e0957c test:1 "/home/test/docker.sh" 29 seconds ago Up 28 seconds 0.0.0.0:2222->22/tcp gallant_sinoussi e01ec5ecc692 test:1 "/home/test/docker.sh" 5 minutes ago Up 5 minutes distracted_curran
Docker命令:rename
给容器进程重命名
[root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 376d22e0957c test:1 "/home/test/docker.sh" About a minute ago Up About a minute 0.0.0.0:2222->22/tcp gallant_sinoussi e01ec5ecc692 test:1 "/home/test/docker.sh" 6 minutes ago Up 6 minutes distracted_curran [root@Docker 7]# docker rename gallant_sinoussi damowang [root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 376d22e0957c test:1 "/home/test/docker.sh" About a minute ago Up About a minute 0.0.0.0:2222->22/tcp damowang e01ec5ecc692 test:1 "/home/test/docker.sh" 6 minutes ago Up 6 minutes distracted_curran
Docker命令:restart
重启一个容器进程
[root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 376d22e0957c test:1 "/home/test/docker.sh" 2 minutes ago Up 2 minutes 0.0.0.0:2222->22/tcp damowang e01ec5ecc692 test:1 "/home/test/docker.sh" 7 minutes ago Up 7 minutes distracted_curran [root@Docker 7]# docker restart 376d22e0957c 376d22e0957c [root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 376d22e0957c test:1 "/home/test/docker.sh" 2 minutes ago Up 3 seconds 0.0.0.0:2222->22/tcp damowang e01ec5ecc692 test:1 "/home/test/docker.sh" 8 minutes ago Up 8 minutes distracted_curran
Docker命令:stats
用于时时输出容器的资源使用情况
[root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 376d22e0957c test:1 "/home/test/docker.sh" 4 minutes ago Up About a minute 0.0.0.0:2222->22/tcp damowang e01ec5ecc692 test:1 "/home/test/docker.sh" 9 minutes ago Up 9 minutes distracted_curran [root@Docker 7]# docker stats 376d22e0957c CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS 376d22e0957c damowang 0.00% 288KiB / 974.6MiB 0.03% 648B / 0B 0B / 0B 2
#--no-tream只输出一次 [root@Docker 7]# docker stats 376d22e0957c --no-stream CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS 376d22e0957c damowang 0.00% 284KiB / 974.6MiB 0.03% 648B / 0B 0B / 0B 2
Docker命令:tag
用于从一个指定的镜像创建另外一个镜像
[root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE test 1 abc35d57c192 18 minutes ago 200MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB [root@Docker 7]# docker tag test:1 test:2 [root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE test 1 abc35d57c192 19 minutes ago 200MB test 2 abc35d57c192 19 minutes ago 200MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB [root@Docker 7]# docker rmi test:2 Untagged: test:2 [root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE test 1 abc35d57c192 19 minutes ago 200MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB [root@Docker 7]# docker tag abc35d57c192 test:2 [root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE test 1 abc35d57c192 19 minutes ago 200MB test 2 abc35d57c192 19 minutes ago 200MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB
Docker命令:top
用于显示指定容器的进程信息
[root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 376d22e0957c test:1 "/home/test/docker.sh" 8 minutes ago Up 6 minutes 0.0.0.0:2222->22/tcp damowang e01ec5ecc692 test:1 "/home/test/docker.sh" 14 minutes ago Up 14 minutes distracted_curran [root@Docker 7]# docker top 376d22e0957c UID PID PPID C STIME TTY TIME CMD root 2644 2629 0 00:37 pts/0 00:00:00 /bin/bash /home/test/docker.sh root 2893 2644 0 00:44 pts/0 00:00:00 sleep 5
Docker命令:update
用于调整一个或多个容器的启动配置
[root@Docker 7]# docker update --help Usage: docker update [OPTIONS] CONTAINER [CONTAINER...] Update configuration of one or more containers Options: --blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0) --cpu-period int Limit CPU CFS (Completely Fair Scheduler) period --cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota --cpu-rt-period int Limit the CPU real-time period in microseconds --cpu-rt-runtime int Limit the CPU real-time runtime in microseconds -c, --cpu-shares int CPU shares (relative weight) --cpus decimal Number of CPUs #调整CPU --cpuset-cpus string CPUs in which to allow execution (0-3, 0,1) --cpuset-mems string MEMs in which to allow execution (0-3, 0,1) --kernel-memory bytes Kernel memory limit -m, --memory bytes Memory limit #调整内存 --memory-reservation bytes Memory soft limit --memory-swap bytes Swap limit equal to memory plus swap: '-1' to enable unlimited swap --restart string Restart policy to apply when a container exits
Docker命令:version and wait
version用于显示docker的版本信息
wait用于捕捉一个或多个容器的退出状态,并返回退出状态码
#显示docker版本信息 [root@Docker 7]# docker version Client: Version: 18.03.1-ce API version: 1.37 Go version: go1.9.5 Git commit: 9ee9f40 Built: Thu Apr 26 07:20:16 2018 OS/Arch: linux/amd64 Experimental: false Orchestrator: swarm Server: Engine: Version: 18.03.1-ce API version: 1.37 (minimum version 1.12) Go version: go1.9.5 Git commit: 9ee9f40 Built: Thu Apr 26 07:23:58 2018 OS/Arch: linux/amd64 Experimental: false #监听容器的退出状态并返回状态码 [root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 376d22e0957c test:1 "/home/test/docker.sh" 12 minutes ago Up 10 minutes 0.0.0.0:2222->22/tcp damowang e01ec5ecc692 test:1 "/home/test/docker.sh" 18 minutes ago Up 18 minutes distracted_curran [root@Docker 7]# docker wait 376d22e0957c #需要在开一个窗口stop这个容器进程再查看 137
Docker命令:login && logout && push
login用于登陆docker hub官方公有仓库
logout用于登出docker hub官方公有仓库
push用于将本地镜像提交到docker hub
由于docker hub国内已经无法注册,因此无法演示上述内容
DockerHub官方公有镜像仓库:https://hub.docker.com/
管理应用程序数据
Volume和Bind Mount
将Docker主机数据挂载到容器
Docker提供三种不同方式将数据从宿主机挂载到容器中:volumes,bind mounts和tmpfs。
- volumes:Docker管理宿主机文件系统的一部分(/var/lib/docker/volumes)
- bind mounts:可以存储在宿主机系统的任意位置
- tmpfs:挂载存储在宿主机系统的内存中,而不会写入宿主机的文件系统
Volume
#创建一个卷 [root@Docker ~]# docker volume create nginx-vol nginx-vol [root@Docker ~]# docker volume ls DRIVER VOLUME NAME local nginx-vol [root@Docker ~]# docker volume inspect nginx-vol [ { "CreatedAt": "2018-07-17T02:16:40+08:00", #创建时间 "Driver": "local", #驱动 "Labels": {}, "Mountpoint": "/var/lib/docker/volumes/nginx-vol/_data", #挂载点 "Name": "nginx-vol", #卷名 "Options": {}, "Scope": "local" } ] #下载一个nginx官方镜像 [root@Docker ~]# docker pull nginx Using default tag: latest latest: Pulling from library/nginx 683abbb4ea60: Pull complete a470862432e2: Pull complete 977375e58a31: Pull complete Digest: sha256:a65beb8c90a08b22a9ff6a219c2f363e16c477b6d610da28fe9cba37c2c3a2ac Status: Downloaded newer image for nginx:latest [root@Docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE test 1 abc35d57c192 2 hours ago 200MB test 2 abc35d57c192 2 hours ago 200MB nginx latest 3c5a05123222 10 days ago 109MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB [root@Docker ~]# docker run -dit --name=nginx-test --mount src=nginx-vol,dst=/usr/share/nginx/html nginx cd5c493d1633074f7fe2485f626bcda38ea96a320e08cbe40368f450445658f8 --name:容器的名字 --mount:挂载 src:源卷的名字 dst:挂载到容器中的路径 [root@Docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cd5c493d1633 nginx "nginx -g 'daemon of…" 8 seconds ago Up 7 seconds 80/tcp nginx-test 376d22e0957c test:1 "/home/test/docker.sh" 2 hours ago Exited (137) 2 hours ago damowang e01ec5ecc692 test:1 "/home/test/docker.sh" 2 hours ago Up 2 hours distracted_curran #向容器中的挂载目录创建文件,查看是否挂载成功 [root@Docker ~]# docker exec nginx-test touch /usr/share/nginx/html/test.txt [root@Docker ~]# docker exec nginx-test ls /usr/share/nginx/html/ 50x.html index.html test.txt #成功写入 [root@docker 7]# ls /var/lib/docker/volumes/nginx-vol/_data/ 50x.html index.html test.txt #成功 #清理容器进程 [root@Docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cd5c493d1633 nginx "nginx -g 'daemon of…" About a minute ago Up About a minute 80/tcp nginx-test 376d22e0957c test:1 "/home/test/docker.sh" 2 hours ago Exited (137) 2 hours ago damowang e01ec5ecc692 test:1 "/home/test/docker.sh" 2 hours ago Up 2 hours distracted_curran [root@Docker ~]# docker stop nginx-test nginx-test [root@Docker ~]# docker rm nginx-test nginx-test [root@Docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 376d22e0957c test:1 "/home/test/docker.sh" 2 hours ago Exited (137) 2 hours ago damowang e01ec5ecc692 test:1 "/home/test/docker.sh" 2 hours ago Up 2 hours distracted_curran [root@Docker ~]# ls /var/lib/docker/volumes/nginx-vol/_data/ 50x.html index.html test.txt #清理容器后,挂载的卷的数据仍旧存在 #重新启动镜像nginx的容器进程 [root@Docker ~]# docker run -dit --name nginx-test -p 88:80 --mount src=nginx-vol,dst=/usr/share/nginx/html nginx ef4c927e1f49e1774525c47fbe483fd8fb0abe7672488bc5ba45ada5d0310cc4 [root@Docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ef4c927e1f49 nginx "nginx -g 'daemon of…" 5 seconds ago Up 4 seconds 0.0.0.0:88->80/tcp nginx-test 376d22e0957c test:1 "/home/test/docker.sh" 2 hours ago Exited (137) 2 hours ago damowang e01ec5ecc692 test:1 "/home/test/docker.sh" 2 hours ago Up 2 hours distracted_curran
docker run的-p参数:指定端口的映射,88:80的意思就是将宿主机88端口的访问映射到容器进程的80端口
通过浏览器访问宿主机的88端口,进而访问容器进程的80端口
#在数据卷nginx-vol里换一个网页在访问 [root@Docker ~]# ls /var/lib/docker/volumes/nginx-vol/_data/ 50x.html index.html test.txt [root@Docker ~]# rm -rf /var/lib/docker/volumes/nginx-vol/_data/* [root@Docker ~]# echo "my name's damowang" > /var/lib/docker/volumes/nginx-vol/_data/index.html
启动一个镜像nginx的进程,让两个nginx的容器进程公用一个数据卷nginx-vol
[root@Docker ~]# docker run -dit --name nginx-test2 -p 89:80 --mount src=nginx-vol,dst=/usr/share/nginx/html nginx 9cedd1d6a884bbd23ea28fa95ff415e4296d6b7a103378463545c85eee468df3 [root@Docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9cedd1d6a884 nginx "nginx -g 'daemon of…" 50 seconds ago Up 49 seconds 0.0.0.0:89->80/tcp nginx-test2 ef4c927e1f49 nginx "nginx -g 'daemon of…" 14 minutes ago Up 14 minutes 0.0.0.0:88->80/tcp nginx-test
访问docker宿主机的89端口
Bind Mounts
[root@Docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9cedd1d6a884 nginx "nginx -g 'daemon of…" 2 minutes ago Up 2 minutes 0.0.0.0:89->80/tcp nginx-test2 ef4c927e1f49 nginx "nginx -g 'daemon of…" 15 minutes ago Up 15 minutes 0.0.0.0:88->80/tcp nginx-test [root@Docker ~]# 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 fa64fffd4db42f82d2e49e6a67d7bc27e4f400ab8705675e7f7564db2e482bb8 [root@Docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fa64fffd4db4 nginx "nginx -g 'daemon of…" 5 seconds ago Up 3 seconds 0.0.0.0:90->80/tcp nginx-test3 9cedd1d6a884 nginx "nginx -g 'daemon of…" 2 minutes ago Up 2 minutes 0.0.0.0:89->80/tcp nginx-test2 ef4c927e1f49 nginx "nginx -g 'daemon of…" 16 minutes ago Up 16 minutes 0.0.0.0:88->80/tcp nginx-test
访问docker宿主机的90端口
bind mounts可以挂载宿主机上的任意目录,而volume先得创建后才能挂载
实战容器部署LNMP网站平台
下载一个wordpress博客
wget https://cn.wordpress.org/wordpress-4.7.4-zh_CN.tar.gz
创建MySQL数据库容器
#下载MySQL5.7版本镜像 [root@Docker ~]# docker pull mysql:5.7 5.7: Pulling from library/mysql 683abbb4ea60: Already exists 0550d17aeefa: Pull complete 7e26605ddd77: Pull complete 9882737bd15f: Pull complete 999c06ab75f6: Pull complete c71d695f9937: Pull complete c38f847c1491: Pull complete 74f9c61f40bf: Pull complete 30b252a90a12: Pull complete 9f92ebb7da55: Pull complete 90303981d276: Pull complete Digest: sha256:1203dfba2600f140b74e375a354b1b801fa1b32d6f80fdee5f155d1e9f38c841 Status: Downloaded newer image for mysql:5.7 [root@Docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 3c5a05123222 10 days ago 109MB mysql 5.7 66bc0f66b7af 2 weeks ago 372MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB #创建一个自定义网络 [root@Docker ~]# docker network create lnmp 2b1f72c8c9d1cda46549081ef807d12925363aa36cf0d2750cdc990c13b31d23 [root@Docker ~]# docker network ls NETWORK ID NAME DRIVER SCOPE a2232bb435d4 bridge bridge local 31ad8236c965 host host local 2b1f72c8c9d1 lnmp bridge local #默认为网桥 4d18e42bc424 none null local #启动MySQL数据库容器 [root@Docker ~]# docker run -dit --name lnmp_mysql --network lnmp -p 3306:3306 --mount src=mysql-vol,dst=/var/lib/mysql -e MYSQL_ROOT_PASSWORD=475541270 mysql:5.7 --character-set-server=utf8 d5f911e7d17415c64695363619917925f9f23b2b17edd1ec2131f7bcce2e9542 [root@Docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d5f911e7d174 mysql:5.7 "docker-entrypoint.s…" 18 seconds ago Up 17 seconds 0.0.0.0:3306->3306/tcp lnmp_mysql fa64fffd4db4 nginx "nginx -g 'daemon of…" 12 minutes ago Up 12 minutes 0.0.0.0:90->80/tcp nginx-test3 9cedd1d6a884 nginx "nginx -g 'daemon of…" 15 minutes ago Up 15 minutes 0.0.0.0:89->80/tcp nginx-test2 ef4c927e1f49 nginx "nginx -g 'daemon of…" 28 minutes ago Up 28 minutes 0.0.0.0:88->80/tcp nginx-test 自定义网络lnmp如果不提前创建的话,在启动容器进程时会报错 如果没有提前pull好mysql:5.6那么容器在启动时会自动下载对应镜像 如果没有提前docker volume create mysql-vol,那么容器启动时会自动创建 #查看容器lnmp_mysql的日志输出 [root@Docker ~]# docker logs lnmp_mysql #查看容器里启动的进程 [root@Docker ~]# docker top lnmp_mysql UID PID PPID C STIME TTY TIME CMD polkitd 5363 5347 0 02:49 pts/0 00:00:00 mysqld --character-set-server=utf8
向容器里的Mysql创建一个库
[root@Docker ~]# docker exec lnmp_mysql sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -e"create database bdk"' mysql: [Warning] Using a password on the command line interface can be insecure.
在docker宿主机上安装mysql的客户端通过端口映射访问容器内的mysql
因为已经将mysql容器的3306端口映射到了docker宿主机的3306,因此我们访问本地即可
#安装mysql客户端 [root@docker ~]# yum -y install mysql #查看本机IP地址 [root@Docker ~]# hostname -I | xargs -n1 | head -1 192.168.94.103 #远程方式连接docker宿主机的3306端口 [root@Docker ~]# mysql -h192.168.94.103 -P3306 -uroot -p475541270 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.22 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | bdk | #已成功创建bdk库 | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)
创建nginx+PHP环境容器
#创建一个网页目录 [root@Docker ~]# mkdir -p /app/damowang [root@Docker ~]# ll -d /app/damowang drwxr-xr-x 2 root root 6 7月 17 03:00 /app/damowang #下载richarvey/nginx-php-fpm镜像 [root@docker ~]# docker pull richarvey/nginx-php-fpm [root@Docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 3c5a05123222 10 days ago 109MB richarvey/nginx-php-fpm latest 26c0e6f09c52 11 days ago 300MB mysql 5.7 66bc0f66b7af 2 weeks ago 372MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB #启动richarvey/nginx-php-fpm镜像的容器 [root@Docker ~]# docker run -dit --name lnmp_web --network lnmp -p 88:80 --mount type=bind,src=/app/damowang,dst=/var/www/html richarvey/nginx-php-fpm 56a0139a59f6ac18520a7edeece8fc340a65801afaa7c8e72a6d17ae8c25deff [root@Docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 56a0139a59f6 richarvey/nginx-php-fpm "docker-php-entrypoi…" 2 minutes ago Up 2 minutes 443/tcp, 9000/tcp, 0.0.0.0:88->80/tcp lnmp_web d5f911e7d174 mysql:5.7 "docker-entrypoint.s…" 18 minutes ago Up 18 minutes 0.0.0.0:3306->3306/tcp lnmp_mysql
解压wordpress到网页目录/app/damowang下
[root@Docker ~]# tar xf wordpress-4.7.4-zh_CN.tar.gz -C /app/damowang/ [root@Docker ~]# cd /app/damowang/ [root@Docker damowang]# ls wordpress [root@Docker damowang]#
博客wordpress访问测试
指定本机ip地址的88端口 如果不通 , 那么就在 ip:88/wordpress/ 后面加上index.php
或者关闭 iptables 和 firewall
关闭selinux