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. LAMP环境跟LNMP环境有什么不同,主要用什么地方

    LAMP即Linux+Apache+Mysql/MariaDB+Perl/PHP/Python Linux+Apache+Mysql/MariaDB+Perl/PHP/Python一组常用来搭建动态网 ...

  2. 学习总结:libevent--简单入门

    libevent--简单入门 一.简介 libevent是一个c语言写的事件驱动库,轻量级,专注于网络,跨平台特性好,支持多种 I/O 多路复用.支持I/O,定时器和信号等事件,允许设置注册事件优先级 ...

  3. .net Core EF统一配置实体类型

    一般情况需要对某个实体进行一些配置时代码如下: protected override void OnModelCreating(ModelBuilder modelBuilder) { base.On ...

  4. Moogoose Constructor小坑

    注意! exports 出来的 Model名字,必须和 Constructor的名字不一样!!! 不然Constructor会被覆盖,报错 这个是修改之后的.修改前,是var account = ne ...

  5. vue学习笔记(五)——指令

    13条指令 1. v-text (数据绑定语法-插值) <span v-text="msg"></span> <!-- 和下面的一样 --> & ...

  6. 监控服务器ssh登录,并发送报警邮件

    最近想监控下云主机的ssh登录情况,所以开始写ssh登录报警监控.实现方式并不难. 一:邮箱申请开启SMTP 在邮箱中选择“设置”----->“账户” 在如下图处开启POP3/SMTP服务,并生 ...

  7. spring MVC 运行过程

    以Tomcat为例,想在Web容器中使用Spirng MVC,必须进行四项的配置: 1.修改web.xml, 2.添加servlet定义.编写servletname-servlet.xml( serv ...

  8. UWP: 实现 UWP 应用自启动

    在上一篇文章中,我们实现了使用命令行来启动 UWP 应用,在这一篇文章中,我们会实现 UWP 应用自启用的实现,也即开机后或用户登陆后,应用自己启动.这些特性原来都是 Win32 程序所具备的,UWP ...

  9. Linxu指令--date,cal

    在linux环境中,不管是编程还是其他维护,时间是必不可少的,也经常会用到时间的运算,熟练运用date命令来表示自己想要表示的时间,肯定可以给自己的工作带来诸多方便. 1.命令格式: date [参数 ...

  10. c++ singleton单例模式

    方法1:加锁的经典懒汉实现: class singleton { public: static pthread_mutex_t mutex; static singleton* initance(); ...