Docker compose实现容器之间的交互

利用Docker-compose控制不同的容器

1.获取脚本

[root@server1 ~]# cd /usr/local/bin/
[root@server1 bin]# ls
docker-compose  docker-compose-Linux-x86_64-1.22.0

2.导入镜像

docker load -i haproxy.tar
docker load -i nginx.tar
[root@server1 ~]# docker images nginx
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              e548f1a579cf        15 months ago       109MB
[root@server1 ~]# docker images haproxy
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
haproxy             latest              fbd1f55f79b3        3 years ago         139MB
[root@server1 ~]#

3.编写 文件

mkdir /mnt/compose
vim docker-compose.yml
web1: 
        image: nginx
        expose: 
                - 80
        volumes: 
                - ./web1:/usr/share/nginx/html

web2:
        image: nginx
        expose:
                - 80
        volumes:
                - ./web2:/usr/share/nginx/html
haproxy:
        image: haproxy
        volumes:
                - ./haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
        links:
                - web1
                - web2
        ports: 
                - "80:80"
        expose:
                - 80

4.修改默认发布页内容

[root@server1 compose]# mkdir web1
[root@server1 compose]# mkdir web2
[root@server1 compose]# echo web1 > web1/index.html
[root@server1 compose]# echo web2 > web2/index.html
[root@server1 compose]# mkdir haproxy
[root@server1 compose]# ls
docker-compose.yml  haproxy  web1  web2
[root@server1 compose]# vim haproxy/haproxy.cfg 
[root@server1 compose]# cat haproxy/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 5000ms
	timeout server 5000ms
	stats uri /status
frontend balancer
	bind 0.0.0.0:80
	default backend web_backends
backend web backends
	balance roundrobin
	server server1 web1:80 check
	server server2 web2:80 check

5.启动

docker-compose up -d
Docker compose实现容器之间的交互_第1张图片

6.测试

在浏览器上输入宿主机ip地址,可以看到我们修改的nginx发布页,刷新页面可以负载均衡

Docker compose实现容器之间的交互_第2张图片

你可能感兴趣的:(Docker compose实现容器之间的交互)