什么是Docker-Compose:
Compose项目来源于之前的fig项目,使用python语言编写,与docker/swarm配合度很高。
Compose 是 Docker 容器进行编排的工具,定义和运行多容器的应用,可以一条命令启动多个容器,使用
Docker Compose不再需要使用shell脚本来启动容器。
Compose 通过一个配置文件来管理多个Docker容器,在配置文件中,所有的容器通过services来定义,然后使用
docker-compose脚本来启动,停止和重启应用,和应用中的服务以及所有依赖服务的容器,非常适合组合使用多个
容器进行开发的场景。
docker-compose默认的模板文件是 docker-compose.yml,其中定义的每个服务都必须通过 image 指令指定镜
像或 build 指令(需要 Dockerfile)来自动构建。
其它大部分指令都跟 docker run 中的类似。
如果使用 build 指令,在 Dockerfile 中设置的选项(例如:CMD, EXPOSE, VOLUME, ENV 等) 将会自动被获取,
无需在 docker-compose.yml 中再次设置。
使用Compose 基本上分为三步:
1.Dockerfile 定义应用的运行环境
2.docker-compose.yml 定义组成应用的各服务
3.docker-compose up 启动整个应用
搭建compose环境:
[root@foundation38 .docker]# cd /tmp/docker/
[root@foundation38 docker]# ls
auth certs Dockerfile ssh supervisord.conf test web yum.repo
[root@foundation38 docker]# mkdir compose
[root@foundation38 docker]# cd compose/
[root@foundation38 compose]# ls
[root@foundation38 compose]# pwd
/tmp/docker/compose
[root@foundation38 compose]# vim docker-compose.yml 将官方文件下载下来修改即可
docker-compose文件结构,官方提供了一个yaml Docker Compose 配置文件的标准例子,
一份标准配置文件应该包含 version、services、networks
三大部分,其中最关键的就是 services 和 networks 两个部分
[root@foundation38 compose]# ls
docker-compose.yml
apache:
image: rhel7:v1
expose:
- 80
volumes:
- ./web:/var/www/html
nginx:
image: nginx
expose:
- 80
haproxy:
image: haproxy
volumes:
- ./haproxy:/usr/local/etc/haproxy
links:
- apache
- nginx
ports:
- "8080:80"
expose:
- 80
[root@foundation38 compose]# cd ..
[root@foundation38 docker]# cd compose/
[root@foundation38 compose]# mkdir haproxy 用haproxy实现负载均衡
[root@foundation38 compose]# cd haproxy/
[root@foundation38 haproxy]# ls
[root@foundation38 haproxy]# vim haproxy.cfg
[root@foundation38 haproxy]# vim haproxy.cfg
[root@foundation38 haproxy]# cat haproxy.cfg 这个直接从官网复制下来就可以使用
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
stats uri /status
frontend balancer
bind 0.0.0.0:80
default_backend web_backends
backend web_backends
balance roundrobin
server web1 apache:80 check
server web2 nginx:80 check
compose的安装:
从github上下载docker-compose二进制文件安装下载最新版的docker-compose文件
[root@foundation38 haproxy]# cd ..
[root@foundation38 compose]# docker-compose -v 系统中没有自带的docker-compose命令
bash: docker-compose: command not found...
[root@foundation38 compose]# cd /usr/local/bin/
[root@foundation38 bin]# ls
rht-vmctl rht-vmicons rht-vmsetkeyboard
[root@foundation38 bin]# ls
docker-compose-Linux-x86_64-1.22.0 rht-vmctl rht-vmicons rht-vmsetkeyboard
[root@foundation38 bin]# chmod +x docker-compose-Linux-x86_64-1.22.0 自己下载的二进制文件赋予执行权限
[root@foundation38 bin]# ls
docker-compose-Linux-x86_64-1.22.0 rht-vmctl rht-vmicons rht-vmsetkeyboard
[root@foundation38 bin]# ln -s docker-compose-Linux-x86_64-1.22.0 docker-compose 制作软链接
[root@foundation38 bin]# docker-compose -v -v可以查看当前compose的版本,测试安装结果
docker-compose version 1.22.0, build f46880fe
[root@foundation38 bin]# ls
docker-compose rht-vmctl rht-vmsetkeyboard
docker-compose-Linux-x86_64-1.22.0 rht-vmicons
[root@foundation38 bin]# cd /tmp/docker/compose/
[root@foundation38 compose]# ls
docker-compose.yml haproxy
如果直接运行compose报错,是由于没有yml文件所写的rhel7:v1,运行即可:
[root@foundation38 compose]# cd ..
[root@foundation38 docker]# pwd
/tmp/docker
[root@foundation38 docker]# ls
auth certs compose Dockerfile ssh supervisord.conf test web yum.repo
[root@foundation38 docker]# vim Dockerfile
[root@foundation38 docker]# cat Dockerfile
FROM rhel7
ENV HOSTNAME server1
MAINTAINER 229683481@qq.com
EXPOSE 80
COPY yum.repo /etc/yum.repos.d/yum.repo
RUN rpmdb --rebuilddb && yum install -y httpd && yum clean all
VOLUME ["/var/www/html"]
CMD ["/usr/sbin/httpd","-D","FOREGROUND"]
[root@foundation38 docker]# docker build -t rhel7:v1 .
Sending build context to Docker daemon 36.35 kB
Step 1/8 : FROM rhel7
---> 0a3eb3fde7fd
Step 2/8 : ENV HOSTNAME server1
---> Using cache
---> bbda1be07d9e
Step 3/8 : MAINTAINER 229683481@qq.com
---> Using cache
---> aa986d9a3d5f
Step 4/8 : EXPOSE 80
---> Using cache
---> 61c2ffadd679
Step 5/8 : COPY yum.repo /etc/yum.repos.d/yum.repo
---> Using cache
---> 3c8129b8f4e3
Step 6/8 : RUN rpmdb --rebuilddb && yum install -y httpd && yum clean all
---> Using cache
---> 5a455b585e9e
Step 7/8 : VOLUME /var/www/html
---> Using cache
---> 21c4f470a32e
Step 8/8 : CMD /usr/sbin/httpd -D FOREGROUND
---> Using cache
---> 1796ce7f20c8
Successfully built 1796ce7f20c8
[root@foundation38 compose]# docker-compose up 运行compose
Starting compose_nginx_1 ... done
Starting compose_apache_1 ... done
Recreating compose_haproxy_1 ... done
Attaching to compose_apache_1, compose_nginx_1, compose_haproxy_1
apache_1 | AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
haproxy_1 | <7>haproxy-systemd-wrapper: executing /usr/local/sbin/haproxy -p /run/haproxy.pid -f /usr/local/etc/haproxy/haproxy.cfg -Ds
[root@foundation38 compose]# docker-compose up 在运行的compose中可以看到信息
Starting compose_nginx_1 ... done
Starting compose_apache_1 ... done
Recreating compose_haproxy_1 ... done
Attaching to compose_apache_1, compose_nginx_1, compose_haproxy_1
apache_1 | AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
haproxy_1 | <7>haproxy-systemd-wrapper: executing /usr/local/sbin/haproxy -p /run/haproxy.pid -f /usr/local/etc/haproxy/haproxy.cfg -Ds
nginx_1 | 172.17.0.4 - - [22/Aug/2018:08:09:19 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0" "-"
nginx_1 | 172.17.0.4 - - [22/Aug/2018:08:10:31 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0" "-"
直接ctrl+c结束在网页就不可以访问:
直接在终端打开服务:
[root@foundation38 compose]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1827c740a059 haproxy "/docker-entrypoin..." 7 minutes ago Exited (0) 40 seconds ago compose_haproxy_1
dd054ad399f4 rhel7:v1 "/usr/sbin/httpd -..." 12 minutes ago Exited (0) 40 seconds ago compose_apache_1
e21cf912a341 nginx "nginx -g 'daemon ..." 34 minutes ago Exited (0) 40 seconds ago compose_nginx_1
[root@foundation38 compose]# docker-compose start 将docker-compose打开
Starting apache ... done
Starting nginx ... done
Starting haproxy ... done
[root@foundation38 compose]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1827c740a059 haproxy "/docker-entrypoin..." 8 minutes ago Up 1 second 0.0.0.0:8080->80/tcp compose_haproxy_1
dd054ad399f4 rhel7:v1 "/usr/sbin/httpd -..." 13 minutes ago Up 2 seconds 80/tcp compose_apache_1
e21cf912a341 nginx "nginx -g 'daemon ..." 34 minutes ago Up 2 seconds 80/tcp compose_nginx_1
在网页测试可以进行负载均衡:
在网页测试可以进行监控:
将httpd功能关闭:
[root@foundation38 compose]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1827c740a059 haproxy "/docker-entrypoin..." 10 minutes ago Up 2 minutes 0.0.0.0:8080->80/tcp compose_haproxy_1
dd054ad399f4 rhel7:v1 "/usr/sbin/httpd -..." 15 minutes ago Up 2 minutes 80/tcp compose_apache_1
e21cf912a341 nginx "nginx -g 'daemon ..." 36 minutes ago Up 2 minutes 80/tcp compose_nginx_1
[root@foundation38 compose]# docker stop compose_apache_1
compose_apache_1
在网页测试就不可以看到httpd的测试页:
在网页测试依旧可以看到监控:
在终端打开阿帕奇访问:
[root@foundation38 compose]# docker start compose_apache_1
compose_apache_1
[root@foundation38 compose]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1827c740a059 haproxy "/docker-entrypoin..." 15 minutes ago Up 6 minutes 0.0.0.0:8080->80/tcp compose_haproxy_1
dd054ad399f4 rhel7:v1 "/usr/sbin/httpd -..." 20 minutes ago Up 1 second 80/tcp compose_apache_1
e21cf912a341 nginx "nginx -g 'daemon ..." 41 minutes ago Up 6 minutes 80/tcp compose_nginx_1
[root@foundation38 compose]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1827c740a059 haproxy "/docker-entrypoin..." 15 minutes ago Up 6 minutes 0.0.0.0:8080->80/tcp compose_haproxy_1
dd054ad399f4 rhel7:v1 "/usr/sbin/httpd -..." 20 minutes ago Up 5 seconds 80/tcp compose_apache_1
e21cf912a341 nginx "nginx -g 'daemon ..." 41 minutes ago Up 6 minutes 80/tcp compose_nginx_1