Docker compose容器编排
入门
安装
curl -L https://get.daocloud.io/docker/compose/releases/download/1.11.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
相关命令
参数 | 说明 |
---|---|
build | 构建或重建服务 |
help | 命令帮助 |
kill | 杀掉容器 |
logs | 显示容器的输出内容 |
port | 打印绑定的开放端口 |
ps | 显示容器 |
pull | 拉取服务镜像 |
restart | 重启服务 |
rm | 删除停止的容器 |
run | 运行一个一次性命令 |
scale | 设置服务的容器数目 |
start | 开启服务 |
stop | 停止服务 |
up | 创建并启动容器 |
配置compose
[root@docker01 mysql]# docker-compose version
docker-compose version 1.11.2, build dfed245
docker-py version: 2.1.0
CPython version: 2.7.13
OpenSSL version: OpenSSL 1.0.1t 3 May 2016
[root@docker01 compose-workpress]# cat compose-workpress.yml
version: "2"
services:
php:
image: workpress/php:1.0
volumes:
- /web:/web
nginx:
image: workpress/nginx:1.0
ports:
- 80:80
volumes:
- /web:/web
links:
- php
mysql:
image: workpress/mysql:1.0
ports:
- 3306:3306
volumes:
- /opt/data:/var/lib/mysql
compose只是为了方便docker容器的管理,并且依赖于前面dockerfile创建的镜像
构建和启动
[root@docker01 compose-workpress]# docker-compose -f compose-workpress.yml up -d
Starting composeworkpress_php_2
Starting composeworkpress_mysql_1
Starting composeworkpress_php_1
Creating composeworkpress_nginx_1
[root@docker01 compose-workpress]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b925b02a893e workpress/nginx:1.0 "/usr/local/nginx/..." 4 seconds ago Up 3 seconds 0.0.0.0:80->80/tcp composeworkpress_nginx_1
82b2384664e6 registry "/entrypoint.sh /e..." About an hour ago Up 21 minutes 0.0.0.0:5000->5000/tcp clever_jang
807d0ea56ab1 workpress/php:1.0 "/usr/local/php/sb..." 7 weeks ago Up 6 seconds 9000/tcp composeworkpress_php_2
1fb8e1f8a56e workpress/php:1.0 "/usr/local/php/sb..." 7 weeks ago Up 4 seconds 9000/tcp composeworkpress_php_1
04ab02885e42 workpress/mysql:1.0 "/bin/bash /opt/st..." 7 weeks ago Up 5 seconds 0.0.0.0:3306->3306/tcp composeworkpress_mysql_1
查看相关启动日志
[root@docker01 compose-workpress]# docker-compose -f compose-workpress.yml logs
Attaching to composeworkpress_nginx_1, composeworkpress_php_2, composeworkpress_php_1, composeworkpress_mysql_1
mysql_1 | 170808 18:37:04 mysqld_safe Logging to '/var/log/mysqld.log'.
mysql_1 | 170808 18:37:04 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
mysql_1 | 171001 21:27:48 mysqld_safe Logging to '/var/log/mysqld.log'.
mysql_1 | 171001 21:27:48 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
查看相关状态
[root@docker01 compose-workpress]# docker-compose -f compose-workpress.yml ps
Name Command State Ports
---------------------------------------------------------------------------------------
composeworkpress_mysql_1 /bin/bash /opt/startup.sh Up 0.0.0.0:3306->3306/tcp
composeworkpress_nginx_1 /usr/local/nginx/sbin/nginx Up 0.0.0.0:80->80/tcp
composeworkpress_php_1 /usr/local/php/sbin/php-fpm Up 9000/tcp
composeworkpress_php_2 /usr/local/php/sbin/php-fpm Up 9000/tcp
停止和删除mysql容器
[root@docker01 compose-workpress]# docker-compose -f compose-workpress.yml stop mysql
Stopping composeworkpress_mysql_1 ... done
[root@docker01 compose-workpress]#docker-compose -f compose-workpress.yml rm mysql
停止全部compose管理的容器
[root@docker01 compose-workpress]# docker-compose -f compose-workpress.yml stop
Stopping composeworkpress_nginx_1 ... done
Stopping composeworkpress_php_2 ... done
Stopping composeworkpress_php_1 ... done
构建复杂的workpress集群
目录结构
[root@docker01 compose-multiple-workpress]# tree -L 2 --charset ASCII
.
|-- 1
|-- docker-compose.yml
`-- nginx
|-- Dockerfile
|-- nginx-1.11.10
|-- nginx.conf
`-- nginx_default.conf
2 directories, 5 files
docker-compose.yml
[root@docker01 compose-multiple-workpress]# cat docker-compose.yml
version: "2"
services:
web_a:
image: workpress/nginx:1.0
volumes:
- /web_a:/web
expose:
- 80
web_b:
image: workpress/nginx:1.0
volumes:
- /web_b:/web
expose:
- 80
web_c:
image: workpress/nginx:1.0
volumes:
- /web_c:/web
expose:
- 80
nginx:
build: nginx
ports:
- 80:80
links:
- web_a
- web_b
- web_c
nginx容器的配置文件
Dockerfile
[root@docker01 nginx]# cat Dockerfile
#lnmp centos 6.0
from centos:centos6
MAINTAINER dennis52o1314@163.com
ENV APP_DIR /web
add nginx-1.11.10 /nginx-1.11.10
RUN yum -y groupinstall "Development Tools" "Server Platform Deveopment"
RUN yum -y install openssl-devel pcre-devel
RUN useradd nginx -s /sbin/nologin
RUN cd /nginx-1.11.10 && ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre && make && make install
RUN mkdir /usr/local/nginx/conf/vhosts
RUN mkdir /var/log/nginx
ADD nginx.conf /usr/local/nginx/conf/nginx.conf
ADD nginx_default.conf /usr/local/nginx/conf/vhosts/default.conf
EXPOSE 80
CMD ["/usr/local/nginx/sbin/nginx"]
nginx_default.conf
[root@docker01 nginx]# cat nginx_default.conf
upstream web_up{
server web_a max_fails=3 fail_timeout=20s weight=2;
server web_b max_fails=3 fail_timeout=20s weight=2;
server web_c max_fails=3 fail_timeout=20s weight=2;
}
server {
listen 80 default_server;
server_name localhost;
#charset koi8-r;
location / {
proxy_pass http://web_up;
proxy_set_header Host ;
proxy_set_header X-Real-IP ;
proxy_set_header X-Forwared-For ;
}
}
创建三个目录,/web_a,/web_b,/web_c,分表放index.html文件到下面
mkdir /web_a
mkdir /web_b
mkdir /web_c
cat>/web_a/index.html<<EOF
web_a
EOF
cat>/web_b/index.html<<EOF
web_b
EOF
cat>/web_c/index.html<<EOF
web_c
EOF
[root@docker01 compose-multiple-workpress]# docker-compose up -d
Creating composemultipleworkpress_web_c_1
Creating composemultipleworkpress_web_b_1
Creating composemultipleworkpress_web_a_1
Creating composemultipleworkpress_nginx_1
[root@docker01 compose-multiple-workpress]#
如果出现下面的问题
WARNING: Image for service nginx was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
需要先执行docker-compose buil
d再执行docker-compose up -d
查看一下运行状态
[root@docker01 compose-multiple-workpress]# docker-compose ps
设置php的数量
docker-compose -f compose-workpress.yml scale php=2
停止容器
docker-compose -f compose-workpress.yml stop