This is the 18th day of my participation in the August Challenge
Introduction to the
Docker-compose project is the official open source project of Docker, which is responsible for the rapid choreography of Docker container clusters.
Docker-compose manages containers in three layers, namely project, Service and Container.
Docker-compose all files in the docker-compose running directory (docker-compose. Yml, extends file, environment variable file, etc.) form a project. If no project name is specified, the project name is the current directory name. A project can contain multiple services, each of which defines images, parameters, and dependencies for the container to run. Docker-compose can contain multiple container instances in a single service. Docker-compose does not solve the problem of load balancing, so it needs to use other tools to realize service discovery and load balancing.
Docker-compose’s project configuration file defaults to docker-comemage. yml, which can be customized by using the environment variable COMPOSE_FILE or -f parameter. The configuration file defines multiple dependent services and the container in which each service runs. Using a Dockerfile template allows users to easily define a separate application container. In the work, it is common to encounter the need for multiple containers to cooperate to complete a task. For example, to implement a Web project, in addition to the Web service container itself, there is often a database service container on the back end, and even a load balancing container.
Compose allows users to define a set of associated application containers as a project from a single docker-comemage. yml template file (YAML format).
Docker-compose project, written in Python, calls apis provided by the Docker service to manage the container. Therefore, you can leverage Compose for orchestration management on any platform you operate on that supports the Docker API.
Install the docker – compose
1. Download the current stable version of Docker Compose
curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Copy the code
2. Apply executable permissions to binary files
chmod +x /usr/local/bin/docker-compose
Copy the code
3. Check whether the installation is successful
docker-compose --version
Copy the code
The basic use
Parameter Options:
Yml -p --project-name name specifies the project name. By default, use the current directory as the project name. --verbose Displays more debugging information -v, -version Prints the version and exits. --log-level level Defines log levels (DEBUG, INFO, WARNING, ERROR, CRITICAL).Copy the code
Common commands:
docker-compose up # start container
docker-compose down Close and delete the container
docker-compose [start | stop | pause | restart] Start/stop/pause/restart the container
docker-compose run NAME ping baidu.com Run the command in the specified container
docker-compose kill # mechanism stop container
docker-compose scale # set the number of containers to run the service
docker-compose ps List all containers in the project
docker-compose rm # delete container
docker-compose logs # check log
docker-compose bulid Build containers in your project
docker-compose push # push mirror
docker-compose pull # pull dependent mirror
docker-compose config View the project container configuration
docker-compose create Create a container for the service
docker-compose exec NAME /bin/bash Enter the specified containerDocker-compose port NAME Port numberDisplay public ports mapped to a container port
Copy the code
Docker-compose template file
The Compose template file is a YAML file that defines services, networks, and volumes. The default path for the Compose template file is docker-comemess. yml in the current directory, and you can use. Yml or. Yaml as the file extension. Docker-compose standard template file should contain version, Services, networks three parts, the most important is the services and networks two parts.
Parameter is introduced
Version: For Compose, there are three versions: Version 1, Version 2, and Version 3. For Compose, Version 1 and Version 2 (Docker Engine 1.10.0+) are available. Version 2 supports more directives. Version 1 will be deprecated in future.
Image: specifies the image name or ID of the service. Compose will try to pull the mirror if it doesn’t already exist locally.
Build: In addition to the specified image, the service can also be based on a Dockerfile. When starting with up, the build tag is build, and you can specify the path of the Dockerfile folder. Compose will automatically build an image using Dockerfile and then use the image to start the service container.
Context: The option can be either the path of the Dockerfile file or a URL linked to a Git repository. When the value provided is relative to the path, it is resolved to be relative to the path of the authored file, which is also the context sent to the Docker daemon
Dockerfile: To use the dockerfile file to build, the build path must be specified
Commond: Use command to override the commands executed by default after the container is started.
Container_name: specifies the project name and service name
Depends_on: When using Compose, the most important benefit is to have fewer start commands. However, the order in which the project container is started is usually required. If you start the container directly from the top down, you will inevitably fail to start the container due to container dependencies. For example, if you start the application container without starting the database container, the application container will exit because it cannot find the database. The Depends_on tag is used to resolve container dependencies and startup priorities
Ports: ports that map services to hosts
Extra_hosts: Adds a host name tag that adds records to the /etc/hosts file.
Volumes: Mount a directory or an existing data volume container
DNS: User-defined DNS server. It can be a value or it can be a list.
Expose: Exposes ports that do not map to the host and are accessible only to services that can be connected.
Net: Set the network mode.
The instance
Here is an official example
1. Create a project folder
mkdir composetest
cd composetest
Copy the code
2. Write a presentation project
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)
Copy the code
3. Write requirements. TXT dependency list file
flask
redis
Copy the code
4. Compile a Dockerfile file
# syntax=docker/dockerfile:1FROM 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 EXPOSE 5000 COPY . . CMD ["flask"."run"]
Copy the code
5. Write docker-comemess. yml file
version: "3.9"
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"
Copy the code
6. Start the project
docker-compose up -d
Copy the code
7. Test
The demo shows how many times the page has been visited
View the container logs
Increase the number of containers with the Docker-compose scale
The resources
Docker official documentation
Docker-compose for Docker entry
Recommended reading
Linux Shell programming basics!
Linux Sudo and Sudoers
Linux TCP kernel parameter Settings and tuning (details)!
Operation and maintenance must know Linux RAID details!
Linux Service Management!
Server Hardware Guide!