1. What is docker-compose
Compose project is derived from previous FIG project. It is written in Python language and has a good cooperation with Docker/Swarm.
Docker Compose Compose is a tool for composing Docker containers. It defines and runs multiple container applications, and can start multiple containers with one command. Docker Compose does not require shell scripts to start containers.
Compose manages multiple Docker containers through a single configuration file, in which all containers are defined by services, and then uses the Docker-compose script to start, stop, and restart the application, as well as the services in the application and all containers that depend on the service. This is ideal for scenarios where multiple containers are used in combination for development.
The default docker-compose template file is docker-comemage. yml, where each service defined must be automatically built by specifying an image or build directive (requiring a Dockerfile) via the image directive.
Most of the other instructions are similar to those in Docker Run.
Using Compose basically consists of three steps:
1.Dockerfile defines the running environment of the application
2. Docker-comemess. yml defines the services that make up the application
Docker-compose up starts the entire application
2. Install Compose
Two ways to install Docker-compose
1. Download the docker-compose binary file from Github and install it
$sudo curl -l https://github.com/docker/compose/releases/download/1.16.1/docker-compose- ` ` uname - s - ` uname -m ` - o /usr/local/bin/docker-composeCopy the code
$ sudo chmod +x /usr/local/bin/docker-composeCopy the code
Test installation results
$docker-compose --version docker-compose version 1.16.1, build 1719cebCopy the code
2. PIP installation
2.1 install python-pip
yum -y install epel-release
yum -y install python-pipCopy the code
2.2 install Docker-compose
pip install docker-composeCopy the code
After the docker-compose installation is complete, run the version query command to install docker-compose
[root@swarm01 fendo]# docker-compose version docker-compose version 1.21.2, build a133471 docker-py version: 3.3.0 CPython Version: 2.7.5 OpenSSL Version: OpenSSL 1.0.2K - FIPS 26 Jan 2017Copy the code
3. Docker-compose. yml configuration file description
Docker-compose is a standard example of a yaml docker compose configuration file
version: "3" services: redis: image: redis:alpine ports: - "6379" networks: - frontend deploy: replicas: 2 update_config: Parallelism: 2 delay: 10s restart_policy: condition: on-failure DB: image: Postgres :9.4 volumes: - db-data:/var/lib/postgresql/data networks: - backend deploy: placement: constraints: [node.role == manager] vote: image: dockersamples/examplevotingapp_vote:before ports: - 5000:80 networks: - frontend depends_on: - redis deploy: replicas: 2 update_config: parallelism: 2 restart_policy: condition: on-failure result: image: dockersamples/examplevotingapp_result:before ports: - 5001:80 networks: - backend depends_on: - db deploy: replicas: 1 update_config: parallelism: 2 delay: 10s restart_policy: condition: on-failure worker: image: dockersamples/examplevotingapp_worker networks: - frontend - backend deploy: mode: replicated replicas: 1 labels: [APP=VOTING] restart_policy: condition: on-failure delay: 10s max_attempts: 3 window: 120s placement: constraints: [node.role == manager] visualizer: image: dockersamples/visualizer:stable ports: - "8080:8080" stop_grace_period: 1m30s volumes: - "/var/run/docker.sock:/var/run/docker.sock" deploy: placement: constraints: [node.role == manager] networks: frontend: backend: volumes: db-data:Copy the code
A standard configuration file should contain version, Services, and Networks, among which services and Networks are the most critical parts
3.1 file configuration
The compose file is a YAML file that defines services, networks, and volumes. The default path for the Compose file is./ docker-comedy.yml
The service definition contains the configuration applied to each container started for the service, like passing command line arguments docker Container Create. Similarly, networks and volumes are defined similarly to Docker Network Create and Docker Volume Create.
Just as Docker Container Create specifies options in Dockerfile such as CMD, EXPOSE, VOLUME, and ENV, by default you don’t need to specify them again docker-comedy.yml.
You can use environment variables in configuration values using the Bash class ${VARIABLE} syntax.
Tip: you can use.yml or.yaml as file extensions
Version 3.2,
For Compose,Version 1,Version 2, and Version 3 are available in Docker Engine 1.10.0+. Version 2 supports more directives. Version 1 is not declared. The default Version is “Version 1”. Version 1 will be deprecated in future.
3.3. Configuration Options
1. Bulid
build: /path/to/build/dirCopy the code
It could be a relative path
build: ./dirCopy the code
Set the context root directory and specify the Dockerfile based on that directory
build: context: .. / dockerfile: path/of/DockerfileCopy the code
2. context
The context option can be either the path to the Dockerfile file or a URL linked to a Git repository. When the value provided is relative to the path, it is resolved to the path relative to the writing file. This directory is also the contextbuild sent to the Docker daemon:
context: ./dirCopy the code
3. Dockerfile
build:
context: .
dockerfile: Dockerfile-alternateCopy the code
4.image
services:
web:
image: nginxCopy the code
Image: redis image: Ubuntu :14.04 image: tutum/ influxDB image: a4bc65fdCopy the code
5. args
ARG fendo
ARG password
RUN echo "Build number: $fendo"
RUN script-requiring-password.sh "$password"Copy the code
Then specify parameters under Build, which can pass maps or lists
build:
context: .
args:
fendo: 1
password: fendoCopy the code
or
build:
context: .
args:
- fendo=1
- password=fendoCopy the code
You can omit this value when specifying build parameters, in which case the build-time value defaults to the value in the run environment
args:
- fendo
- passwordCopy the code
6.command
Use command to override the commands that are executed by default after the container is started.
command: bundle exec thin -p 3000Copy the code
This command can also be a list, with a method similar to dockerfile:
command: ["bundle", "exec", "thin", "-p", "3000"]Copy the code
7.container_name
Compose’s container name format is: < project name >< service name >< serial number >
You can customize the project name and service name, but if you want full control over the container name, you can specify it using this tag:
container_name: appCopy the code
So the name of the container is app.
8.depends_on
The main advantage of using Compose is that there are fewer start commands, but the order in which project containers are started is very specific. If you start the container from the top down, you will inevitably fail due to container dependencies.
Depends_on is a depends_on tag that solves the dependency and startup sequence of the application container. If the application container is not started, the application container will exit because the database cannot be found.
For example, the following container starts the redis and DB services first and starts the Web service last:
version: '3'
services:
web:
build: .
depends_on:
- db
- redis
redis:
image: redis
db:
image: postgresCopy the code
Note that by default, when you start a Web service using docker-compose up Web, both redis and DB services are also started because the dependencies are defined in the configuration file.
9.pid
pid: "host"Copy the code
Set the PID mode to the host PID mode and share the process namespace with the host system. Containers use this tag to access and manipulate the namespaces of other containers and hosts.
10.ports
Label of the mapped port.
Using the HOST:CONTAINER format or just specifying the port of the CONTAINER, the HOST randomly maps the port.
Ports: - "3000" - "8000-8000" - "49100:22" - "127.0.0.1:8001:8001"Copy the code
11.extra_hosts
Add the host name tag to the /etc/hosts file, similar to the Docker client’s –add-host:
Extra_hosts: - "somehost:162.242.195.82" - "otherhost:50.31.209.229"Copy the code
Check the internal hosts of the container after startup:
162.242.195.82 somehost
50.31.209.229 otherhostCopy the code
12.volumes
You can mount a directory or an existing volume CONTAINER in the format of [HOST:CONTAINER] or in the format of [HOST:CONTAINER:ro], where the volume is read-only for the CONTAINER, to protect the file system of the HOST. The path specified for Compose’s data volume can be a relative path, using. Or.. To specify the relative directory.
The format of a data volume can be any of the following:
Volumes: // Just specify a path and Docker will automatically create a data volume. - /var/lib/mysql // Using the absolute path to mount data volumes - /opt/data:/var/lib/mysql // Mount the data volumes to the container using the relative path centered in the Compose configuration file. -./cache:/ TMP /cache // Uses the relative path of the user (~/ indicates /home/user directory/or /root/). - ~ / configs: / etc/configs / : ro / / named after the existing data volume. - datavolume:/var/lib/mysqlCopy the code
If you don’t use the host’s path, you can specify a volume_driver.
volume_driver: mydriverCopy the code