Docker combat six (Docker Compose)

Docker Compose

It is a tool you can define and run multiple applications docker, you can use yaml file to configure your applications. Just perform a simple command can create it from the configuration and start all services.

Compose for all environments: production, continuous integration, development, testing and CI workflow

The use of three steps:

  • Need to install the docker, in order to use multiple running Dockerfile

  • In docker-compose composition as defined in the service application. So that they can run together in an isolated environment.

  • Running docker-compose upstart all your applications

Enter the following topic: how to install and deploy a simple application

Install Docker Compose(linux)

Docker Compose installation must be installed docker, details can refer to Get Started with Docker

# 下载
sudo curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# 授权
sudo chmod +x /usr/local/bin/docker-compose
# 查看版本
docker-compose --version

See the version information of the installation is successful

[root@localhost composetest]# docker-compose --version
docker-compose version 1.23.2, build 1110ad01

Building a simple application

Referring to the official website compose-demo
first create a project pathcomposetest

$ mkdir composetest
$ cd composetest

1 build python application

Create a named app.pyfile in your project path

import time

import redis
from flask import Flask


app = Flask(__name__)
cache = redis.Redis(host='redis', port=6379)


def get_hit_count():
    retries = 5
    while True:
        try:
            return cache.incr('hits')
        except redis.exceptions.ConnectionError as exc:
            if retries == 0:
                raise exc
            retries -= 1
            time.sleep(0.5)


@app.route('/')
def hello():
    count = get_hit_count()
    return 'Hello World! I have been seen {} times.\n'.format(count)

if __name__ == "__main__":
    app.run(host="0.0.0.0", debug=True)

File content is roughly: Set the default port 6379 host redis native, started redis connection and perform hello (), the output of visits to the page.

2 build Dockerfile file

Under the project to create a path Dockerfile, as follows

FROM python:3.4-alpine
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD ["python", "app.py"]

Roughly meaning:

  • Use base image Python 3.4
  • The image mapped to the current directory / code directory
  • Set the working directory is / code
  • Python installation dependencies
  • Start app.py program

There pit, use python-3.4 will build error

Fatal Python error: getentropy() failed
ERROR: Service 'web' failed to build: The command '/bin/sh -c pip install -r requirements.txt' returned a non-zero code: 139

Modify python python-3.5 version can be

3 Build docker-compose.yml file

Created in the current directorydocker-compose.yml

version: '3'
services:
  web:
    build: .
    ports:
     - "5000:5000"
  redis:
    image: "redis:alpine"

Compose a web service defines a two redis

  • Building web applications, built from Dockerfile in the current directory, and open port 5000
  • Use official redis image library, the default port 6379

This enables access 6379 python script, hits plus one.

4 run and build docker compose

carried out docker-compose up

Building web
Step 1/5 : FROM python:3.4-alpine3.8
3.4-alpine3.8: Pulling from library/python
c87736221ed0: Pull complete
c3f51b0d0765: Pull complete
e919dffdee54: Pull complete
f0766e77b512: Pull complete
bcf776216162: Pull complete
Digest: sha256:e88f2ce19bcabb986adb3a87b5effc7eaf5900b8d6049ffb85ca3dd59df0c466
Status: Downloaded newer image for python:3.4-alpine3.8
 ---> f6d3872c2a66
Step 2/5 : ADD . /code
 ---> 7a4e55428219
 ...
 Creating composetest_redis_1 ... done
Creating composetest_web_1   ... done
Attaching to composetest_web_1, composetest_redis_1

accesshttp://192.168.124.16:5000/
page

5 modify and add to mount compose.yml

Add bindings for web services

version: '3'
services:
  web:
    build: .
    ports:
     - "5000:5000"
    volumes:
     - .:/code
  redis:
    image: "redis:alpine"

6 Restart docker-compose

[root@localhost composetest]# docker-compose restart
Recreating composetest_web_1 ... done
Starting composetest_redis_1 ... done
Attaching to composetest_redis_1, composetest_web_1
redis_1  | 1:C 21 Mar 2019 02:38:38.862 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1  | 1:C 21 Mar 2019 02:38:38.862 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1  | 1:C 21 Mar 2019 02:38:38.862 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf

7 Modify the application configuration

Modify app.py return information

return 'Hello Chen! I have been seen {} times.\n'.format(count)

Visit again
! [Insert Picture description here] (https://img-blog.csdnimg.cn/20190321111724948.png

8 Docker Compose commonly used commands

构建建启动nignx容器
docker-compose up -d nginx                     
登录到nginx容器中
docker-compose exec nginx bash            
删除所有nginx容器,镜像
docker-compose down                              
显示所有容器
docker-compose ps                                   
重新启动nginx容器
docker-compose restart nginx                   
在php-fpm中不启动关联容器,并容器执行php -v 执行完成后删除容器
docker-compose run --no-deps --rm php-fpm php -v  
构建镜像
docker-compose build nginx                         
不带缓存的构建
docker-compose build --no-cache nginx   
查看nginx的日志 
docker-compose logs  nginx                     
查看nginx的实时日志
docker-compose logs -f nginx                   

验证(docker-compose.yml)文件配置,当配置正确时,不输出任何内容,当文件配置错误,输出错误信息。 
docker-compose config  -q                        
以json的形式输出nginx的docker日志
docker-compose events --json nginx       
暂停nignx容器
docker-compose pause nginx                 
恢复ningx容器
docker-compose unpause nginx             
删除容器(删除前必须关闭容器)
docker-compose rm nginx                       
停止nignx容器
docker-compose stop nginx                    
启动nignx容器
docker-compose start nginx                    

Reference Documents

Docker Compose official installation
Docker-Compose official command Daquan
docker-compose v3 version Command Reference


Not matter of the today will drag tomorrow!

Published 83 original articles · won praise 58 · views 170 000 +

Guess you like

Origin blog.csdn.net/qq_38423105/article/details/88710228
Recommended