docker单独只能run一次创建一个容器,而docker-compose可以对多个容器进行编排。
这里我用docker-compose用haproxy做负载均衡,一键配置多个容器
先导入需要的镜像.haproxy,nginx,apache
在/tmp/docker目录下建立compose目录,并编写.yml文件
[root@foundation20 .docker]# cd /tmp/docker/
[root@foundation20 docker]# ls
auth certs Dockerfile dvd.repo ssh supervisord.conf test web
[root@foundation20 docker]# mkdir compose
[root@foundation20 docker]# cd compose/
[root@foundation20 compose]# ls
[root@foundation20 compose]# pwd
/tmp/docker/compose
[root@foundation20 compose]# vim docker-compose.yml
apache:
build: ./web
expose:
- 80
nginx:
image: nginx
expose:
- 80
haproxy:
image: haproxy
volumes:
- ./haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
links:
- nginx
- apache
ports:
- "8080:80"
expose:
- "80"
[root@foundation20 compose]# mkdir haproxy
[root@foundation20 compose]# cd haproxy/
[root@foundation20 haproxy]# ls
[root@foundation20 haproxy]# vim 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
[root@foundation20 compose]# cd /web/Dockerfile
FROM rhel7
EXPOSE 80
COPY dvd.repo /etc/yum.repos.d/dvd.repo
RUN rpmdb --rebuilddb && yum install -y httpd && yum clean all
CMD ["/usr/sbin/httpd","-D","FOREGROUND"]
[root@foundation20 compose]# cd /web/dvd.repo
[dvd]
name=rhel7.0
baseurl=http://172.25.254.250/rhel7.0/x86_64/dvd/
gpgcheck=0
[root@foundation20 compose]# cd /usr/local/bin/
[root@foundation20 bin]# chmod +x docker-compose-Linux-x86_64-1.22.0
[root@foundation20 bin]# mv docker-compose-Linux-x86_64-1.22.0 docker-compose
##up推送,crtl+c退出
[root@foundation20 compose]# docker-compose up
Starting compose_apache_1 ... done
Starting compose_nginx_1 ... done
Starting 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
^CGracefully stopping... (press Ctrl+C again to force)
Stopping compose_haproxy_1 ... done
Stopping compose_apache_1 ... done
Stopping compose_nginx_1 ... done