Docker Compose

Compose is a tool for defining and running multi-container Docker applications. With Compose, you can use YML files to configure all the services your application needs. Then, with a single command, all services can be created and started from the YML file configuration. Compose uses three steps:

  • Use Dockerfile to define your application’s environment.
  • Use docker-comemage.yml to define the services that make up the application so that they can run together in an isolated environment.
  • Finally, execute the docker-compose up command to get the entire application up and running.

Compose the installation

Linux on Linux, you can download the binary package from Github, the latest release address: github.com/docker/comp… .

Run the following command to download the current stable version of Docker Compose:

$ sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Copy the code

Apply executable permissions to binaries:

$ sudo chmod +x /usr/local/bin/docker-compose
Copy the code

Create a soft chain:

$ sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
Copy the code

Test whether the installation is successful:

$docker-compose --version cocker-compose version 1.24.1, build 4667896bCopy the code

Note: For Alpine, the following dependencies are required: py-pip, python-dev, libffi-dev, openssl-dev, GCC, libc-dev, and make.

MacOS and Windows

Docker Desktop edition and Docker Toolbox for Mac and Windows already include Compose and other Docker apps, so Mac users don’t need to install Compose separately.

use

Create a test directory:

$ mkdir composetest
$ cd composetest
Copy the code

Create a file named app.py in the test directory

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:
            ifRetries == 0: raise exc retries -= 1 time.sleep(0.5) @app.route(raise exc retries -= 1 time.'/')
def hello():
    count = get_hit_count()
    return 'Hello World! I have been seen {} times.\n'.format(count)
Copy the code

In this example, redis is the host name of the Redis container on the application network using port 6379.

Create another file named requirements.txt in the composetest directory with the following contents:

flask
redis
Copy the code

2. Create Dockerfile

In the composetest directory, create a file named Dockerfile with the following contents:

FROM python:3.7-alpine WORKDIR /code ENV FLASK_APP app.py ENV FLASK_RUN_HOST 0.0.0.0 RUN apk add --no-cache GCC musl-dev  linux-headers COPY requirements.txt requirements.txt RUN pip install -r requirements.txt COPY . . CMD ["flask"."run"]
Copy the code

Dockerfile contents

  • FROM Python :3.7-alpine: Build an image FROM a Python 3.7 image.
  • WORKDIR /code: Set the working directory to /code.
  ENV FLASK_APP app.py
  ENV FLASK_RUN_HOST 0.0.0.0
Copy the code
  • Set the environment variables used by the flask command.

RUN apk add –no-cache GCC musl-dev linux-headers: Install GCC so that Python packages such as MarkupSafe and SQLAlchemy can compile and accelerate.

COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
Copy the code

Copy requirements.txt and install Python dependencies.

COPY.. : Copies the current directory in the. Project to the working directory in the. Mirror. CMD [” flask “, “run”]: The container provides the default execution command: flask run.

Create docker-comemage.yml

Create a file called docker-comemage. yml in your test directory and paste the following:

Docker-comemage. yml configuration file

Yaml configuration

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

The Compose file defines two services: Web and Redis.

  • Web: This Web service uses an image built from the current directory of Dockerfile. It then binds the container and host to the exposed port 5000. This sample service uses the Flask Web server’s default port 5000.
  • Redis: This Redis service uses the public Redis image of Docker Hub.

4. Build and run your application using the Compose command

In the test directory, run the following command to start the application:

docker-compose up
Copy the code

If you want to run the service in the background, add the -d parameter:

docker-compose up -d
Copy the code