容器自动化专题(二):docker基础(下)
云计算领域专题式教学
--私人课件,不公开,不出版,禁止传播
想做好运维工作,人先要学会勤快;
居安而思危,勤记而补拙,方可不断提高;
别人资料不论你用着再如何爽那也是别人的;
自己总结东西是你自身特有的一种思想与理念的展现;
精髓不是看出来的,精髓是记出来的;
请同学们在学习的过程中养成好的学习习惯;
勤于实践,抛弃教案,勤于动手,整理文档。
五,网络管理
5.1 容器网络模式
- Docker支持5种网络模式
- bridge
- 默认网络,Docker启动后默认创建一个docker0网桥,默认创建的容器也是添加到这个网桥中
- host
- 容器不会获得一个独立的network namespace,而是与宿主机共用一个
- none
- 获取独立的network namespace,但不为容器进行任何网络配置
- container
- 与指定的容器使用同一个network namespace,网卡配置也都是相同的
- 自定义
- 自定义网桥,默认与bridge网络一样
- bridge
5.1.1 bridge网络类型
#安装bridge管理工具
[root@docker ~]# yum -y install bridge-utils
#查看网桥状态
[root@docker ~]# brctl show
bridge name bridge id STP enabled interfaces
br-b02d86520223 8000.02427fd4e96d no veth3c0da5f #网桥br-br0绑定了两个虚拟网卡
vethcd34854
docker0 8000.02424cab4d14 no veth0d15221 #网桥docker0绑定了一个虚拟网卡
#查看网络类型
[root@docker ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
29e8c90dbd80 bridge bridge local #两个网桥类型的网络
f11e598312bd host host local
b02d86520223 lnmp bridge local #两个网桥类型的网络
3978eff69b11 none null local
#查看容器进程
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
274cd088c4ad centos "/bin/bash" 14 minutes ago Up 14 minutes test
1ab26792a73c richarvey/nginx-php-fpm "docker-php-entrypoi…" 2 days ago Up 2 days 443/tcp, 9000/tcp, 0.0.0.0:88->80/tcp lnmp_web
07bd252ec637 mysql:5.6 "docker-entrypoint.s…" 2 days ago Up 2 days 0.0.0.0:3306->3306/tcp lnmp_mysql
#查看容器lnmp_mysql的网络信息
[root@docker ~]# docker inspect lnmp_mysql | grep -A 15 "Networks"
"Networks": {
"lnmp": { #网络类型lnmp
"IPAMConfig": null,
"Links": null,
"Aliases": [
"07bd252ec637"
],
"NetworkID": "b02d8652022382f21780ee4935f472689883b64389ae120174268de57ec03e4e",
"EndpointID": "6059606168f72d1561e4ce1d345fdc8dafed7b3956f9b2a392f29635c4001b7c",
"Gateway": "172.18.0.1", #网关172.18.0.1,这就是网桥br-b02d86520223
"IPAddress": "172.18.0.2", #容器IP172.18.0.2
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:12:00:02",
#查看容器lnmp_web的网络信息
[root@docker ~]# docker inspect lnmp_web | grep -A 15 "Networks"
"Networks": {
"lnmp": { #网络类型lnmp
"IPAMConfig": null,
"Links": null,
"Aliases": [
"1ab26792a73c"
],
"NetworkID": "b02d8652022382f21780ee4935f472689883b64389ae120174268de57ec03e4e",
"EndpointID": "778d44313652bce9af6f09c3f67d56946eca2c6b7cf7dc8b9c79046e7874842a",
"Gateway": "172.18.0.1", #网关172.18.0.1,这就是网桥br-b02d86520223
"IPAddress": "172.18.0.3", #容器IP172.18.0.3
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:12:00:03",
#查看容器test的网络信息
[root@docker ~]# docker inspect test | grep -A 15 "Networks"
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "29e8c90dbd80c38e964c4a6055456fb1718f499f03a1ff81af0a3c2643231de5",
"EndpointID": "cacf4aea37a8d12bae2358fa682da1e3c3b6bb85947a88d8242fbf1bff3d3d86",
"Gateway": "172.17.0.1", #网关172.17.0.1,这就是网桥docker0
"IPAddress": "172.17.0.2", #容器IP172.17.0.2
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
5.1.2 host网络类型
#启动一个网络类型为host的容器
[root@docker ~]# docker run -dit --name test2 --network host centos:latest /bin/bash
156dbada7627542fd0ab7134cec270466bcef5180feeec44343821d71cc6ebaf
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
156dbada7627 centos:latest "/bin/bash" 3 seconds ago Up 3 seconds test2
274cd088c4ad centos "/bin/bash" 2 hours ago Up 2 hours test
1ab26792a73c richarvey/nginx-php-fpm "docker-php-entrypoi…" 3 days ago Up 3 days 443/tcp, 9000/tcp, 0.0.0.0:88->80/tcp lnmp_web
07bd252ec637 mysql:5.6 "docker-entrypoint.s…" 3 days ago Up 3 days 0.0.0.0:3306->3306/tcp lnmp_mysql
#查看容器的ip
[root@docker ~]# docker exec test2 hostname -I #这就是网络类型为host的容器,ip地址和docker宿主机完全一样
192.168.200.142 172.17.0.1 172.18.0.1
[root@docker ~]# docker exec test hostname -I #网桥类型容器
172.17.0.2
[root@docker ~]# docker exec lnmp_mysql hostname -i #网桥类型容器
172.18.0.2
[root@docker ~]# docker exec lnmp_web hostname -i #网桥类型容器
172.18.0.3
5.1.3 none网络类型(用于建立与宿主机的桥接模式)
#启动一个网络类型为none的容器
[root@docker ~]# docker run -dit --name test3 --net none centos:latest
cddf4c8888d71df9224b6455ae426fe33470e219c5c755252875da7a20c4527f
#查看容器IP地址
[root@docker ~]# docker exec test3 hostname -I
查询后发现,什么IP地址都没有,是的,none类型就是暂时不给容器指定网卡。
5.1.4 container网络类型
指定新容器使用指定容器的网卡
#启动一个容器,网络类型container,使用test容器的网卡
[root@docker ~]# docker run -dit --name test4 --net container:test centos:latest /bin/bash
d0100ebdadde9733d5c2d0fd8f1a8017d1c74c4323408cd11ac593c0f7d7f42f
[root@docker ~]# docker inspect test | grep -A 15 "Networks"
\ "Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "29e8c90dbd80c38e964c4a6055456fb1718f499f03a1ff81af0a3c2643231de5",
"EndpointID": "cacf4aea37a8d12bae2358fa682da1e3c3b6bb85947a88d8242fbf1bff3d3d86",
"Gateway": "172.17.0.1", #test容器的网关
"IPAddress": "172.17.0.2", #test容器模式网桥
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
[root@docker ~]# docker inspect test4 | grep -A 15 "Networks"
"Networks": {} #test4容器并没有自己的网络设置
}
}
]
[root@docker ~]# docker exec test4 hostname -I #test4没有网络设置却有IP地址和test容器完全一样
172.17.0.2
5.2 桥接宿主机网络与配置固定IP地址
5.2.1 建立网桥桥接到宿主机网络
#构建一个永久生效的网桥br0
[root@docker network-scripts]# cat ifcfg-ens32
TYPE=Ethernet
BOOTPROTO=dhcp
NAME=ens32
DEVICE=ens32
ONBOOT=yes
BRIDGE=br0
[root@docker network-scripts]# cat ifcfg-br0
TYPE=Bridge
BOOTPROTO=static
DEVICE=br0
ONBOOT=yes
IPADDR=192.168.200.142
NETMASK=255.255.255.0
GATEWAY=192.168.200.2
DNS1=192.168.200.2
[root@docker network-scripts]# service network restart
#查看网卡IP
[root@docker network-scripts]# ifconfig ens32
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:93:37:0b txqueuelen 1000 (Ethernet) #ens32网卡已经没有IP地址了
RX packets 626902 bytes 599726150 (571.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 570556 bytes 1022355519 (974.9 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@docker network-scripts]# ifconfig br0
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.200.142 netmask 255.255.255.0 broadcast 192.168.200.255 #网桥br0代替了ens32
inet6 fe80::f82d:6dff:fed3:a9bb prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:93:37:0b txqueuelen 1000 (Ethernet)
RX packets 306 bytes 28092 (27.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 141 bytes 19806 (19.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@docker network-scripts]# brctl show
bridge name bridge id STP enabled interfaces
br-b02d86520223 8000.02427fd4e96d no veth3c0da5f
vethcd34854
br0 8000.000c2993370b no ens32 #网桥br0,桥接在了真实的物理网卡ens32上
docker0 8000.02424cab4d14 no veth0d15221
5.2.2 通过pipework工具配置容器固定IP
pipework工具下载地址:https://github.com/jpetazzo/pipework.git
#解压安装pipework工具
[root@docker ~]# yum -y install unzip
[root@docker ~]# unzip pipework-master.zip
Archive: pipework-master.zip
ae42f1b5fef82b3bc23fe93c95c345e7af65fef3
creating: pipework-master/
extracting: pipework-master/.gitignore
inflating: pipework-master/LICENSE
inflating: pipework-master/README.md
inflating: pipework-master/docker-compose.yml
creating: pipework-master/doctoc/
inflating: pipework-master/doctoc/Dockerfile
inflating: pipework-master/pipework
inflating: pipework-master/pipework.spec
[root@docker ~]# mv pipework-master /usr/local/
[root@docker ~]# ln -s /usr/local/pipework-master/pipework /usr/local/bin/
[root@docker ~]# which pipework
/usr/local/bin/pipework
#建立网络类型为none的容器,并通过pipework配置固定ip地址
[root@docker ~]# docker run -dit --name test5 --net none centos:latest /bin/bash
5b06b180ce8477eb21959facde5c48f1d3670396baa5696ad319d3052b610a4b
[root@docker ~]# pipework br0 test5 192.168.200.199/24@192.168.200.2 #设置容器固定IP为192.168.200.199网关192.168.200.2
[root@docker ~]# docker exec test5 hostname -I #有IP了
192.168.200.199
[root@docker ~]# ping 192.168.200.199 #宿主机ping能通
PING 192.168.200.199 (192.168.200.199) 56(84) bytes of data.
64 bytes from 192.168.200.199: icmp_seq=1 ttl=64 time=0.076 ms
^C
--- 192.168.200.199 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.076/0.076/0.076/0.000 ms
[root@docker ~]# docker exec -it test5 /bin/bash #进入容器
[root@5b06b180ce84 /]# ping www.baidu.com #能连接外网
PING www.a.shifen.com (61.135.169.125) 56(84) bytes of data.
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=1 ttl=128 time=5.73 ms
^C
--- www.a.shifen.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 5.733/5.733/5.733/0.000 ms
通过windows宿主机ping虚拟机中的容器进程IP进行测试
综上,外部机器访问虚拟机中的容器进程也可以联通了
六,Docker的镜像制作
6.1 Dockerfile常用指令介绍
指令 | 描述 |
---|---|
FROM | 构建的新镜像是基于哪个镜像。例如:FROM centos:6 |
MAINTAINER | 镜像维护者姓名或邮箱地址。例如:MAINTAINER Mr.chen |
RUN | 构建镜像时运行的Shell命令。例如:RUN ["yum","install","httpd"] |
或者RUN yum install httpd | |
CMD | 运行容器时执行的Shell命令(可以被运行时传递的参数覆盖)。例如:CMD ["-c","/start.sh"] |
或者CMD ["/usr/sbin/sshd","-D"]或者CMD /usr/sbin/sshd -D | |
EXPOSE | 声明容器运行的服务端口。例如:EXPOSE 80 443 |
ENV | 设置容器内环境变量。例如:ENV MYSQL_ROOT_PASSWORD 123456 |
ADD | 拷贝文件或目录到镜像(可以自动解压缩或者下载) |
例如:ADD ["src","dest"]或者ADD https://xxx.com/html.tar.gz /var/www/html | |
或者:ADD html.tar.gz /var/www/html | |
COPY | 拷贝文件或目录到镜像(不能自动解压缩)。例如:COPY ./start.sh /start.sh |
ENTRYPOINT | 运行容器时执行的Shell命令(不能被运行时传递的参数覆盖)。例如:ENTRYPOINT ["/bin/bash","-c","/start.sh"] |
或者ENTRYPOINT /bin/bash -c "/start.sh" | |
VOLUME | 指定容器挂载点到宿主机自动生成的目录或其他容器 |
例如:VOLUME ["/var/lib/mysql"] | |
USER | 为RUN,CMD和ENTRYPOINT执行命令指定运行用户 |
例如:USER Mr_chen | |
WORKDIR | 为RUN,CMD,ENTRYPOINT,COPY和ADD设置工作目录(指定进入容器中默认被切换的目录)。 |
例如:WORKDIR /data | |
HEALTHCHECK | 健康检查。例如:HEALTHCHECK --interval=5m --timeout=3s --retries=3 |
CMD curl -f http://localhost/ || exit 1 | |
ARG | 在构建镜像时指定一些参数。例如:ARG user |
6.2 利用Dockerfile编写简单的nginxWeb镜像
#首先我们开启ipv4转发
[root@docker ~]# echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
[root@docker ~]# sysctl -p
net.ipv4.ip_forward = 1
#创建nginx的镜像目录
[root@docker ~]# mkdir -p dockerfile/lib/centos/nginx
[root@docker ~]# cd dockerfile/lib/centos/nginx
#准备Dockerfile文件
[root@docker nginx]# cat Dockerfile
FROM centos:7
MAINTAINER www.yunjisuan.com
RUN yum install -y gcc gcc-c++ make openssl-devel pcre-devel
ADD nginx-1.12.1.tar.gz /tmp
RUN cd /tmp/nginx-1.12.1 && \
./configure --prefix=/usr/local/nginx && \
make -j 2 && \
make install
RUN rm -rf /tmp/nginx-1.12.1* && yum clean all
COPY nginx.conf /usr/local/nginx/conf
WORKDIR /usr/local/nginx
EXPOSE 80
CMD ["./sbin/nginx", "-g", "daemon off;"]
#将事先准备好的nginx-1.12.1.tar.gz和nginx.conf配置文件拷贝到当前目录下
[root@docker nginx]# ls
Dockerfile nginx-1.12.1.tar.gz nginx.conf
#build基于nginx的docker镜像
[root@docker nginx]# docker build -t nginx:1 .
6.3 构建PHP网站平台镜像实战
现在我们创建一个PHP的docker镜像
#创建php的镜像目录
[root@docker php]# mkdir -p /root/dockerfile/lib/centos/php
[root@docker php]# cd /root/dockerfile/lib/centos/php
#准备Dockerfile文件
[root@docker ~]# cat Dockerfile
FROM centos:7
MAINTAINER www.yunjisuan.com
RUN yum install -y gcc gcc-c++ make gd-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel openssl-devel
ADD php-5.6.31.tar.gz /tmp
RUN cd /tmp/php-5.6.31 && \
./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-mysql --with-mysqli \
--with-openssl --with-zlib --with-curl --with-gd \
--with-jpeg-dir --with-png-dir --with-iconv \
--enable-fpm --enable-zip --enable-mbstring && \
make -j 4 && \
make install && \
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf && \
sed -i "s#127.0.0.1#0.0.0.0#" /usr/local/php/etc/php-fpm.conf && \
sed -i "21a \daemonize = no" /usr/local/php/etc/php-fpm.conf
COPY php.ini /usr/local/php/etc
RUN rm -rf /tmp/php-5.6.31* && yum -y clean all
WORKDIR /usr/local/php
EXPOSE 9000
CMD ["./sbin/php-fpm","-c","/usr/local/php/etc/php-fpm.conf"]
#将事先准备好的php.ini文件及php-5.6.31.tar.gz拷贝到当前目录下
[root@docker php]# ls
Dockerfile php-5.6.31.tar.gz php.ini
#build基于php的docker镜像
[root@docker php]# docker build -t php:1 .
[root@docker php]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
php 1 b32db03ee974 9 minutes ago 965MB
richarvey/nginx-php-fpm latest 26c0e6f09c52 6 days ago 300MB
nginx latest 649dcb69b782 7 days ago 109MB
mysql 5.6 97fdbdd65c6a 2 weeks ago 256MB
centos 7 49f7960eb7e4 5 weeks ago 200MB
centos latest 49f7960eb7e4 5 weeks ago 200MB
6.4 创建nginx-php网络环境,并启动容器
#创建一个叫做lnmp的网络
[root@docker nginx]# docker network create lnmp
[root@docker nginx]# docker network ls
NETWORK ID NAME DRIVER SCOPE
29e8c90dbd80 bridge bridge local
f11e598312bd host host local
b02d86520223 lnmp bridge local
3978eff69b11 none null local
#创建nginx-php网页挂载目录
[root@docker ~]# mkdir -p /app/wwwroot
#启动php容器
[root@docker nginx]# docker run -dit --name lnmp_php --network lnmp --mount type=bind,src=/app/wwwroot/,dst=/usr/local/nginx/html php:1
a58142f8e7a219f4cfdbb5d919fdd66a9a5b25b938b7b37db5af3bb4ccb47030
[root@docker nginx]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a58142f8e7a2 php:1 "./sbin/php-fpm -c /…" 4 seconds ago Up 4 seconds 9000/tcp lnmp_php
[root@docker nginx]# docker exec lnmp_php hostname -I
172.18.0.2
#启动nginx容器
[root@docker ~]# docker run -dit --name lnmp_nginx -p 888:80 --network lnmp --mount type=bind,src=/app/wwwroot,dst=/usr/local/nginx/html nginx:1
c9cc03248aee776185ca6c9f2d094e4958f1dbd1e5e6485c51204acdcb6611cf
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c9cc03248aee nginx:1 "./sbin/nginx -g 'da…" 3 seconds ago Up 2 seconds 80/tcp lnmp_nginx
078d85b7b76c php:1 "./sbin/php-fpm -c /…" 9 minutes ago Up 9 minutes 9000/tcp lnmp_php
[root@docker ~]# docker exec lnmp_nginx hostname -I
172.18.0.3
#创建测试页面php.info
[root@docker wwwroot]# echo "<?php phpinfo();?>" > /app/wwwroot/index.php
最后通过浏览器进行访问测试
6.5 docke基础阶段作业
- :在nginx-php镜像的基础上构建mysql:1的镜像,并启动lnmp_mysql容器进程
- :实现wordpress博客的部署
6.6 构建JAVA网站环境镜像实战
#创建java镜像构建存储目录
[root@docker ~]# mkdir -p dockerfile/lib/centos/tomcat
[root@docker ~]# cd dockerfile/lib/centos/tomcat
#将所需软件包拷贝到当前目录下
[root@docker tomcat]# ls
apache-tomcat-8.0.46.tar.gz Dockerfile jdk-8u45-linux-x64.tar.gz server.xml
#筹备Dockerfile
[root@docker tomcat]# cat Dockerfile
FROM centos:7
MAINTAINER www.yunjisuan.com
ADD jdk-8u45-linux-x64.tar.gz /usr/local
ENV JAVA_HOME /usr/local/jdk1.8.0_45
ADD apache-tomcat-8.0.46.tar.gz /usr/local
COPY server.xml /usr/local/apache-tomcat-8.0.46/conf
RUN rm -f /usr/local/apache*.tar.gz
WORKDIR /usr/local/apache-tomcat-8.0.46
EXPOSE 8080
ENTRYPOINT ["./bin/catalina.sh", "run"]
#build基于tomcat的镜像
[root@docker tomcat]# docker build -t tomcat:1 .
#创建网页挂载目录
[root@docker tomcat]# mkdir -p /app/webapps
#启动tomcat的容器进程
[root@docker tomcat]# docker run -dit --name=tomcat -p 8080:8080 --mount type=bind,src=/app/webapps/,dst=/usr/local/apache-tomcat-8.0.46/webapps tomcat:1
5ca13d17a24bd390f1eaaf5e7e4ab33e299eeb55b3719d84e15702d7dbee9557
#查看容器进程
[root@docker tomcat]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5ca13d17a24b tomcat:1 "./bin/catalina.sh r…" 34 seconds ago Up 34 seconds 0.0.0.0:8080->8080/tcp tomcat
afb646218459 nginx:1 "./sbin/nginx -g 'da…" 36 minutes ago Up 36 minutes 0.0.0.0:888->80/tcp lnmp_nginx
078d85b7b76c php:1 "./sbin/php-fpm -c /…" About an hour ago Up About an hour 9000/tcp lnmp_php
#创建网站测试页
[root@docker tomcat]# mkdir -p /app/webapps/ROOT
[root@docker tomcat]# echo "welcome to yunjisuan" >> /app/webapps/ROOT/index.html
然后我们用浏览器访问8080端口
特别提示:
- tomcat有三种启动方式:
- 直接启动./startup.sh
- 作为服务启动 nohup ./startup.sh &
- 控制台动态输出方式启动./catalina.sh run动态地显示tomcat后台的控制台输出信息,Ctrl+C后退出并关闭服务
- 构建镜像的Dockerfile里的最后一步启动的进程不能是后台模式,否则容器直接退出。因此,nginxWeb镜像构建时,用
CMD ["./sbin/nginx", "-g", "daemon off;"]
方式进行启动
七,Docker企业实战案例一:整套项目一键打包部署
7.1 Docker Compose用法
7.1.1 介绍
Compose是一个定义和管理多容器的工具,使用Python语言编写。使用Compose配置文件描述多个容器应用的架构,比如使用什么镜像,数据卷,网络,映射端口等;然后一条命令管理所有服务,比如启动,停止,重启等。
7.1.2 安装
下载docker-compose-linux-x86_64.zip
[root@docker ~]# ll docker-compose-linux-x86_64.zip
-rw-r--r-- 1 root root 8733389 7月 13 22:54 docker-compose-linux-x86_64.zip
[root@docker ~]# which unzip
/usr/bin/unzip
[root@docker ~]# unzip docker-compose-linux-x86_64.zip
Archive: docker-compose-linux-x86_64.zip
inflating: docker-compose
[root@docker ~]# ll docker-compose
-rw-r--r-- 1 root root 8858496 8月 31 2017 docker-compose
[root@docker ~]# chmod +x docker-compose
[root@docker ~]# mv docker-compose /usr/bin/
[root@docker ~]# which docker-compose
/usr/bin/docker-compose
7.1.3 YAML文件格式及编写注意事项
YAML是一种标记语言很直观的数据序列化格式,可读性高。类似于XML数据描述语言,语法比XML简单的很多。
YAML数据结构通过缩进来表示,连续的项目通过减号来表示,键值对用冒号分隔,数组用中括号括起来,hash用花括号括起来。
- YAML文件格式注意事项:
- 不支持制表符tab键缩进,需要使用空格缩进
- 通常开头缩进2个空格
- 字符后缩进1个空格,如冒号,逗号,横杆
- 用井号注释
- 如果包含特殊字符用单引号引起来
- 布尔值(true,false,yes,no,on,off)必须用引号括起来,这样分析器会将他们解释为字符串。
- 配置文件常用字段
字段 | 描述 |
---|---|
build | 下级字段dockerfile:指定Dockerfile文件名 |
下级字段context;构建镜像上下文路径 | |
image | 指定镜像 |
command | 执行命令,覆盖默认命令 |
container_name | 指定容器名称 |
deploy | 指定部署和运行服务相关配置,只能在Swarm模式使用 |
environment | 添加环境变量 |
networks | 加入网络,引用顶级networks下条目 |
ports | 暴露端口,与-p相同,但端口不能低于60 |
volumes | 挂载宿主机路径或命名卷。如果是命名卷在顶级volumes定义卷名称 |
restart | 重启策略,默认no,always | on-failure | unless-stopped |
hostname | 容器主机名 |
- 常用命令
字段 | 描述 |
---|---|
build | 重新构建服务 |
ps | 列出容器 |
up | 创建和启动容器 |
exec | 在容器里执行命令 |
scale | 指定一个服务容器启动数量 |
top | 显示容器进程 |
logs | 查看容器输出 |
down | 删除容器,网络,数据卷和镜像 |
stop/start/restart | 停止/启动/重启服务 |
- 例如:
[root@docker ~]# cat docker-compose.yml
version: '3'
services:
nginx:
hostname: nginx
build:
context: ./nginx
dockerfile: Dockerfile
ports:
- 81:80
networks:
- lnmp
volumes:
- ./wwwroot:/usr/local/nginx/html
php:
hostname: php
build:
context: ./php
dockerfile: Dockerfile
networks:
- lnmp
volumes:
- ./wwwroot:/usr/local/nginx/html
mysql:
hostname: mysql
image: mysql:5.6
ports:
- 3306:3306
networks:
- lnmp
volumes:
- ./mysql/conf:/etc/mysql/conf.d
- ./mysql/data:/var/lib/mysql
command: --character-set-server=utf8
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: wordpress
MYSQL_USER: user
MYSQL_PASSWORD: user123
networks:
lnmp:
7.2 一键部署LNMP网站平台实战
#查看compose_lnmp包
[root@docker ~]# tree compose_lnmp/
compose_lnmp/
├── docker-compose.yml #docker-compose启动接口文件
├── mysql
│ ├── conf
│ │ └── my.cnf #mysql配置文件
│ └── data #待挂载mysql数据目录
├── nginx
│ ├── Dockerfile #自定义nginx的Docker镜像配置文件
│ ├── nginx-1.12.1.tar.gz #源码包
│ └── nginx.conf #nginx配置文件
├── php
│ ├── Dockerfile #自定义php的Docker镜像配置文件
│ ├── php-5.6.31.tar.gz #源码包
│ └── php.ini #php解析器配置文件
└── wwwroot #nginx容器和php容器待挂载的网页目录
└── index.php #网页测试文件
6 directories, 9 files
#一键部署LNMP
[root@docker compose_lnmp]# docker-compose -f docker-compose.yml up
浏览器访问:
#一键查看所有部署的容器进程
[root@docker compose_lnmp]# docker-compose -f docker-compose.yml ps
Name Command State Ports
-------------------------------------------------------------------------------------
composelnmp_mysql_1 docker-entrypoint.sh --cha ... Up 0.0.0.0:3306->3306/tcp
composelnmp_nginx_1 ./sbin/nginx -g daemon off; Up 0.0.0.0:81->80/tcp
composelnmp_php_1 ./sbin/php-fpm -c /usr/loc ... Up 9000/tcp
#一键终止所有部署的容器进程
[root@docker compose_lnmp]# docker-compose -f docker-compose.yml stop
Stopping composelnmp_nginx_1 ... done
Stopping composelnmp_php_1 ... done
Stopping composelnmp_mysql_1 ... done
#一键查看所有部署的容器进程
[root@docker compose_lnmp]# docker-compose -f docker-compose.yml ps
Name Command State Ports
---------------------------------------------------------------------
composelnmp_mysql_1 docker-entrypoint.sh --cha ... Exit 0
composelnmp_nginx_1 ./sbin/nginx -g daemon off; Exit 0
composelnmp_php_1 ./sbin/php-fpm -c /usr/loc ... Exit 0
#一键清理所有部署的容器进程
[root@docker compose_lnmp]# docker-compose -f docker-compose.yml down
Removing composelnmp_nginx_1 ... done
Removing composelnmp_php_1 ... done
Removing composelnmp_mysql_1 ... done
Removing network composelnmp_lnmp
[root@docker compose_lnmp]# docker-compose -f docker-compose.yml ps
Name Command State Ports
------------------------------
7.3 一键部署Nginx反向代理Tomcat集群实战
#查看compose_nginx_tomcat包
[root@docker ~]# tree compose_nginx_tomcat/
compose_nginx_tomcat/
├── docker-compose.yml
├── mysql
│ ├── conf
│ │ └── my.cnf #mysql的配置文件
│ └── data #待挂载mysql数据目录
├── nginx
│ ├── Dockerfile #自定义镜像配置文件
│ ├── nginx-1.12.1.tar.gz #源码包
│ └── nginx.conf #nginx配置文件
├── tomcat
│ ├── apache-tomcat-8.0.46.tar.gz #源码包
│ ├── Dockerfile #自定义镜像配置文件
│ └── server.xml #tomcat配置文件
└── webapps
└── ROOT
└── index.html #网页测试文件
7 directories, 9 files
#查看docker-compose的启动接口文件
[root@docker ~]# cat compose_nginx_tomcat/docker-compose.yml
version: '3'
services:
nginx:
hostname: nginx
build:
context: ./nginx
dockerfile: Dockerfile
ports:
- 82:80
networks:
- lnmt
volumes:
- ./webapps:/opt/webapps
tomcat01:
hostname: tomcat01
build: ./tomcat
networks:
- lnmt
volumes:
- /usr/local/jdk1.8.0_45:/usr/local/jdk1.8.0_45
- ./webapps:/usr/local/apache-tomcat-8.0.46/webapps
tomcat02:
hostname: tomcat02
build: ./tomcat
networks:
- lnmt
volumes:
- /usr/local/jdk1.8.0_45:/usr/local/jdk1.8.0_45 #docker宿主机的java环境被挂载了!
- ./webapps:/usr/local/apache-tomcat-8.0.46/webapps
mysql:
hostname: mysql
image: mysql:5.6
ports:
- 3307:3306
networks:
- lnmt
volumes:
- ./mysql/conf:/etc/mysql/conf.d
- ./mysql/data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: db
MYSQL_USER: user
MYSQL_PASSWORD: user123
networks:
lnmt:
#由于tomcat容器进程需要挂载docker宿主机本地的java环境
#所以在docker宿主机本地安装jdk-8u45-linux-x64.tar.gz
[root@docker ~]# ll jdk-8u45-linux-x64.tar.gz
-rw-r--r-- 1 root root 173271626 7月 14 01:38 jdk-8u45-linux-x64.tar.gz
[root@docker ~]# tar xf jdk-8u45-linux-x64.tar.gz -C /usr/local
[root@docker ~]# ll -d /usr/local/jdk1.8.0_45
drwxr-xr-x 8 10 143 255 4月 11 2015 /usr/local/jdk1.8.0_45
#一键部署ngxin+tomcat反向代理集群
[root@docker compose_nginx_tomcat]# pwd
/root/compose_nginx_tomcat
[root@docker compose_nginx_tomcat]# docker-compose -f docker-compose.yml up
浏览器访问:
[root@docker ~]# docker-compose -f compose_nginx_tomcat/docker-compose.yml ps
Name Command State Ports
--------------------------------------------------------------------------------------------
composenginxtomcat_mysql_1 docker-entrypoint.sh mysqld Up 0.0.0.0:3307->3306/tcp
composenginxtomcat_nginx_1 ./sbin/nginx -g daemon off; Up 0.0.0.0:82->80/tcp
composenginxtomcat_tomcat01_1 ./bin/catalina.sh run Up 8080/tcp
composenginxtomcat_tomcat02_1 ./bin/catalina.sh run Up 8080/tcp
[root@docker ~]# docker-compose -f compose_nginx_tomcat/docker-compose.yml stop
Stopping composenginxtomcat_nginx_1 ... done
Stopping composenginxtomcat_mysql_1 ... done
Stopping composenginxtomcat_tomcat02_1 ... done
Stopping composenginxtomcat_tomcat01_1 ... done
[root@docker ~]# docker-compose -f compose_nginx_tomcat/docker-compose.yml ps
Name Command State Ports
------------------------------------------------------------------------------
composenginxtomcat_mysql_1 docker-entrypoint.sh mysqld Exit 0
composenginxtomcat_nginx_1 ./sbin/nginx -g daemon off; Exit 0
composenginxtomcat_tomcat01_1 ./bin/catalina.sh run Exit 143
composenginxtomcat_tomcat02_1 ./bin/catalina.sh run Exit 143
[root@docker ~]# docker-compose -f compose_nginx_tomcat/docker-compose.yml down
Removing composenginxtomcat_nginx_1 ... done
Removing composenginxtomcat_mysql_1 ... done
Removing composenginxtomcat_tomcat02_1 ... done
Removing composenginxtomcat_tomcat01_1 ... done
Removing network composenginxtomcat_lnmt
[root@docker ~]# docker-compose -f compose_nginx_tomcat/docker-compose.yml ps
Name Command State Ports
------------------------------