官方原话: Docker provides a way to run applications securely isolated in a container, packaged with all its dependencies and libraries. linux
Docker Documentation: https://docs.docker.com/get-started/nginx
在很是低的额外开销的状况下,打造一个一致性的环境。 git
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
[root@localhost ~]# uname -r
3.10.0-862.11.6.el7.x86_64
[root@localhost ~]# yum install -y docker Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com Package 2:docker-1.13.1-74.git6e3bb8e.el7.centos.x86_64 already installed and latest version Nothing to do [root@localhost ~]# systemctl start docker [root@localhost ~]# systemctl enable docker [root@localhost ~]# docker version Client: Version: 1.13.1 API version: 1.26 Package version: docker-1.13.1-74.git6e3bb8e.el7.centos.x86_64 Go version: go1.9.4 Git commit: 6e3bb8e/1.13.1 Built: Tue Aug 21 15:23:37 2018 OS/Arch: linux/amd64 Server: Version: 1.13.1 API version: 1.26 (minimum version 1.12) Package version: docker-1.13.1-74.git6e3bb8e.el7.centos.x86_64 Go version: go1.9.4 Git commit: 6e3bb8e/1.13.1 Built: Tue Aug 21 15:23:37 2018 OS/Arch: linux/amd64 Experimental: false [root@localhost ~]# systemctl start docker.service
# 搜索全部centos的docker镜像docker
[root@localhost ~]# docker search centos
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/centos The official build of CentOS. 4642 [OK]
docker.io docker.io/ansible/centos7-ansible Ansible on Centos7 115 [OK]
docker.io docker.io/jdeathe/centos-ssh CentOS-6 6.10 x86_64 / CentOS-7 7.5.1804 x... 99 [OK]
#可使用docker pull命令来从仓库获取所须要的镜像。下面的例子将从Docker Hub仓库下载一个Centos操做系统的镜像。centos
[root@localhost ~]# docker pull centos
Using default tag: latest
Trying to pull repository docker.io/library/centos ...
latest: Pulling from docker.io/library/centos
Digest: sha256:6f6d986d425aeabdc3a02cb61c02abb2e78e57357e92417d6d58332856024faf
Status: Image is up to date for docker.io/centos:latest
[root@localhost ~]# docker run -it centos /bin/bash
# 完成后可使用该镜像建立一个容器
[root@2a1b7476db10 /]# ls
anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
# 镜像的ID惟一标识了镜像,若是ID相同,说明是同一镜像。TAG信息来区分不一样发行版本,若是不指定具体标记,默认使用latest标记信息。bash
[root@2a1b7476db10 /]# exit
exit
# 查看docker镜像
[root@localhost ~]# docker images
REPOSITORY(来自那个仓库) TAG(标签) IMAGE ID(惟一ID) CREATED(建立时间) SIZE(大小)
docker.io/nginx latest 71c43202b8ac 19 hours ago 109 MB
docker.io/centos latest 5182e96772bf 3 weeks ago 200 MB
docker.io/tutum/centos latest 99a633ad346f 2 years ago 297 MB
[root@localhost ~]#
# 若是要移除本地的镜像,可使用docker rmi命令(在删除镜像以前先用docker rm删除依赖于这个镜像的全部容器)。注意docker rm 命令是移除容器。服务器
[root@localhost ~]# docker rmi 99a633ad346f # 删除docker镜像
Untagged: docker.io/tutum/centos:latest
Untagged: docker.io/tutum/centos@sha256:b4de18abfef698f6ae3a4921d6f58edef8fc770c6ee5da7636fa4ea51ab545c5
Deleted: sha256:99a633ad346ff9debae2e18ef28e36da72c4535d936500e497cd34377173d4b6
Deleted: sha256:e72d160abbd5585d403996036763c88e9624c70d9d80d6fb0e1ccb2c49b8a26d
Deleted: sha256:8f201733668a3ce8ce77a0dc8b2d7249cdc70b1022a7fc78650527a6f3ec09b6
Deleted: sha256:8fdcefb6d553194a6881387055c1d4110bc385d3dbf4d51b0aa07f1251e877ef
Deleted: sha256:8f98c28a55c2e54cf8b8815d843cbfaf1495dd0bf601774f27da1cbe80183225
Deleted: sha256:6163e9854471a9b6c817b9514d6b8d1cf7be7684fa0c52d76d42188e28785859
Deleted: sha256:85946201865d0456e3d2db4df8699ecc017f2a8df599a1c0b241d30e8f942ed6
Deleted: sha256:3b6d6efff38bc7552b4e42d78a142460dd122efe3f86a921298e82d60f770c85
Deleted: sha256:28508c96f9bffc44568bdcf1d0cd20a0357a106a18440302ba67ee4c68a3ccad
Deleted: sha256:33b3813290610f3880214f35c32d2175adfcc75500cae48dfdbcbcf31928a1ac
Deleted: sha256:ce3c560f1cfc18c6febc0a8ed4f8330d32df64e52fa0b34b82d42ad35406ac39
Deleted: sha256:cf032be00d9aedaf94363af683f83fda44140490759c1c048584dc9b3dd1f34e
Deleted: sha256:5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef
[root@localhost ~]#
# 若是要导出镜像到本地文件,可使用docker save命令。架构
[root@localhost ~]# docker save centos > /opt/centos.tar.gz # 导出docker镜像至本地
[root@localhost ~]# ls -l /opt/centos.tar.gz
-rw-r--r--. 1 root root 208301056 Aug 30 06:47 /opt/centos.tar.gz
[root@localhost ~]# du -sh /opt/centos.tar.gz
199M /opt/centos.tar.gz
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/nginx latest 71c43202b8ac 19 hours ago 109 MB
docker.io/centos latest 5182e96772bf 3 weeks ago 200 MB
# 可使用docker load从本地文件中导入到本地docker镜像库app
[root@localhost ~]# docker load < /opt/centos.tar.gz # 导入本地镜像到docker镜像库
Loaded image: docker.io/centos:latest
[root@localhost ~]# docker images # 查看镜像导入状况
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/nginx latest 71c43202b8ac 19 hours ago 109 MB
docker.io/centos latest 5182e96772bf 3 weeks ago 200 MB
[root@localhost ~]#
Docker的容器实在过轻量级了,不少时候用户都是随时删除和新建立容器。启动容器有两种方式:运维
# 新建容器并启动
[root@localhost ~]# docker run centos /bin/echo 'Hello,World!' # 这跟在本地直接执行 /bin/echo ‘Hello,World’同样
Hello,World!
[root@localhost ~]# docker run --name mycentos -it centos /bin/bash # 启动一个bash终端,容许用户进行交互。
[root@e930e732fa58 /]#
当利用docker run来建立容器时,Docker在后台运行的标准操做包括:
# 启动已终止的容器
能够利用docker start命令,直接将一个已经终止的容器启动运行。容器的核心为所执行的应用程序,所须要的资源都是应用程序运行所必需的。除此以外,并无其余的资源。能够在伪终端中利用ps和top来查看进程信息。
[root@localhost ~]# docker start mycentos # 启动一个终止的容器 mycentos [root@localhost ~]# docker ps -a # 查看是否启动 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e930e732fa58 centos "/bin/bash" 8 minutes ago Up 8 seconds mycentos 19992e1d2669 centos "/bin/echo Hello,W..." 9 minutes ago Exited (0) 9 minutes ago distracted_borg …… [root@localhost ~]#
# 守护进程运行
若是须要让Docker容器在后台以守护形式运行。此时能够经过添加-d参数来实现。
[root@localhost ~]# docker run -d centos /bin/bash -c 'while true; do echo "Hello,World!"; sleep 1;done'
a017e98a7ce8e8675d077fff1237886f8de8a59ce6b24d4774de009621ce5449
[root@localhost ~]# docker ps # 列出容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a017e98a7ce8 centos "/bin/bash -c 'whi..." 18 seconds ago Up 17 seconds mystifying_mclean
ff3c866aa8c8 docker.io/centos "/bin/bash" 4 hours ago Up 4 hours inspiring_torvalds
[root@localhost ~]# docker logs a017e98a7ce8 # 获取容器输出信息,经过dockerlogs命令
Hello,World!
Hello,World!
Hello,World!
Hello,World!
……
[root@localhost ~]#
# 中止容器
可使用docker stop来终止一个运行中的容器。此外,当Docker容器中指定的应用终结时,容器也自动终止。例如启动一个终端的容器,用户经过exit命令或者ctrl+d来退出终端时,所建立的容器马上终止。
终止状态的容器能够用docker ps -a命令看到,也能够经过docker start ID 命令来启动容器。
[root@localhost ~]# docker ps # 列出容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a017e98a7ce8 centos "/bin/bash -c 'whi..." 3 minutes ago Up 3 minutes mystifying_mclean
ff3c866aa8c8 docker.io/centos "/bin/bash" 4 hours ago Up 4 hours inspiring_torvalds
[root@localhost ~]# docker stop a017e98a7ce8 # 中止指定ID的容器 a017e98a7ce8
[root@localhost ~]# docker ps # 指定ID的容器已经看不到了
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ff3c866aa8c8 docker.io/centos "/bin/bash" 4 hours ago Up 4 hours inspiring_torvalds
[root@localhost ~]#
# 删除容器
[root@localhost ~]# docker ps -a # 查看全部容器当前状态
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
78e4523d92c3 centos "/bin/bash -c 'whi..." About a minute ago Exited (137) 51 seconds ago relaxed_swirles
ff3c866aa8c8 docker.io/centos "/bin/bash" 4 hours ago Up 4 hours inspiring_torvalds
[root@localhost ~]# docker rm `docker ps -a -q` # 删除全部的容器
78e4523d92c3
Error response from daemon: You cannot remove a running container ff3c866aa8c8e342a5418f8de9eaec6f7445a03c420cdc35b1b5a39a9c07e237. Stop the container before attempting removal or use -f [root@localhost ~]# docker ps -a # 查看全部容器当前状态,发现运行中的容器未删除
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ff3c866aa8c8 docker.io/centos "/bin/bash" 4 hours ago Up 4 hours inspiring_torvalds
[root@localhost ~]# docker rm -f ff3c866aa8c8 # 删除正在运行的容器
ff3c866aa8c8
[root@localhost ~]# docker ps -a # 查看全部的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]#
# 进入容器
使用-d参数时,容器启动后会进入后台。某些时候须要进入容器进行操做,有不少种方法,包括使用docker attach命令或nsenter工具等。
一、docker attach是Docker自带的命令:
[root@localhost ~]# docker ps -a # 查看全部的容器当前状态
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
3a67130228df docker.io/centos "/bin/bash -c 'whi..." About a minute ago Up About a minute
[root@localhost ~]# docker attach 3a67130228df # 一直卡在这里,不知道是否是由于没有ssh访问的缘由
因而我从新pull一个带ssh的centos镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/nginx latest 71c43202b8ac 20 hours ago 109 MB
docker.io/centos latest 5182e96772bf 3 weeks ago 200 MB
docker.io/tutum/centos latest 99a633ad346f 2 years ago 297 MB # 就是它!
[root@localhost ~]# docker run -d --name newcentos docker.io/tutum/centos /bin/bash -c 'while true; do echo Hello,Wolrd;sleep 1;done' # 启动容器
5c9d7d40e4d8e6654a4592681d97826558c2e3f30b7c264f8487a8f387dda30e
[root@localhost ~]# docker logs 5c9d7d40e4d8e6654a4592681d97826558c2e3f30b7c264f8487a8f387dda30e # 一直有输出,我放心了
Hello,Wolrd
Hello,Wolrd
Hello,Wolrd
Hello,Wolrd
Hello,Wolrd
Hello,Wolrd
Hello,Wolrd
Hello,Wolrd
[root@localhost ~]# docker ps # 列出容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5c9d7d40e4d8 docker.io/tutum/centos "/bin/bash -c 'whi..." 12 seconds ago Up 11 seconds 22/tcp newcentos
3a67130228df docker.io/centos "/bin/bash -c 'whi..." 14 minutes ago Up 14 minutes zen_golick
[root@localhost ~]# docker attach 5c9d7d40e4d8 # 进入容器
Hello,Wolrd
Hello,Wolrd
Hello,Wolrd
Hello,Wolrd
可是使用attach命令有时候并不方便。当多个窗口同时attach到同一个容器的时候,全部的窗口都会同步显示,当某个窗口因命令阻塞时,其余窗口也没法执行操做了。
二、nsenter命令
nsenter能够访问另外一个进程的名字空间。nsenter须要有root权限。
[root@localhost ~]# docker ps # 列出容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS 5c9d7d40e4d8 docker.io/tutum/centos "/bin/bash -c 'whi..." 6 minutes ago Up 6 minutes 22/tcp 3a67130228df docker.io/centos "/bin/bash -c 'whi..." 20 minutes ago Up 20 minutes [root@localhost ~]# yum install -y util-linux # 安装包中有须要用到的nsenter Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com Package util-linux-2.23.2-52.el7_5.1.x86_64 already installed and latest version Nothing to do [root@localhost ~]# docker inspect --format "{{.State.Pid}}" 5c9d7d40e4d8 #找到容器的第一个进程PID 13333 [root@localhost ~]# nsenter -t 13333 -u -i -n -p # 经过这个PID链接到容器
[root@5c9d7d40e4d8 ~]# # 进入容器中
方便起见,将这2条命令脚本化
[root@192.168.0.155]$~:> cat in.sh
#!/bin/bash
# zuoyang@jd.com
PID=$(docker inspect --format “{{.State.Pid}}” $1)
nsenter -t $PID -u -i -n -p