Compose是Docker官方的开源项目,可以实现对Docker容器集群的快速编排。
Compose 中有两个重要的概念:
服务(service):一个应用的容器,实际上可以包括若干运行相同镜像的容器实例。
项目(project):由一组关联的应用容器组成的一个完整业务单元,在 docker-compose.yml 文件中定义。

一、安装Compose

Compose由python开发,因此可以使用pip方式进行安装。

# pip install -U docker-compose

安装成功后可以查看docker-compose的用法:

# docker-compose -h
Define and run multi-container applications with Docker. Usage:
docker-compose [-f=<arg>...] [options] [COMMAND] [ARGS...]
docker-compose -h|--help Options:
-f, --file FILE Specify an alternate compose file (default: docker-compose.yml)
-p, --project-name NAME Specify an alternate project name (default: directory name)
--verbose Show more output
-v, --version Print version and exit
-H, --host HOST Daemon socket to connect to --tls Use TLS; implied by --tlsverify
--tlscacert CA_PATH Trust certs signed only by this CA
--tlscert CLIENT_CERT_PATH Path to TLS certificate file
--tlskey TLS_KEY_PATH Path to TLS key file
--tlsverify Use TLS and verify the remote
--skip-hostname-check Don't check the daemon's hostname against the name specified
in the client certificate (for example if your docker host
is an IP address) Commands:
build Build or rebuild services
config Validate and view the compose file
create Create services
down Stop and remove containers, networks, images, and volumes
events Receive real time events from containers
exec Execute a command in a running container
help Get help on a command
kill Kill containers
logs View output from containers
pause Pause services
port Print the public port for a port binding
ps List containers
pull Pulls service images
restart Restart services
rm Remove stopped containers
run Run a one-off command
scale Set number of containers for a service
start Start services
stop Stop services
unpause Unpause services
up Create and start containers
version Show the Docker-Compose version information

添加bash补全命令:

# curl -L https://raw.githubusercontent.com/docker/compose/1.8.0/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose

其它安装方式:二进制包、容器中运行。

二、Compose命令说明

格式:
docker-compose [-f=<arg>...] [options] [COMMAND] [ARGS...]
选项:
-f, --file FILE 指定使用的 Compose 模板文件,默认为 docker-compose.yml,可以多次指定。
-p, --project-name NAME 指定项目名称,默认将使用所在目录名称作为项目名。
--x-networking 使用 Docker 的可拔插网络后端特性(需要 Docker 1.9 及以后版本)。
--x-network-driver DRIVER 指定网络后端的驱动,默认为 bridge(需要 Docker 1.9 及以后版本)。
--verbose 输出更多调试信息。
-v, --version 打印版本并退出
常用命令使用说明:

三、Compose模板文件
默认的模板文件名称为 docker-compose.yml,格式为 YAML 格式。
版本1中,其中每个顶级元素为服务名称,次级元素为服务容器的配置信息,例如

webapp:
image: examples/web
ports:
- "80:80"
volumes:
- "/data"

版本2扩展了 Compose 的语法,同时尽量保持跟版本1的兼容,除了可以声明网络和存储信息外,最大的不同一是添加了版本信息,另一个是需要将所有的服务放到 services 根下面。版本2写法如下:

version: "2"
services:
webapp:
image: examples/web
ports:
- "80:80"
volumes:
- "/data"

每个服务都必须通过 image 指令指定镜像或 build 指令(需要 Dockerfile)等来自动构建生成镜像。
常用指定如下:

四、使用docker-compose编排nginx、tomcat集群

创建一个名为compose-tomcat-nginx的目录,作为项目工作目录,并在其中创建两个子目录:nginx、tomcat。目录结构如下:

# tree
.
├── docker-compose.yml
├── nginx
│   ├── Dockerfile
│   ├── nginx-1.8.0.tar.gz
│   ├── nginx.conf
│   └── nginx-sticky.tar.gz
└── tomcat
├── apache-tomcat-7.0.56.tar.gz
├── Dockerfile
└── jdk-8u73-linux-x64.tar.gz 2 directories, 8 files

 docker-compose.yml文件内容如下:

# cat docker-compose.yml
tomcat1:
build: ./tomcat
expose:
- 8080 tomcat2:
build: ./tomcat
expose:
- 8080 nginx:
build: ./nginx
links:
- tomcat1:t01
- tomcat2:t02
ports:
- "80:80"
expose:
- "80"

tomcat的Dockerfile文件内容如下:

# cat Dockerfile
FROM centos:6.9
MAINTAINER eivll0m@163.com ADD jdk-8u73-linux-x64.tar.gz /app ENV JAVA_HOME /app/jdk1.8.0_73 ADD apache-tomcat-7.0.56.tar.gz /app WORKDIR /app/apache-tomcat-7.0.56 ENTRYPOINT ["bin/catalina.sh","run"] EXPOSE 8080

nginx的Dockerfile文件内容如下:

FROM centos:6.9
MAINTAINER eivll0m@163.com ADD nginx-1.8.0.tar.gz /app
ADD nginx-sticky.tar.gz /app RUN yum -y install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel RUN cd /app/nginx-1.8.0 \
&& ./configure --prefix=/app/nginx --add-module=/app/nginx-sticky \
&& make \
&& make install
COPY nginx.conf /app/nginx/conf cmd ["/app/nginx/sbin/nginx", "-g", "daemon off;"] EXPOSE 80

nginx.conf文件内容如下(未优化):

# cat nginx.conf

user  root root;
worker_processes 2; #error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid; events {
worker_connections 1024;
} http {
include mime.types;
default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; sendfile on;
#tcp_nopush on; #keepalive_timeout 0;
keepalive_timeout 65; gzip on;
gzip_comp_level 6;
gzip_proxied any;
gzip_buffers 4 8k;
gzip_min_length 1024;
gzip_types text/plain text/xml text/css application/x-javascript text/javascript image/jpeg; proxy_buffer_size 64k;
proxy_buffers 32 32k;
proxy_ignore_client_abort on;
client_header_buffer_size 4k;
client_max_body_size 50m;
#send_timeout 5m; upstream tomcat_client {
sticky;
server t01:8080 weight=1;
server t02:8080 weight=1;
} server {
listen 80;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main;
location / {
proxy_pass http://tomcat_client;
proxy_redirect default;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
} #location ~ ^/console/ {
# proxy_pass http://mh.lkpower.com;
# ssi on;
# proxy_redirect off;
#}
#location ~ ^/hospital/ {
# proxy_pass http://mh.lkpower.com;
# ssi on;
# proxy_redirect off;
#}
#error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#} # deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
} }

运行docker-compose:

# docker-compose up     #前台
# docker-compose up -d #后台

其它相关命令:

# docker-compose ps
# docker-compose build --no-cache --force-rm
# docker-compose rm --all
# docker-compose scale tomcat1=5

弹性伸缩:

本集群实现了nginx代理后端tomcat,并实现了session保持。

Docker Compose容器编排的更多相关文章

  1. AspNetCore容器化(Docker)部署(三) —— Docker Compose容器编排

    一.前言 上一篇部署了一个最基础的helloworld应用,创建了两个容器和一个network,还算应付得过来. 如果该应用继续引入mysql.redis.job等若干服务,到时候发布一次得工作量之大 ...

  2. Docker Compose 容器编排 NET Core 6+MySQL 8+Nginx + Redis

    环境: CentOS 8.5.2111Docker 20.10.10Docker-Compose 2.1.0 服务: db  redis  web nginx NET Core 6+MySQL 8+N ...

  3. Docker Compose 容器编排

    1. 前言 Docker Compose 是 Docker 容器进行编排的工具,定义和运行多容器的应用,可以一条命令启动多个容器. 使用Compose 基本上分为三步: Dockerfile 定义应用 ...

  4. 八、docker compose容器编排

    一. Docker-Compose 1.1. 什么是Docker Compose Compose 项目是 Docker 官方的开源项目,负责实现 Docker 容器集群的快速编排,开源代码在 http ...

  5. asp.net core容器&mysql容器network互联 & docker compose方式编排启动多个容器

    文章简介 asp.net core webapi容器与Mysql容器互联(network方式) docker compose方式编排启动多个容器 asp.net core webapi容器与Mysql ...

  6. 物联网架构成长之路(24)-Docker练习之Compose容器编排

    0.前言 一开始学的之后,是想一步到位直接上Kubernetes(K8s)的,后面没想到,好像有点复杂,有些概念不是很懂.因此学习东西还是要循序渐进,慢慢来.先了解单机编排技术Docker Compo ...

  7. Docker | 第七章:Docker Compose服务编排介绍及使用

    前言 前面章节,我们学习了如何构建自己的镜像文件,如何保存自己的镜像文件.大多都是一个镜像启动.当一个系统需要多个子系统进行配合时,若每个子系统也就是镜像需要一个个手动启动和停止的话,那估计实施人员也 ...

  8. docker compose容器互联

    使用docker-compose编排容器时,如容器之间需要互相通信,使用本地连接,需要使用容器名来代替localhost "connection": "postgresq ...

  9. 28. docker swarm 容器编排简介

    1.采用集群架构 集群架构包含节点和角色 docker 节点中 包含 worker 和 manager 两个角色 manager 相当于 swarm 集群的 大脑  是用来管理配置节点的 (避免单点故 ...

随机推荐

  1. mysql 恢复数据

    前提:保存了需要恢复数据库的文件 .frm 和 .ibd 文件 条件:InnoDB 类型的 恢复表结构1.新建一个数据库--新建一个表,表名和列数和需要恢复数据库相同2.停止mysql服务器 serv ...

  2. dig(域信息搜索器)命令

    dig命令   dig命令是常用的域名查询工具,可以用来测试域名系统工作是否正常. 语法 dig(选项)(参数) 选项 @<服务器地址>:指定进行域名解析的域名服务器: -b<ip地 ...

  3. 织梦CMS提示DedeTag Engine Create File False错误的解决办法总结

    今天帮客户升级站点,遇到了一个老问题,生成栏目的时候提示"DedeTag Engine Create File False",突然发觉这个问题竟然在以前做站的时候困扰过我多次,于是 ...

  4. ser-u服务器安装和使用(创建ftp服务器)

    安装serv-u,创建ftp服务器,以及连接服务器上传和下载文件. 工具/原料 serv-u,8uftp 装有win7以上或者winserver系统的电脑 安装serv-u 1 http://pan. ...

  5. sudo :apt-get:command not found

    在centos下用yum install xxx yum和apt-get的区别 一般来说著名的linux系统基本上分两大类:  1.RedHat系列:Redhat.Centos.Fedora等  2. ...

  6. php的底层原理

    PHP说简单,但是要精通也不是一件简单的事.我们除了会使用之外,还得知道它底层的工作原理. PHP是一种适用于web开发的动态语言.具体点说,就是一个用C语言实现包含大量组件的软件框架.更狭义点看,可 ...

  7. libpng+VS2012(VS2015)的使用

    OpenCV保存PNG图像底层调用的就是libpng库,简要说一下libPNG库的单独使用. 1.首先需要下载两个库,一个是libpng,一个是zlib libpng库下载地址:http://www. ...

  8. Hadoop问题:Incorrect configuration: namenode address dfs.namenode.rpc-address is not configured

    问题描述:Incorrect configuration: namenode address dfs.namenode.rpc-address is not configured 问题分析:core- ...

  9. Java通配符解惑

    T  有类型 ?  未知类型 一.通配符的上界 既然知道List<Cat>并不是List<Anilmal>的子类型,那就需要去寻找替他解决的办法, 是AnimalTrianer ...

  10. C/C++ typedef

    body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...