Docker Container Choreographer Compose (Starter)

  • 2020-11-26 19:04:21
  • OfStack

The number of containers in a large Docker microservice application is so large that relying on traditional manual configuration for maintenance can be a development and operational nightmare. Compose was created to solve this problem.

Compose profile

Compose, formerly known as Fig, officially changed its name to Compose after its acquisition by Docker, and Compose is compatible with Fig downwards. Compose is a tool for defining and running multi-container Docker applications. It takes only one Compose configuration file and one simple command to create and run all the containers needed for the application. In the configuration file, all containers pass through services To define and use docker-compose The command starts or stops the container and all dependent containers.

Install Compose

There are several ways to install Compose, which is recommended here curl Command installation. Before installing, make sure you have Docker installed and running on your machine sudo docker version Command to verify that Docker is installed. So far, the latest release of Compose is 1.11.2 , to demonstrate installing Compose on an Linux host with Docker already installed.

The installation is simple, just execute the following command:

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

After waiting for the installation to complete, execute the following command, as docker-compose Add executable permissions:


chmod +x /usr/local/bin/docker-compose

The input docker-compose --version Command to view the installation results.

In addition to the installation, can also through Python pip command to install or install into Docker Compose containers, for details, please see https: / / docs docker. com/compose/install / # install - as a -- container.

If you want to uninstall Compose, you can do so sudo rm /usr/local/bin/docker-compose Command.

Introduction to Compose

Here's a simple example of how to use Compose, using Python to build an Web application, using the Flask framework, and maintaining a hit count in Redis (even if you're not familiar with Python, you don't even need to install Python and Redis, we'll get these dependency environments from the container).

Create a project

First, a folder is needed as the project folder:


mkdir composetest
cd composetest

Create 1 under the project folder app.py And copy and paste the following code into the file:


from flask import Flask
from redis import Redis

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

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

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

Create 1 under the project folder requirements.txt And copy and paste the following code into the file:


flask
redis

At this point, we have completed the new project, coding, adding dependencies and so on.

Create Dockerfile

So let's create one docker-compose0 The file is used to build the Docker image, which contains all the dependencies to run the Web application, including the Python runtime environment.

Create 1 under the project folder docker-compose0 File and copy and paste the following into the file:


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

Roughly explain 1 this configuration file:

python-3.4-alpine was used as the base image Adds the current directory to the image under /code Set /code to the working directory Install Python dependencies Set the default execution command

Define services in the Compose file

Create 1 under the project folder docker-compose.yml File and copy and paste the following into the file:


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

The configuration file contains two services, web and redis. web builds the image using the Dockerfile file in the current directory, exposes the container's port 5000 to the host, and then mounts the project folder to the /code directory in the container. redis USES the official release of the mirror build.

Build and run

Execute the following command to build and run the container:


sudo docker-compose up

After the container is built and started, you can type http://localhost:5000 in your browser to see the results. The page will print "Hello World! I have been seen 1 times. ", refresh the page and the count will add up to 2.

Update the application

Since the project folder is mounted in the container, we can modify the application of the project folder directly, and the result of the modification is immediately reflected in the container without restarting the container. Change the return value of the hello method in the ES160en.py file to the following:

[

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

]

After saving, refresh the browser and find that the print result has been updated.

Other commands of Compose

The Componse mentioned above USES the command to build and start the container, which starts as a foreground object. If you want a later stage to start, you can add the -ES178en parameter, as follows:


sudo docker-compose up -d

docker-compose ps Command to see the running container:


liuwei@liuwei-Ubuntu:~$ sudo 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

If you are using sudo docker-compose up -d Command after the station mode to start, can be used docker-compose stop Command stop. docker-compose down --volumes The command stops the container and removes it, --volumns Delete the redis data file directory.

More commands on Compose can be accessed sudo docker-compose --help Look at it.


Related articles: