docker learning 6-docker-compose container cluster arrangement

Foreword

The actual work we deploy an application, not just a general container, may involve multiple, such as used in the database, middleware MQ, web front-end and back-end services, and other containers.
If we go one by one to start the application, when the project is very large, it is difficult to remember all need a configuration file, responsible for fast layout of Docker container cluster.

docker-compose

Brief introduction

Docker Docker-Compose official project is an open source project, responsible for fast layout of Docker container cluster.
Docker-Compose will be managed by a container divided into three levels, namely the project (project), service (service) and container (container).
All files (docker-compose.yml, extends file or environment variable file, etc.) in Docker-Compose run up a project directory, in the absence of special designated project name is the name of the current directory. A project which may comprise a plurality of services, each defined image container runs, parameters dependent. A service which may include multiple container instance, Docker-Compose does not solve the problem of load balancing, and therefore need to use other tools to achieve service discovery and load balancing.

Docker-Compose project configuration files by default docker-compose.yml, can COMPOSE_FILE through the environment variable or -f parameter custom configuration file, which defines several services that depend on each service and run a container.

installation

docker-compose can be installed using pip

pip install docker-compose

Use docker-compose version View 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 ~]# 

Examples

docker-compose.yml file

docker-compose been successfully installed, then go https://hub.docker.com on hand and thought to find a project to deploy the project Address: https://hub.docker.com/r/easymock/easymock

docker-compose the file contents

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:

Create a new folder in the local / root / easymock, then the new file docker-compose.yml, the above content is written to the file, the steps of

[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 to start

Use docker-compose up command to start the service, you can start to see the turn three containers

  • 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

If you see the following error, permission denied, open 'logs / 2019-08-18-info.log', to add to the log folder permissions to

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! [email protected] 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 [email protected] 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

The browser then enter http: // ip: 7300 / to visit

docker-compose hanging background

Hanging running in the background need to add -d parameter

docker-compose up -d

View is starting docker containers 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 stop running

Service stopped using the 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]# 

Guess you like

Origin www.cnblogs.com/yoyoketang/p/11374481.html