In a microservice application composed of large Docker, the number of containers is very large. If you rely on traditional manual configuration for maintenance, it will be a nightmare for development and operation and maintenance. Compose emerged to solve this problem.
Compose Introduction
Compose's predecessor was Fig. After Fig was acquired by Docker, it was officially renamed Compose. Compose is backward compatible with Fig. Compose is a tool for defining and running multi-container Docker applications. It only requires a Compose configuration file and a simple command to create and run all the containers needed by the application. In the configuration file, all containers passservices
to define and usedocker-compose
Commands start or stop containers and all dependencies.
Install Compose
There are many ways to install Compose, and it is recommended to use it herecurl
Command to install. Before installing, make sure that Docker is installed on your machine and can runsudo docker version
Command to confirm whether Docker is installed. As of now, the latest release of Compose is1.11.2
, the following demonstrates installing Compose on a Linux host with Docker installed.
Installation is very simple, just execute the following command:
After waiting for the installation, execute the following command,docker-compose
Add executable permissions:
chmod +x /usr/local/bin/docker-compose
enterdocker-compose --version
The command can view the installation results.
In addition to this installation method, you can also install Compose into a Docker container through Python's pip command. For details, please refer to/compose/install/#install-as-a-container。
If you want to uninstall Compose, you can execute itsudo rm /usr/local/bin/docker-compose
Order.
Getting started with Compose
Below we demonstrate the steps of using Compose with a simple example, using Python to build a web application that uses the Flask framework and maintains a hit count in Redis (it doesn't matter even if you are not familiar with Python, you don't even need to install Python and Redis, we will get these dependencies from the container).
Create a project
First, you need a folder as the project folder:
mkdir composetest cd composetest
Create a project folderand 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) @('/') def hello(): count = ('hits') return 'Hello World! I have been seen {} times.\n'.format(count) if __name__ == "__main__": (host="0.0.0.0", debug=True)
Create a project folderand copy and paste the following code into the file:
flask redis
At this point, we have completed the work of creating new projects, coding, adding dependencies, etc.
Create a Dockerfile
Let's create oneDockerfile
The file is used to build a Docker image that contains all dependencies for running the web application, including the Python running environment.
Create a project folderDockerfile
and copy and paste the following content into the file:
FROM python:3.4-alpine ADD . /code WORKDIR /code RUN pip install -r CMD ["python", ""]
Let's explain this configuration file roughly:
- Use python-3.4-alpine as the base image
- Add the current directory to the image/code directory
- Set /code as working directory
- Install Python dependencies
- Set default execution commands
Define services in Compose file
Create a project folderand copy and paste the following content into the file:
version: '2' services: web: build: . ports: - "5000:5000" volumes: - .:/code redis: image: "redis:alpine"
This configuration file contains two services, namely web and redis. The web will use the Dockerfile file in the current directory to build the image, expose the 5000 port of the container to the host, and then mount the project folder to the /code directory in the container; redis will be built using the officially released image.
Build and run
Run the following command to build and run the container:
sudo docker-compose up
After the container is built and started, you can enter http://localhost:5000 in the browser to view the results. The page will print "Hello World! I have been seen 1 times." After refreshing the page, the count will accumulate to 2.
Update the application
Since the project folder is mounted in the container, we can directly modify the application of the project folder, and the modification results are immediately reflected into the container without restarting the container. Modify the return value in the hello method in the 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 results have been updated.
Other commands for Compose
The above-mentioned Componse uses commands to build and start the container, which is started in the previous mode. If you want the future station to start, you can add the parameter -d, such as the following:
sudo docker-compose up -d
docker-compose ps
The command can view the running container:
liuwei@liuwei-Ubuntu:~$ sudo docker-compose ps Name Command State Ports ------------------------------------------------------------------------------------- composetest_redis_1 redis ... Up 6379/tcp composetest_web_1 python Up 0.0.0.0:5000->5000/tcp
If usingsudo docker-compose up -d
Command to start the background mode, you can usedocker-compose stop
The command stops.docker-compose down --volumes
The command can stop the container and delete it.--volumns
Indicates that the redis data file directory is deleted at the same time.
More commands about Compose can be usedsudo docker-compose --help
Check.
The above is a basic usage process of Compose. You can find that Compose willdocker run
The commands are integrated into oneIn the configuration file, it is very convenient to manage large Docker clusters, for example, you can use multiple
service
Combining into more complexservice
group, for eachservice
Specify differentDockerfile
, and then put themlink
Together. I hope it will be helpful to everyone's learning and I hope everyone will support me more.