-
学习要点归纳:
- 使用容器执行互动和后台终端程序
- 容器和PID命名空间
- 容器配置和输出
- 容器中运行多个程序
- 注入配置到容器
- 持久化容器和容器的生命周期
- 容器的清理
-
环境准备
- win10 + 虚拟机(centos7)
- docker运行在虚拟机的centos7系统上
- 下载docker :
yum install docker
- 修改默认镜像仓库地址为国内地址,防止
docker pull
镜像时报错:vim /etc/docker/daemon.json
{ "registry-mirrors": ["https://registry.docker-cn.com"] }
systemctl daemon-reload
systemctl restart docker
-
笔记
- 建立一个网站的监控器(熟悉常用命令)
docker run -d --name web nginx
docker run -d --name mailer goinaction/ch2_mailer
docker run -it --link web:web -name web_test busybox /bin/sh
wget -O http://web:80
docker run -it --name agent --link web:insideweb --link mailer:insidemailer dockerinaction/ch2_agent
-d
以守护模式运行容器,-it
以shell模式运行容器-it
可以使用exit退出shell,并停止容器;也可以使用ctrl P + ctrl Q的方式只退出shell,不停止容器。docker restart web
docker restart agent
docker restart mailer
docker logs web
docker logs mailer
docker logs agent
-it
可以通过crtl P+ctrl Q退出shell界面- pid命名空间隔离(pid组)
- 获取容器id
- 将容器id写入文件中:
docker create --cidfile /tmp/web.cid nginx
- 通过ps命令来获取容器id:
docker ps --quiet --latest
(获取最新创建的容器id) --quiet
--latest
的缩写为-q
-l
- 通过
--no-trunc
参数获取完整的容器id(若不添加,默认获取的是截断的 12位ID): - 将容器id赋给shell变量:
ID=$(docker ps --quiet --latest)
echo $ID
- 将容器id写入文件中:
- 容器状态
- 运行中
- 暂停中
- 已退出
- 重启中
- 容器依赖(
--link
)- 需要以相反的依赖顺序来启动容器(如容器B依赖容器A,启动顺序A->B)
docker run -d --name web nginx
docker -it --name web_test --link web:web busybox /bin/sh
wget -O http://web:80
docker logs web
查看日志docker exec web_test ps
- 原因:链接的机制是将IP地址注入所依赖的容器中。(将B的IP注入A中,但是若A没运行,docker是不会将B的IP注入到A中的)
- 由于上述原因,在docker中建立循环依赖关系是不可能的
- 需要以相反的依赖顺序来启动容器(如容器B依赖容器A,启动顺序A->B)
- 构建与环境无关的系统
- 只读环境系统
docker run -d --name wp --read-only wordpress:4
docker inspect --format "{{.State.Running}}" wp
docker run -d --name wpdb -e MYSQL_ROOT_PASSWORD=ch2demo mysql:5
docker run -d --name wp2 --link wpdb:mysql -p 80 --read-only wordpress:4
docker run -d --name wp3 --link wpdb:mysql -p 9999:80 -v /run/lock/apache2/ -v /run/apache2/ --read-only wordpress:4
- 报错:
WordPress not found in /var/www/html - copying now... Complete! WordPress has been successfully copied to /var/www/html Wed Dec 9 23:15:21 2015 (21): Fatal Error Unable to create lock file: Bad file descriptor (9)
- 解决:添加
-v /tmp
docker run -d --name wp10 --read-only -v /run/lock/apache2/ -v /run/apache2/ -v /tmp/ --link wpdb:mysql -p 80 wordpress:4
- 原因:
Wordpress的文件锁机制(file locking mechanisms)发生了改变(ver4.2 -> ver4.3)
docker run -d --name wp11 --link wpdb:mysql -p 80 wordpress:4
docker diff wp11
C /run C /run/apache2 A /run/apache2/apache2.pid C /tmp```
- 报错:
- 环境变量注入
docker -e MY_ENV="this is a test" busybox env
- 容器持久化
- 方式1:
使用init进程(PID为1的进程)(init、systemd、supervisord、runit、upstart)
docker run -d -p 80:80 --name lamp-test tutum/lamp
docker exec lamp-test ps
看到supervisord PID为1
- 使用
--restart
+启动脚本的方式
docker run --entrypoint="cat" --restart always wordpress:4 /entrypoint.sh
- 方式1:
- 容器清理
docker ps -a
docker rm <容器ID>
docker rm -f
与docker stop
+docker rm
的区别- docker rm -f 发送的是SIG_KILL信号,立即终止接收过程
- docker stop发送的是SIG_HUP信号,SIG_HUP信号的接收人可以有时间进行最后的退出和清理任务
docker run --rm busybox
(–rm参数,容器进入退出状态就会被自动删除)- 删除所有容器:
docker rm -vf $(docker ps -a -q)
- 只读环境系统
- 建立一个网站的监控器(熟悉常用命令)
-
总结
- 容器可以基于虚拟终端,连接到用户shell或已守护模式的形式运行
- 在默认情况下,每个docker容器都有自己的PID命名空间,隔离每个容器的进程信息
- docker用产生的容器ID简写ID或者其他人性化的名称来标记容器
- 容器的四种状态:运行、暂停、退出、重启
- 用户可以通过输入或在容器创建时给 进程指定环境变量的方式,提供额外的配置
- 使用–read-only标志创建容器时,会将挂载的容器文件系统设置为只读,防止容器被修改
- 容器重启策略,即在容器创建时设置–restart标志,将有助于系统出现故障时进行自动修改
- docker使用docker rm命令清理容器
docker in action 笔记之第二章(在容器中运行软件)
最新推荐文章于 2020-07-06 22:52:50 发布