Docker(四)Docker Compose

Docker Docker-Compose is a service orchestration, is a definition for Docker and run the tool in complex applications, allowing users to deploy distributed applications in a cluster.

The first two articles we introduced Dockerfile use Docker (two): Dockerfile use introduction , we know Dockerfile use a template file you can define a single application container, you need to define a plurality of containers if you need service orchestration. There are many technical service orchestration solutions, today introduces the official product Docker Docker Compose.

Dockerfile allows users to manage a single application container; and Compose application allows users to define a set of associated container in a template (YAML format) in (to be referred to a project, namely the project), such as a Web service container plus on the back-end database server containers.

Docker Compose Introduction

You can easily define a multi-use container with a profile by Docker-Compose user, then all instructions dependent on an installation of this application, be built. Docker-Compose how to solve the problem between the container and container management choreography.

Docker Compose operating principle

Compose There are two important concepts:

  • Service (service): a container application, the container may include several instances actually running the same image.
  • Project (project): a unit represented by a complete set of service associated with the application container composed of docker-compose.yml definition file.

It can be associated with a project by a plurality of services (containers) made of, for Compose project management, project a set of containers for easy lifecycle management subcommand.

Compose project written by Python, calling the API Docker services provided on implementation to manage the container. Therefore, as long as the operating platform to support Docker API, you can manage to orchestrate use Compose thereon.

Docker Compose installation

Docker Docker Compose a separate product, and therefore need to be installed after the installation Docker Docker Compose separately.

method one:

#下载
sudo curl -L https://github.com/docker/compose/releases/download/1.20.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose #安装 chmod +x /usr/local/bin/docker-compose #查看版本 docker-compose version 

Method Two:

#安装pip
yum -y install epel-release
yum -y install python-pip #确认版本 pip --version #更新pip pip install --upgrade pip #安装docker-compose pip install docker-compose #查看版本 docker-compose version 

A recommended method for installation, the installation is successful input docker-compose versionwill return the version information docker-compose, as follows:

[root@localhost ~]# docker-compose version
docker-compose version 1.19.0, build 9e633ef
docker-py version: 2.7.0
CPython version: 2.7.13
OpenSSL version: OpenSSL 1.0.1t  3 May 2016

The above information appears, indicating that the docker-compose a successful installation

Installation completion Tool (optional)

In order to help us enter the command, you can also install Docker's completion hints tool to help us to quickly enter the command

#安装
yum install bash-completion

#下载docker-compose脚本
curl -L https://raw.githubusercontent.com/docker/compose/$(docker-compose version --short)/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose 

Quick Start

There's nothing to learn than to a small example of better practice your hand, and a simple example we take a look at the official website of the use of docker compose.

We have designed such a scenario, a Web service to start using Python, output a hello()method, every time access is counted in Redis cache, and the statistical results to the printed page.

The first step, create a Python service

Create a project path:

mkdir composetest
cd composetest

Create a directory in the app.pyfile

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) 

In this example, redis network within the container using the default port 6379. Python content of this program is that after starting Redis and output connection  hello()method, when the cumulative number of visits per visit and the results are returned to the page.

Created in the same directory as requirements.txtthe file, add project dependencies python package:

flask
redis

Flask is a tiny Python Web development framework.

Next, create Dockerfile

Let's write a Dockerfile to define Docker mirror, this mirror contains dependencies Python and Python environment.

Also in this directory, we create a Dockerfile file.

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

This code says:

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

The third step is to use Compose file defines a service

In the current directory, we create a docker-compose.yml file, as follows:

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

This document defines two service Compose, a Pyhon Web services and redis service.

  • Pyhon Web Services: Use Dockerfile build a current mirror. The port 5000 is mapped to the interior of the container Web port 5000 of the host; and redis Web container and the container is connected.
  • redis service: the container is created directly from the official redis mirror.

The fourth step, start applications compiled using Compose

Use the command docker-compose upto start

version: '2'
services:
  web:
    build: .
    command: python app.py
    ports:
     - "5000:5000"
    volumes:
     - .:/code
  redis:
    image: "redis:alpine" 

After a successful start in the browser at: http://ipaddress:5000/ returns the following:

Hello World! I have been seen 1 times.

Refresh visit back again

Hello World! I have been seen 2 times.

Constantly refresh numbers will continue to grow.

Docker Compose commonly used commands

Use docker-compose up -dstarted in the background service

[root@localhost composetest]# docker-compose up -d
Starting composetest_web_1 ... 
Starting composetest_web_1 ... done

Use docker-compose psthe command to view the start of the service

[root@localhost composetest]# docker-compose ps
       Name                      Command               State           Ports         
-------------------------------------------------------------------------------------
composetest_redis_1   docker-entrypoint.sh redis ...   Up      6379/tcp              
composetest_web_1     python app.py                    Up      0.0.0.0:5000->5000/tcp

Use docker-compose stopstop service.

[root@localhost composetest]# docker-compose stop
Stopping composetest_web_1   ... done
Stopping composetest_redis_1 ... done

Other commonly used commands

#查看帮助
docker-compose -h

# -f  指定使用的 Compose 模板文件,默认为 docker-compose.yml,可以多次指定。
docker-compose -f docker-compose.yml up -d #启动所有容器,-d 将会在后台启动并运行所有的容器 docker-compose up -d #停用移除所有容器以及网络相关 docker-compose down #查看服务容器的输出 docker-compose logs #列出项目中目前的所有容器 docker-compose ps #构建(重新构建)项目中的服务容器。服务容器一旦构建后,将会带上一个标记名,例如对于 web 项目中的一个 db 容器,可能是 web_db。可以随时在项目目录下运行 docker-compose build 来重新构建服务 docker-compose build #拉取服务依赖的镜像 docker-compose pull #重启项目中的服务 docker-compose restart #删除所有(停止状态的)服务容器。推荐先执行 docker-compose stop 命令来停止容器。 docker-compose rm #在指定服务上执行一个命令。 docker-compose run ubuntu ping docker.com #设置指定服务运行的容器个数。通过 service=num 的参数来设置数量 docker-compose scale web=3 db=2 #启动已经存在的服务容器。 docker-compose start #停止已经处于运行状态的容器,但不删除它。通过 docker-compose start 可以再次启动这些容器。 docker-compose stop 

reference

Get started with Docker Compose
use container arrangement Docker-Compose

Guess you like

Origin www.cnblogs.com/eer123/p/12100793.html