二,安装Docker
2.1--检测安装的前提条件
1.内核
确认服务器安装了3.8或者更高的内核版本。
1 [root@ecs-d68e-0003 data]# uname -a 2 Linux ecs-d68e-0003.novalocal 3.10.0-693.11.1.el7.x86_64 #1 SMP Mon Dec 4 23:52:40 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
2.检查 Device Mapper
Device Mapper作为docker的存储驱动,为docker提供存储能力,确认是否安装。
[root@ecs-d68e-0003 ~]# ls -l /sys/class/misc/device-mapper lrwxrwxrwx 1 root root 0 Jun 15 17:10 /sys/class/misc/device-mapper -> ../../devices/virtual/misc/device-mapper
如果你的没有检测到,需要安装device-mapper
yum install -y device-mapper
还需要加载dm_mod模块
modprobe dm_mod
2.2--安装Docker
[root@ecs-d68e-0003 ~]# yum install -y docker-io ...... ...... Installed: docker.x86_64 2:1.13.1-63.git94f4240.el7.centos Dependency Installed: docker-client.x86_64 2:1.13.1-63.git94f4240.el7.centos docker-common.x86_64 2:1.13.1-63.git94f4240.el7.centos Complete!
安装完成
2.3--docker的守护进程
1.启动
[root@ecs-d68e-0003 ~]# systemctl start docker
2.添加docker的守护进程为开机启动项
[root@ecs-d68e-0003 ~]# systemctl enable docker Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
3.检测Docker是否正确安装
[root@ecs-d68e-0003 ~]# docker info Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 0 Server Version: 1.13.1 Storage Driver: overlay2 Backing Filesystem: extfs Supports d_type: true Native Overlay Diff: true Logging Driver: journald Cgroup Driver: systemd Plugins: Volume: local Network: bridge host macvlan null overlay Swarm: inactive Runtimes: docker-runc runc Default Runtime: docker-runc Init Binary: /usr/libexec/docker/docker-init-current containerd version: (expected: aa8187dbd3b7ad67d8e5e3a15115d3eef43a7ed1) runc version: e9c345b3f906d5dc5e8100b05ce37073a811c74a (expected: 9df8b306d01f59d3a8029be411de015b7304dd8f) init version: 5b117de7f824f3d3825737cf09581645abbe35d4 (expected: 949e6facb77383876aeff8a6944dde66b3089574) Security Options: seccomp WARNING: You're not using the default seccomp profile Profile: /etc/docker/seccomp.json Kernel Version: 3.10.0-693.11.1.el7.x86_64 Operating System: CentOS Linux 7 (Core) OSType: linux Architecture: x86_64 Number of Docker Hooks: 3 CPUs: 4 Total Memory: 7.639 GiB Name: ecs-d68e-0003.novalocal ID: 2UIG:WL75:4G6D:2VFS:YL7I:2OLN:O6IU:TTZQ:DBY2:F5VW:QOJQ:QJFS Docker Root Dir: /var/lib/docker Debug Mode (client): false Debug Mode (server): false Registry: https://index.docker.io/v1/ Experimental: false Insecure Registries: 127.0.0.0/8 Live Restore Enabled: false Registries: docker.io (secure)
4.检测Docker的守护进程是不是正常运行,Docker是以root权限运行它的守护进程的,来处理普通用户无法完成的操作(如挂载文件系统)。dcoker程序 Docker守护进程的客户端程序,同样也需要以root身份运行。用户可以使用docker命令控制Dokcer守护进程。
当Docker的守护进程启动时,守护进程监听/var/run/docker.sock这个套接字文件,来获取来自客户端的Docker请求。
[root@ecs-d68e-0003 ~]# service docker status Redirecting to /bin/systemctl status docker.service ● docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled) Active: active (running) since Fri 2018-06-15 17:18:09 CST; 22min ago Docs: http://docs.docker.com Main PID: 29827 (dockerd-current)
5.查看守护进程的状态
[root@ecs-d68e-0003 ~]# service docker status Redirecting to /bin/systemctl status docker.service ● docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled) Active: active (running) since Fri 2018-06-15 17:18:09 CST; 22min ago Docs: http://docs.docker.com Main PID: 29827 (dockerd-current)
6.启动和停止
service docker start --启动 service docker stop --停止
三、卸载docker
查看docker 安装条目
[root@ecs-d68e-0003 data]# yum list installed|grep docker docker.x86_64 2:1.13.1-63.git94f4240.el7.centos @extras docker-client.x86_64 2:1.13.1-63.git94f4240.el7.centos @extras docker-common.x86_64 2:1.13.1-63.git94f4240.el7.centos @extras
逐条卸载
[root@ecs-d68e-0003 data]# yum -y remove docker.x86_64
[root@ecs-d68e-0003 data]# yum -y remove docker-client.x86_64
[root@ecs-d68e-0003 data]# yum -y remove docker-common.x86_64
再次查看安装条目,删除docker镜像
[root@ecs-d68e-0003 data]# yum list installed|grep docker [root@ecs-d68e-0003 data]# rm -rf /var/lib/docker
四、容器的运行
1.运行第一个容器
[root@ecs-d68e-0003 ~]# docker run -i -t centos /bin/bash Unable to find image 'centos:latest' locally Trying to pull repository docker.io/library/centos ... latest: Pulling from docker.io/library/centos 7dc0dca2b151: Pull complete Digest: sha256:b67d21dfe609ddacf404589e04631d90a342921e81c40aeaf3391f6717fa5322 Status: Downloaded newer image for docker.io/centos:latest [root@92ba035b0a41 /]
解释说明:
告诉Docker执行docker run 命令,并制定了-i 和 -t 参数。
-i参数保证容器中的STDIN(标准输入)是开启的。
-t参数告诉Docker为要创建的容器分配一个伪tty终端。
这样创建的容器才能提供一个交互式的shell。若要创建一个我们能与之交互的容器,而不是一个运行后台服务的容器,则这两个参数已经是最基本的参数了。
执行过程:告诉Docker基于什么镜像来创建容器,在此实例中我们使用centos作为我们的基础镜像,首先Docker会去检测本地是否含有conetos镜像,如果没有,则Docker就会链接官方维护的Dokcer Hub Registry查看Docker Hub中
是否含有该镜像,如果有机会自动下载,保存到宿主机上。最后再容器中运行了/bin/bash命令启动了一个Bash shell。
[root@ecs-d68e-0003 ~]# docker run -i -t centos /bin/bash [root@9162077ba1d7 /]#
2、使用我们的第一个容器
检查容器主机名
[root@9162077ba1d7 /]# hostname 9162077ba1d7
查看容器的/etc/hosts文件
[root@9162077ba1d7 /]# cat /etc/hosts 127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters 172.17.0.2 9162077ba1d7
这样我们就创建了一个容器。再次容器中我们可以做任何我们需要做的事情。
3.容器命名
创建
docker run -i -t --name=zhutong-test1 centos /bin/bash
查看
[root@ecs-d68e-0003 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 04f7272ec99e centos "/bin/bash" 17 seconds ago Exited (130) 4 seconds ago zhutong-test1 9162077ba1d7 centos "/bin/bash" 13 minutes ago Exited (0) 4 minutes ago flamboyant_roentgen
4.重启已经停止的容器
#id重启
[root@ecs-d68e-0003 ~]# docker start 04f7272ec99e 04f7272ec99e
#名字重启 [root@ecs-d68e-0003 ~]# docker start flamboyant_roentgen flamboyant_roentgen
5.附着到容器上
root@ecs-d68e-0003 ~]# docker attach 04f7272ec99e [root@04f7272ec99e /]#
6.创建守护式容器
[root@ecs-d68e-0003 ~]# docker run -i -t -d --name=zhutong-test2 centos /bin/sh -c "while true; do echo hello world;sleep 1; done" 6ff3c6816bb8370d6aede45ee1881f563d8f0eea35e8884ed62ba4c8b137e32e
使用了-d参数,因此Docker会将容器放在后台运行
查看容器中在干什么
[root@ecs-d68e-0003 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6ff3c6816bb8 centos "/bin/sh -c 'while..." 5 seconds ago Up 4 seconds zhutong-test2 [root@ecs-d68e-0003 ~]# docker logs 6ff3c6816bb8 hello world hello world hello world ......
7.容器的日志查看
跟踪守护式容器日志
[root@ecs-d68e-0003 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6ff3c6816bb8 centos "/bin/sh -c 'while..." 5 minutes ago Up 5 minutes zhutong-test2 [root@ecs-d68e-0003 ~]# docker logs -f zhutong-test2 hello world hello world hello world hello world hello world hello world
查看日志的某几条
[root@ecs-d68e-0003 ~]# docker logs --tail 10 zhutong-test2 hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world [root@ecs-d68e-0003 ~]#
追踪最新日志并给日志加时间
[root@ecs-d68e-0003 ~]# docker logs --tail 0 -ft zhutong-test2
8.查看容器内部的进程
[root@ecs-d68e-0003 ~]# docker top zhutong-test2 UID PID PPID C STIME TTY TIME CMD root 2108 2092 0 01:52 pts/1 00:00:00 /bin/sh -c while true; do echo hello world;sleep 1; done root 3110 2108 0 02:06 pts/1 00:00:00 sleep 1 [root@ecs-d68e-0003 ~]#
9.在容器内部运行进程
[root@ecs-d68e-0003 ~]# docker exec -i -t -d zhutong-test3 touch /opt/zbbiex [root@ecs-d68e-0003 ~]# docker attach zhutong-test3 [root@f1c31fd5bbd1 /]# ll /opt/ total 0 -rw-r--r-- 1 root root 0 Jun 19 18:15 zbbiex
这样我们就可在正在运行的容器中进行维护,监控及管理任务
10.停止守护进程
docker stop #停止守护进程 docker stop 容器id或者容器名 #停止正在运行的容器
11.容器的自动重启动
#当容器的推出代码为非0时,容器才会重启 #总是重启,最多5次 docker run -i -t --restart=always --name=zhutong-test4 centos /bin/bash #设置重启次数,最多启动多少次 docker run -i -t --restart=on-failure:3 --name=zhutong-test5 centos /bin/bash
10.深入容器
1.查看容器信息
[root@ecs-d68e-0003 ~]# docker inspect zhutong-test4 [ { "Id": "72bf04eba59a033164fdc56e0a6776cb4c7b86bc20b24d876ee5b611524a4b84", "Created": "2018-06-19T18:33:44.637464354Z", "Path": "/bin/bash", "Args": [], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 3943, "ExitCode": 0, "Error": "", "StartedAt": "2018-06-19T18:34:42.913455172Z", "FinishedAt": "2018-06-19T18:34:42.680082124Z" },
.......
2./var/lib/docker目录
该目录下存放着Docker的镜像,容器以及容器的配置
[root@ecs-d68e-0003 ~]# cd /var/lib/docker/ [root@ecs-d68e-0003 docker]# ll total 36 drwx------ 7 root root 4096 Jun 20 02:33 containers drwx------ 3 root root 4096 Jun 15 17:18 image drwxr-x--- 3 root root 4096 Jun 15 17:18 network drwx------ 20 root root 4096 Jun 20 02:33 overlay2 drwx------ 4 root root 4096 Jun 15 17:18 plugins drwx------ 2 root root 4096 Jun 15 17:18 swarm drwx------ 2 root root 4096 Jun 19 10:19 tmp drwx------ 2 root root 4096 Jun 15 17:18 trust drwx------ 2 root root 4096 Jun 15 17:18 volumes
11、删除容器
1.删除单个退出容器
[root@ecs-d68e-0003 docker]# docker rm zhutong-test1 zhutong-test1
2.删除单个正在运行的容器
[root@ecs-d68e-0003 docker]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 72bf04eba59a centos "/bin/bash" 10 minutes ago Up 9 minutes zhutong-test4 [root@ecs-d68e-0003 docker]# docker rm -f zhutong-test4 zhutong-test4 [root@ecs-d68e-0003 docker]#
3. 删除所有容器
[root@ecs-d68e-0003 docker]# cd containers/ [root@ecs-d68e-0003 containers]# ll total 12 drwx------ 5 root root 4096 Jun 20 02:13 6ff3c6816bb8370d6aede45ee1881f563d8f0eea35e8884ed62ba4c8b137e32e drwx------ 5 root root 4096 Jun 20 01:50 9162077ba1d7652ddc5617232f37e81793e0c22ebb907766dd30dcc83e8eb449 drwx------ 5 root root 4096 Jun 20 02:16 f1c31fd5bbd1714d42d6241e4731d3793e7ae70b2d68aa755ced43d16d114f45 [root@ecs-d68e-0003 containers]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f1c31fd5bbd1 centos "/bin/bash" 31 minutes ago Exited (0) 29 minutes ago zhutong-test3 6ff3c6816bb8 centos "/bin/sh -c 'while..." 53 minutes ago Exited (137) 32 minutes ago zhutong-test2 9162077ba1d7 centos "/bin/bash" About an hour ago Exited (137) 55 minutes ago flamboyant_roentgen [root@ecs-d68e-0003 containers]# docker rm `docker ps -a -q` f1c31fd5bbd1 6ff3c6816bb8 9162077ba1d7 [root@ecs-d68e-0003 containers]# ll total 0 [root@ecs-d68e-0003 containers]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@ecs-d68e-0003 containers]#