Docker Compose is one of Docker's official orchestration projects, responsible for rapidly deploying distributed applications in clusters.
Introduction to Compose
The Compose project is the official open source project of Docker, responsible for the rapid orchestration of Docker container clusters. Compose's positioning is "Defining and running multicontainer Docker applications", and its predecessor is the open source project Fig.
Using a Dockerfile template file, users can easily define a single application container. However, in daily work, there are often situations where multiple containers are required to cooperate with each other to complete a certain task. For example, to implement a web project, in addition to the web service container itself, it is often necessary to add a back-end database service container, and even a load balancing container.
Compose meets exactly that need. It allows users to define a set of associated application containers as a project through a single docker-compose.yml template file (YAML format).
There are two important concepts in Compose:
- Service (service): An application container, which can actually include several container instances running the same image
- Project ( project ): A complete business unit consisting of a set of associated application containers, defined in the dockercompose.yml file.
Compose's default management object is the project, which facilitates lifecycle management of a set of containers in the project through subcommands. It can be seen that a project can be associated with multiple services (containers), and Compose manages the project for the project
The Compose project is written in Python, and the implementation calls the API provided by the Docker service to manage containers. Therefore, as long as the operating platform supports the Docker API, you can use Compose on it for orchestration management.
Install and uninstall
Compose can be installed through Python's package management tool pip, or you can directly download compiled binaries for use, or even run directly in a Docker container. The first two methods are traditional methods, suitable for installation and use in the local environment; the last method does not damage the system environment and is more suitable for cloud computing scenarios. Docker for Mac and Docker for Windows come with docker-compose binary files, which can be used directly after installing Docker. For Linux system, please use the method described below to install.
binary package installation
Installation on Linux is also very simple, just download the compiled binaries directly from the official GitHub Release.
For example, directly download the corresponding binary package on a Linux 64-bit system.
sudo curl -L https://github.com/docker/compose/releases/download/1.17.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
For uninstallation, if it is installed by binary package, just delete the binary file.
sudo rm /usr/local/bin/docker-compose
pip install
This way Compose is installed from the pip source as a Python application. Execute the installation command:
sudo pip install -U docker-compose
When using pip to install, uninstall using the following command:
sudo pip uninstall docker-compose
Getting Started
Below we simulate a website whose role is to be able to record the number of visits to a page. We use Flask to develop, cache uses redis
Write the wen application
Create a new folder web, and create an app.py file in this folder with the following contents:
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! {}'.format(count)
if __name__ == "__main__":
app.run(host="0.0.0.0",debug=True)
Write Dockerfile
FROM python:3.6-alpine
ADD . /code
WORKDIR /code
RUN pip install redis flask
CMD ["python","app.py"]
docker-compose.yml
Write the docker-compose.yml file, which is the main template file used by Compose.
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
redis:
images: "redis:alpine"
run the compose project
docker-compose up
At this time, when accessing the local port 5000, the count will increase by 1 every time the page is refreshed.