Docker-compose 容器集群

Posted esec

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker-compose 容器集群相关的知识,希望对你有一定的参考价值。

一、Docker-compose 简介

Docker-Compose项目是Docker官方的开源项目,负责实现对Docker容器集群的快速编排。

Docker-Compose将所管理的容器分为三层,分别是 工程(project),服务(service)以及容器(container)。Docker-Compose运行目录下的所有文件(docker-compose.yml,extends文件或环境变量文件等)组成一个工程,若无特殊指定工程名即为当前目录名。一个工程当中可包含多个服务,每个服务中定义了容器运行的镜像、参数、依赖。一个服务当中可包括多个容器实例,Docker-Compose并没有解决负载均衡的问题,因此需要借助其它工具实现服务发现及负载均衡,比如 Consul。

Docker-Compose的工程配置文件默认为docker-compose.yml,可通过环境变量COMPOSE_FILE或-f参数自定义配置文件,其定义了多个有依赖关系的服务及每个服务运行的容器。

使用一个Dockerfile模板文件,可以让用户很方便的定义一个单独的应用容器。在工作中,经常会碰到需要多个容器相互配合来完成某项任务的情况。例如要实现一个Web项目,除了Web服务容器本身,往往还需要再加上后端的数据库服务容器,甚至还包括负载均衡容器等。

Compose允许用户通过一个单独的docker-compose.yml模板文件(YAML 格式)来定义一组相关联的应用容器为一个项目(project)。

Docker-Compose项目由Python编写,调用Docker服务提供的API来对容器进行管理。因此,只要所操作的平台支持Docker API, 就可以在其上利用Compose来进行编排管理。

docker-compose分三层:project,service,container

project:代表多个service组成的项目,默认是用 工作目录的名称作为project的工程名称
service:一个service可以包含一个或多个容器,在里面可以定义 网络模式 端口 镜像 数据卷 等参数
container:可以直接由一个已存在的镜像运行实现,也可以通过dockerfile构建镜像实现。

二、compose 部署

1.安装docker
2.安装docker compse

Docker Compose 环境安装
Docker Compose 是 Docker 的独立产品,因此需要安装 Docker 之后在单独安装 Docker Compose

#下载
curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

#或者已下载过直接从宿主机传入到/opt目录下
mv docker-compose /usr/local/bin/
#安装
chmod +x /usr/local/bin/docker-compose
#查看版本
docker-compose --version

YAML 文件格式及编写注意事项

YAML 是一种标记语言,它可以很直观的展示数据序列化格式,可读性高。类似于 XML数据描述语言,语法比 XML 简单的很多。YAML 数据结构通过缩进来表示,连续的项目通过减号来表示,键值对用冒号分隔,数组用中括号 [ ] 括起来, hash 用花括号 括起来。

YAML 文件格式及编写注意事项

大小与敏感
通过缩进表示层级关系
不支持制表符tab键缩进,只能使用空格缩进
缩进的空格数目不重要,只要相同层级左对齐,通常开头缩进2个空格
用#号注释
符号字符后缩进1个空格,如冒号:、逗号,、横杠-

docker compose常用字段
字段 描述
build 指定Dockerfile文件名(要指定的Dockerfile文件需要在build标签的子级标签中用dockerfile标签指定)
dockerfile 构建镜像上下文路径
context 可以是dockerfile路径,或者是执行git 仓库的url地址
image 指定镜像(已存在)
command 执行命令,会覆盖容器启动后默认执行的命令(会覆盖Dockerfile的CMD指令)
container_name 指定容器名称,由于容器名称是唯一的,如果指定自定义名称,则无法scale
deploy 指定部署和运行服务相关配置,只能在Swarm模式使用
enviroment 添加环境变量
networks 加入网络,引用顶级networks下条目
networks-mode 设置容器的网络模式,host、bridge…
ports 暴露容器端口,与-p 相同,但是端口不能低于60
volumes 挂载一个宿主机目录或命令卷到容器,命名卷要在顶级volumes 定义卷名称
volumes_from 从另一个服务或容器挂载卷,可选参数 :ro 和 :rw,仅版本’2’支持
hostname 容器主机名
sysctls 在容器内设置内核参数
links 连接到另一个容器,- 服务名称[ :服务别名 ]
privilcgcd 用来给容器root权限,注意是不安全的,truc
restart 重启策略,定义是否重启容器; no(默认,不重启), always(总是重启), on-failure,(退出状态非0时重启), on-failure:3 ,在容器非正常退出时重启容器,最多重启3次, unless-stoped 在容器退出时总是重启容器,但是不考虑在Docker 守护进程启动时就已经停止了的容器
depends_on 此标签用于解决容器的依赖,启动先后问题。如启动应用容器,需要先启动数据库容器php:
depends_on:
apache
mysql |
docker compose常用命令:
字段 描述
build 重新构建服务
ps 列出容器
up 创建和启动容器
exec 在容器里面执行命令
scale 指定一个服务容器启动数量
top 显示容器进程
logs 查看容器输出
down 删除容器/网络/数据卷和镜像
stop/start/restart 停止/启动/重启服务
编写Docker Compose搭建lnmp
vim /etc/sysctl.conf
net.ipv4.ip_forward = 1 #末行添加
sysctl -p #刷新配置
systemctl restart docker #重启docker服务

配置nginx
编写dockerfile文件

FROM centos:7
MAINTAINER this is nginx images<gb>
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin nginx
ADD nginx-1.12.0.tar.gz /opt/
WORKDIR /opt/nginx-1.12.0/
RUN ./configure \\
--prefix=/usr/local/nginx \\
--user=nginx \\
--group=nginx \\
--with-http_stub_status_module && make -j4 && make install
ENV PATH $PATH:/usr/local/nginx/sbin
ADD nginx.conf /usr/local/nginx/conf/
RUN chmod 777 -R /usr/local/nginx/html/
EXPOSE 80
EXPOSE 443
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]


准备配置好nginx配置文件(添加支持php解析配置)

#user nobody;
worker_processes 1;

#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;

server
listen 80;
server_name localhost;

charset utf-8;

#access_log logs/host.access.log main;

location /
root html;
index index.html index.php;

#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 172.18.0.30:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
include fastcgi_params;

# deny access to .htaccess files, if Apache\'s document root
# concurs with nginx\'s one
#
#location ~ /\\.ht
# deny all;
#

# another virtual host using mix of IP-, name-, and port-based configuration
#
#server
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

# location /
# root html;
# index index.html index.htm;
#
#


# HTTPS server
#
#server
# listen 443 ssl;
# server_name localhost;

# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location /
# root html;
# index index.html index.htm;
#
#

配置mysql
准备配置文件

vim my.cnf
[client]
port = 3306
socket=/usr/local/mysql/mysql.sock

[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES

 

编写dockerfile文件

FROM centos:7
MAINTAINER this is mysql image <lnmp>
RUN yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make;useradd -M -s /sbin/nologin mysql
ADD mysql-boost-5.7.20.tar.gz /usr/local/src/
WORKDIR /usr/local/src/mysql-5.7.20/
RUN cmake \\
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \\
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \\
-DSYSCONFDIR=/etc \\
-DSYSTEMD_PID_DIR=/usr/local/mysql \\
-DDEFAULT_CHARSET=utf8 \\
-DDEFAULT_COLLATION=utf8_general_ci \\
-DWITH_EXTRA_CHARSETS=all \\
-DWITH_INNOBASE_STORAGE_ENGINE=1 \\
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \\
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \\
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \\
-DMYSQL_DATADIR=/usr/local/mysql/data \\
-DWITH_BOOST=boost \\
-DWITH_SYSTEMD=1;make -j 4;make install
ADD my.cnf /etc/my.cnf
EXPOSE 3306
RUN chown -R mysql:mysql /usr/local/mysql/;chown mysql:mysql /etc/my.cnf
WORKDIR /usr/local/mysql/bin/
RUN ./mysqld \\
--initialize-insecure \\
--user=mysql \\
--basedir=/usr/local/mysql \\
--datadir=/usr/local/mysql/data;cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/;systemctl enable mysqld
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
VOLUME [ "/usr/local/mysql" ]
CMD ["/usr/sbin/init"]

配置php
编写Dockerfile文件

FROM centos:7
MAINTAINER this is php image <lnmp>
RUN yum install -y gd \\
libjpeg libjpeg-devel \\
libpng libpng-devel \\
freetype freetype-devel \\
libxml2 libxml2-devel \\
zlib zlib-devel \\
curl curl-devel \\
openssl openssl-devel \\
gcc gcc-c++ make pcre-devel;useradd -M -s /sbin/nologin nginx
ADD php-7.1.10.tar.bz2 /usr/local/src/
WORKDIR /usr/local/src/php-7.1.10
RUN ./configure \\
--prefix=/usr/local/php \\
--with-mysql-sock=/usr/local/mysql/mysql.sock \\
--with-mysqli \\
--with-zlib \\
--with-curl \\
--with-gd \\
--with-jpeg-dir \\
--with-png-dir \\
--with-freetype-dir \\
--with-openssl \\
--enable-fpm \\
--enable-mbstring \\
--enable-xml \\
--enable-session \\
--enable-ftp \\
--enable-pdo \\
--enable-tokenizer \\
--enable-zip ; make -j 4 ; make install
ENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATH
ADD php.ini /usr/local/php/lib/
ADD php-fpm.conf /usr/local/php/etc/
RUN rm -rf /usr/local/php/etc/php-fpm.d/www.conf.default
ADD www.conf /usr/local/php/etc/php-fpm.d/
EXPOSE 9000
CMD /usr/local/php/sbin/php-fpm -F

 

将所需的三个配置文件修改后写入

vim php-fpm.conf
[global]
pid = run/php-fpm.pid
include=/usr/local/php/etc/php-fpm.d/*.conf


vim php.ini
[PHP]
engine = On
short_open_tag = Off
precision = 14
output_buffering = 4096
zlib.output_compression = Off
implicit_flush = Off
unserialize_callback_func =
serialize_precision = -1
disable_functions =
disable_classes =
zend.enable_gc = On
expose_php = On
max_execution_time = 30
max_input_time = 60
memory_limit = 128M
error_reporting = E_ALL
display_errors = On
display_startup_errors = On
log_errors = On
log_errors_max_len = 1024
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
track_errors = On
html_errors = On
variables_order = "GPCS"
request_order = "GP"
register_argc_argv = Off
auto_globals_jit = On
post_max_size = 8M
auto_prepend_file =
auto_append_file =
default_mimetype = "text/html"
default_charset = "UTF-8"
doc_root =
user_dir =
enable_dl = Off
file_uploads = On
upload_max_filesize = 2M
max_file_uploads = 20
allow_url_fopen = On
allow_url_include = Off
default_socket_timeout = 60
[CLI Server]
cli_server.color = On
[Date]
date.timezone = Asia/Shanghai
[filter]
[iconv]
[intl]
[sqlite3]
[Pcre]
[Pdo]
[Pdo_mysql]
pdo_mysql.cache_size = 2000
pdo_mysql.default_socket=
[Phar]
[mail function]
SMTP = localhost
smtp_port = 25
mail.add_x_header = On
[SQL]
sql.safe_mode = Off
[ODBC]
odbc.allow_persistent = On
odbc.check_persistent = On
odbc.max_persistent = -1
odbc.max_links = -1
odbc.defaultlrl = 4096
odbc.defaultbinmode = 1
[Interbase]
ibase.allow_persistent = 1
ibase.max_persistent = -1
ibase.max_links = -1
ibase.timestampformat = "%Y-%m-%d %H:%M:%S"
ibase.dateformat = "%Y-%m-%d"
ibase.timeformat = "%H:%M:%S"
[MySQLi]
mysqli.max_persistent = -1
mysqli.allow_persistent = On
mysqli.max_links = -1
mysqli.cache_size = 2000
mysqli.default_port = 3306
mysqli.default_socket = /usr/local/mysql/mysql.sock
mysqli.default_host =
mysqli.default_user =
mysqli.default_pw =
mysqli.reconnect = Off
[mysqlnd]
mysqlnd.collect_statistics = On
mysqlnd.collect_memory_statistics = On
[OCI8]
[PostgreSQL]
pgsql.allow_persistent = On
pgsql.auto_reset_persistent = Off
pgsql.max_persistent = -1
pgsql.max_links = -1
pgsql.ignore_notice = 0
pgsql.log_notice = 0
[bcmath]
bcmath.scale = 0
[browscap]
[Session]
session.save_handler = files
session.use_strict_mode = 0
session.use_cookies = 1
session.use_only_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440
session.referer_check =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.sid_length = 26
session.trans_sid_tags = "a=href,area=href,frame=src,form="
session.sid_bits_per_character = 5
[Assertion]
zend.assertions = 1
[COM]
[mbstring]
[gd]
[exif]
[Tidy]
tidy.clean_output = Off
[soap]
soap.wsdl_cache_enabled=1
soap.wsdl_cache_dir="/tmp"
soap.wsdl_cache_ttl=86400
soap.wsdl_cache_limit = 5
[sysvshm]
[ldap]
ldap.max_links = -1
[mcrypt]
[dba]
[opcache]
[curl]
[openssl]

vim php-fpm.conf
[global]
pid = run/php-fpm.pid
include=/usr/local/php/etc/php-fpm.d/*.conf



vim www.conf
[www]
user = nginx
group = nginx
listen = 172.18.0.30:9000
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3


编写docker-compose.yml文件

version: \'2\'
services:
nginx:
container_name: nginx
build:
context: ./nginx
dockerfile: Dockerfile
ports:
- 80:80
networks:
lnmp:
ipv4_address: 172.18.0.10

mysql:
container_name: mysql
privileged: true
build:
context: ./mysql
dockerfile: Dockerfile
ports:

- 3306:3306
networks:
lnmp:
ipv4_address: 172.18.0.20
php:
depends_on:
- nginx
- mysql
build:
context: ./php
dockerfile: Dockerfile
container_name: php
ports:
- 9000:9000
networks:
lnmp:
ipv4_address: 172.18.0.30
volumes_from:
- nginx
- mysql
networks:
lnmp:
driver: bridge
ipam:
config:
- subnet: 172.18.0.0/16

#启动容器

docker-compose -f docker-compose.yml up -d
docker ps -a

docker学习6-docker-compose容器集群编排

前言

实际工作中我们部署一个应用,一般不仅仅只有一个容器,可能会涉及到多个,比如用到数据库,中间件MQ,web前端和后端服务,等多个容器。
我们如果一个个去启动应用,当项目非常多时,就很难记住了,所有需要一个配置文件,负责实现对Docker容器集群的快速编排。

docker-compose

简介

Docker-Compose项目是Docker官方的开源项目,负责实现对Docker容器集群的快速编排。
Docker-Compose将所管理的容器分为三层,分别是工程(project),服务(service)以及容器(container)。
Docker-Compose运行目录下的所有文件(docker-compose.yml,extends文件或环境变量文件等)组成一个工程,若无特殊指定工程名即为当前目录名。一个工程当中可包含多个服务,每个服务中定义了容器运行的镜像,参数,依赖。一个服务当中可包括多个容器实例,Docker-Compose并没有解决负载均衡的问题,因此需要借助其它工具实现服务发现及负载均衡。

Docker-Compose的工程配置文件默认为docker-compose.yml,可通过环境变量COMPOSE_FILE或-f参数自定义配置文件,其定义了多个有依赖关系的服务及每个服务运行的容器。

安装

docker-compose使用pip安装即可

pip install docker-compose

使用docker-compose version查看版本

[root@yoyo ~]# docker-compose version
/usr/lib/python2.7/site-packages/requests/__init__.py:80: RequestsDependencyWarning: urllib3 (1.22) or chardet (2.2.1) doesn\'t match a supported version!
  RequestsDependencyWarning)
docker-compose version 1.24.1, build 4667896
docker-py version: 3.7.3
CPython version: 2.7.5
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
[root@yoyo ~]# 

实例

docker-compose.yml文件

docker-compose已经安装成功,接下来去https://hub.docker.com上找个项目部署练练手,项目地址:https://hub.docker.com/r/easymock/easymock

docker-compose 文件内容

version: \'3\'

services:
  mongodb:
    image: mongo:3.4
    volumes:
      # ./data/db 数据库文件存放地址,根据需要修改为本地地址
      - \'./data/db:/data/db\'
    networks:
      - easy-mock
    restart: always

  redis:
    image: redis:4.0.6
    command: redis-server --appendonly yes
    volumes:
      # ./data/redis redis 数据文件存放地址,根据需要修改为本地地址
      - \'./data/redis:/data\'
    networks:
      - easy-mock
    restart: always

  web:
    image: easymock/easymock:1.6.0
    command: /bin/bash -c "npm start"
    ports:
      - 7300:7300
    volumes:
      # 日志地址,根据需要修改为本地地址
      - \'./logs:/home/easy-mock/easy-mock/logs\'
      # 配置地址,请使用本地配置地址替换
      # - \'./production.json:/home/easy-mock/easy-mock/config/production.json\'
    networks:
      - easy-mock
    restart: always

networks:
  easy-mock:

在本地新建一个文件夹/root/easymock,接着新建文件docker-compose.yml,把上面的内容写入到这个文件里,如下步骤

[root@yoyo ~]# mkdir /root/easymock
[root@yoyo ~]# cd /root/easymock/
[root@yoyo easymock]# touch docker-compose.yml
[root@yoyo easymock]# vi docker-compose.yml 
[root@yoyo easymock]# cat docker-compose.yml 
version: \'3\'

services:
  mongodb:
    image: mongo:3.4
    volumes:
      # ./data/db 数据库文件存放地址,根据需要修改为本地地址
      - \'./data/db:/data/db\'
    networks:
      - easy-mock
    restart: always

  redis:
    image: redis:4.0.6
    command: redis-server --appendonly yes
    volumes:
      # ./data/redis redis 数据文件存放地址,根据需要修改为本地地址
      - \'./data/redis:/data\'
    networks:
      - easy-mock
    restart: always

  web:
    image: easymock/easymock:1.6.0
    command: /bin/bash -c "npm start"
    ports:
      - 7300:7300
    volumes:
      # 日志地址,根据需要修改为本地地址
      - \'./logs:/home/easy-mock/easy-mock/logs\'
      # 配置地址,请使用本地配置地址替换
      # - \'./production.json:/home/easy-mock/easy-mock/config/production.json\'
    networks:
      - easy-mock
    restart: always

networks:
  easy-mock:

[root@yoyo easymock]# 

docker-compose up启动

使用docker-compose up 指令启动服务,可以看到依次启动三个容器

  • Creating easymock_redis_1 ... done
  • Creating easymock_mongodb_1 ... done
  • Creating easymock_web_1 ... done
[root@yoyo easymock]# docker-compose up
/usr/lib/python2.7/site-packages/requests/__init__.py:80: RequestsDependencyWarning: urllib3 (1.22) or chardet (2.2.1) doesn\'t match a supported version!
  RequestsDependencyWarning)
Creating network "easymock_easy-mock" with the default driver
Pulling mongodb (mongo:3.4)...
3.4: Pulling from library/mongo
f7277927d38a: Pull complete
8d3eac894db4: Pull complete
edf72af6d627: Pull complete
3e4f86211d23: Pull complete
5747135f14d2: Pull complete
f56f2c3793f6: Pull complete
f8b941527f3a: Pull complete
4000e5ef59f4: Pull complete
b2229b3c9065: Pull complete
63e3700b27e5: Pull complete
a77f0e7057fd: Pull complete
4ea8e2e4c04b: Pull complete
3214e739257b: Pull complete
d05fd2e6729f: Pull complete
Digest: sha256:0264d096073b0a862586cfe9a4a8d1a41e206b447d5f1043f241177d9cc70b7d
Status: Downloaded newer image for mongo:3.4
Pulling redis (redis:4.0.6)...
4.0.6: Pulling from library/redis
c4bb02b17bb4: Pull complete
58638acf67c5: Pull complete
f98d108cc38b: Pull complete
83be14fccb07: Pull complete
5d5f41793421: Pull complete
ed89ff0d9eb2: Pull complete
Digest: sha256:0e773022cd6572a5153e5013afced0f7191652d3cdf9b1c6785eb13f6b2974b1
Status: Downloaded newer image for redis:4.0.6
Pulling web (easymock/easymock:1.6.0)...
1.6.0: Pulling from easymock/easymock
7e6591854262: Pull complete
089d60cb4e0a: Pull complete
9c461696bc09: Pull complete
45085432511a: Pull complete
70a7567bd8cf: Pull complete
0f7e44bf28f5: Pull complete
523067e58a08: Pull complete
35e77c0527fd: Pull complete
0df0b0c81b8b: Pull complete
cd5e5a709bf4: Pull complete
d50e2d78de99: Pull complete
Digest: sha256:37d50204c694eb4e50a7ba66406736bc1ae631e0e230abf77573d5ed154fb5a2
Status: Downloaded newer image for easymock/easymock:1.6.0
Creating easymock_redis_1   ... done
Creating easymock_mongodb_1 ... done
Creating easymock_web_1     ... done

如果看到以下报错,permission denied, open \'logs/2019-08-18-info.log\',给log文件夹加个权限即可

chmod 777 /root/easymock/logs/

web_1      | WARNING: NODE_ENV value of \'production\' did not match any deployment config file names.
web_1      | WARNING: See https://github.com/lorenwest/node-config/wiki/Strict-Mode
web_1      | server started at http://0.0.0.0:7300
web_1      | events.js:182
web_1      |       throw er; // Unhandled \'error\' event
web_1      |       ^
web_1      | 
web_1      | Error: EACCES: permission denied, open \'logs/2019-08-18-info.log\'
web_1      | npm ERR! code ELIFECYCLE
web_1      | npm ERR! errno 1
web_1      | npm ERR! easy-mock@1.6.0 start: `cross-env NODE_ENV=production node app`
web_1      | npm ERR! Exit status 1
web_1      | npm ERR! 
web_1      | npm ERR! Failed at the easy-mock@1.6.0 start script.
web_1      | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
web_1      | 
web_1      | npm ERR! A complete log of this run can be found in:
web_1      | npm ERR!     /home/easy-mock/.npm/_logs/2019-08-18T15_02_51_366Z-debug.log

接着浏览器输入http://ip:7300/即可访问了

docker-compose挂后台运行

挂后台运行需加-d参数

docker-compose up -d

查看正在启动的docker容器使用docker ps

[root@yoyo easymock]# docker-compose up -d
/usr/lib/python2.7/site-packages/requests/__init__.py:80: RequestsDependencyWarning: urllib3 (1.22) or chardet (2.2.1) doesn\'t match a supported version!
  RequestsDependencyWarning)
Starting easymock_mongodb_1 ... done
Starting easymock_web_1     ... done
Starting easymock_redis_1   ... done
[root@yoyo easymock]# docker ps
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS              PORTS                    NAMES
026f41a372cf        easymock/easymock:1.6.0   "/bin/bash -c \'npm s…"   11 minutes ago      Up 7 seconds        0.0.0.0:7300->7300/tcp   easymock_web_1
5dbbb2086b1c        redis:4.0.6               "docker-entrypoint.s…"   11 minutes ago      Up 7 seconds        6379/tcp                 easymock_redis_1
f4772582dd03        mongo:3.4                 "docker-entrypoint.s…"   11 minutes ago      Up 7 seconds        27017/tcp                easymock_mongodb_1
1d07763a6eaa        tomcat                    "catalina.sh run"        7 weeks ago         Up 5 weeks          0.0.0.0:8090->8080/tcp   yoyotomcat
37a3b50d151c        mysql:5.6                 "docker-entrypoint.s…"   7 weeks ago         Up 5 weeks          0.0.0.0:3308->3306/tcp   mymysql
[root@yoyo easymock]# 

docker-compose停止运行

停掉服务使用docker-compose down

docker-compose down

[root@yoyo easymock]# docker-compose down
/usr/lib/python2.7/site-packages/requests/__init__.py:80: RequestsDependencyWarning: urllib3 (1.22) or chardet (2.2.1) doesn\'t match a supported version!
  RequestsDependencyWarning)
Stopping easymock_web_1     ... done
Stopping easymock_redis_1   ... done
Stopping easymock_mongodb_1 ... done
Removing easymock_web_1     ... done
Removing easymock_redis_1   ... done
Removing easymock_mongodb_1 ... done
Removing network easymock_easy-mock
[root@yoyo easymock]# 

以上是关于Docker-compose 容器集群的主要内容,如果未能解决你的问题,请参考以下文章

Docker-Compose 容器集群的快速编排

docker基础之docker-compose搭建consul集群

docker基础之docker-compose搭建consul集群

docker基础之docker-compose搭建consul集群

docker基础之docker-compose搭建consul集群

使用Docker-compose编排容器