Article Directory
-
- Docker quick start-Docker-Compose
-
- 1. Introduction to Docker-Compose
- Two, Docker-Compose commonly used commands
-
-
-
- 1. Docker-Compose command format
- 2、docker-compose up
- 3 、 docker-compose ps
- 4、docker-compose stop
- 5、docker-compose -h
- 6、docker-compose down
- 7、docker-compose logs
- 8、docker-compose build
- 9、docker-compose pull
- 10、docker-compose restart
- 11、docker-compose rm
- 12、docker-compose start
- 13、docker-compose run
- 14、docker-compose scale
- 15、docker-compose pause
- 16、docker-compose kill
- 17、dokcer-compose config
- 18、docker-compose create
- 19、docker-compose exec
- 20、docker-compose port
- 21、docker-compose push
- 22、docker-compose stop
- 23、docker-compose unpause
- 24、docker-compose version
-
-
- Three, Docker-Compose template file
-
-
- 1. Introduction to Docker-Compose template file
- 2、image
- 3、build
- 4、context
- 6、command
- 7、container_name
- 8、depends_on
- 9、pid
- 10、ports
- 11、extra_hosts
- 12、volumes
- 13、volumes_from
- 14、dns
- 16、entrypoint
- 17、env_file
- 18、cap_add
- 19、cap_drop
- 20、cgroup_parent
- 21、devices
- 22、expose
- 23、extends
- 24、external_links
- 28、log_opt
- 29、net
- 30、security_opt
- 31, environment variables
-
- Four, Docker-Compose template file example
Docker quick start-Docker-Compose
1. Introduction to Docker-Compose
1. Introduction to Docker-Compose
The Docker-Compose project is an official open source project of Docker, responsible for the rapid orchestration of Docker container clusters.
Docker-Compose divides the managed containers into three layers, namely project, service and container. All the files (docker-compose.yml, extends files or environment variable files, etc.) in the Docker-Compose running directory form a project. If there is no special designation, the project name is the current directory name. A project can contain multiple services, and each service defines the image, parameters, and dependencies for the container to run. A service can include multiple container instances. Docker-Compose does not solve the problem of load balancing, so other tools need to be used to realize service discovery and load balancing.
The project configuration file of Docker-Compose is docker-compose.yml by default. The configuration file can be customized through the environment variable COMPOSE_FILE or -f parameter, which defines multiple dependent services and the container in which each service runs.
Using a Dockerfile template file allows users to easily define a separate application container. In work, we often encounter situations that require multiple containers to cooperate with each other to complete a 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, or even a load balancing container.
Compose allows users to define a group of associated application containers as a project through a single docker-compose.yml template file (YAML format).
The Docker-Compose project is written in Python and 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.
2. Docker-Compose installation
Installation method 1:
Download Docker-Compose:
sudo curl -L https://github.com/docker/compose/releases/download/1.23.0-rc3/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
Install Docker-Compose:
sudo chmod +x /usr/local/bin/docker-compose
View version:
docker-compose version
Installation method two:
install pip
yum -y install epel-release
yum -y install python-pip
Confirm version
pip --version
Update pip
pip install --upgrade pip
Install docker-compose
pip install docker-compose
View version
docker-compose version
Install completion tool:
In order to facilitate the input of commands, you can also install Docker's completion prompt tool to help you quickly enter commands:
install
yum install bash-completion
Download the docker-compose script
curl -L https://raw.githubusercontent.com/docker/compose/$(docker-compose version --short)/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose
3. Docker-Compose uninstallation
If it is installed as a binary package, just delete the binary file:
sudo rm /usr/local/bin/docker-compose
If installed through the Python pip tool, execute the following command to delete:
sudo pip uninstall docker-compose
Two, Docker-Compose commonly used commands
1. Docker-Compose command format
docker-compose [-f …] [options] [COMMAND] [ARGS…]
The command options are as follows:
-f, -file FILE specifies the Compose template file, the default is docker-compose.yml, which can be specified multiple times.
-p, -project-name NAME specifies the name of the project. By default, the name of the directory will be used as the project name.
-x-network-driver uses Docker's pluggable network backend feature (requires Docker 1.9+ version)
-x-network-driver DRIVER specifies the driver of the network backend, the default is bridge (requires Docker 1.9+ version)
-verbose output more Multiple debugging information
-v, -version print version and exit
2、docker-compose up
docker-compose up [options] [–scale SERVICE=NUM…] [SERVICE…]
Options include:
-d Run the service container in the background
–no-color Do not use color to distinguish the control output of different services
–no-deps Do not start the container linked to the service
–force-recreate Force the container to be recreated, cannot be combined with –no- Use recreate at the same time
–no-recreate If the container already exists, it will not be recreated. It cannot be used at the same time with –force-recreate
–no-build Does not automatically build the missing
service image
–build Build the service image before starting the container –abort-on- container-exit stops all containers. If any container is stopped, it cannot be used with -d at the same time.
-t, --timeout TIMEOUT Timeout when stopping the container (default is 10 seconds)
--remove-orphans delete service is not defined in compose file Container
–scale SERVICE=NUM Set the number of service running containers, which will override the parameters specified by scale in compose
docker-compose up
Start all services
docker-compose up -d
In the background, all startup services
-f specify the use of the Compose template file, the default is docker-compose.yml, you can specify multiple times.
docker-compose -f docker-compose.yml up -d
3 、 docker-compose ps
docker-compose ps [options] [SERVICE…]
docker-compose ps
List all current containers in the project
4、docker-compose stop
docker-compose stop [options] [SERVICE…]
Options include:
-t, --timeout TIMEOUT Timeout when stopping the container (default is 10 seconds)
docker-compose stop
Stop the running container, you can start it again through docker-compose start
5、docker-compose -h
docker-compose -h
View help
6、docker-compose down
docker-compose down [options]
Stop and delete containers, networks, volumes, and mirrors.
Options include:
-rmi type, delete the mirror, the type must be: all, delete all mirrors defined in the compose file; local, delete the mirror with an empty mirror name
-v, -volumes, delete the anonymous and anonymous ones that have been defined in the compose file The data volume attached to the container
–remove-orphans, delete the container in the service that is not defined in compose
docker-compose down
Disable the removal of all containers and network related
7、docker-compose logs
docker-compose logs [options] [SERVICE…]
View the output of the service container. By default, docker-compose will use different colors to distinguish different service outputs. The color can be turned off by -no-color.
docker-compose logs
View the output of the service container
8、docker-compose build
docker-compose build [options] [–build-arg key=val…] [SERVICE…]
Build (rebuild) the service container in the project.
Options include:
–compress build the upper and lower environment through gzip compression
–force-rm delete the temporary container during the build process
–no-cache do not use the cache during the image building process
–pull always try to get the updated version of the image through the pull operation
-m, –Memory MEM sets the memory size for the built container
–build-arg key=val sets the build-time variable for the
service Once the service container is built, it will carry a tag name. You can run docker-compose build in the project directory at any time to rebuild the service
9、docker-compose pull
docker-compose pull [options] [SERVICE…]
Pull the image that the service depends on.
Options include:
-ignore-pull-failures, ignore errors in the process of pulling mirrors
-parallel, pull multiple mirrors at the same time
-quiet, do not print progress information during the mirroring process
docker-compose pull
Pull the image that the service depends on
10、docker-compose restart
docker-compose restart [options] [SERVICE…]
Restart the services in the project.
Options include:
-t, -timeout TIMEOUT, specify the timeout for stopping the container before restarting (10 seconds by default)
docker-compose restart
Restart the services in the project
11、docker-compose rm
docker-compose rm [options] [SERVICE…]
Delete all (stopped) service containers.
Options include:
-f, -force, force direct deletion, including non-stop container
-v, delete the data volume mounted by the container
docker-compose rm
Delete all (stopped) service containers. It is recommended to execute the docker-compose stop command first to stop the container.
12、docker-compose start
docker-compose start [SERVICE…]
docker-compose start
Start the existing service container.
13、docker-compose run
docker-compose run [options] [-v VOLUME…] [-p PORT…] [-e KEY=VAL…] SERVICE [COMMAND] [ARGS…]
Execute a command on the specified service.
docker-compose run ubuntu ping www.baidu.com
Execute a ping command on the specified container.
14、docker-compose scale
docker-compose scale web=3 db=2
Set the number of containers running the specified service. Set the number through the parameter of service=num
15、docker-compose pause
docker-compose pause [SERVICE…]
Pause a service container
16、docker-compose kill
docker-compose kill [options] [SERVICE…]
By sending the SIGKILL signal to forcibly stop the service container.
Support the -s parameter to specify the signal to be sent, for example, send the SIGINT signal through the following command:
docker-compose kill -s SIGINT
17、dokcer-compose config
docker-compose config [options]
Verify and view the compose file configuration.
Options include:
-resolve-image-digests mark the image tag as digest
-q, -quiet only verify the configuration, not output. When the configuration is correct, nothing will be output. When the file configuration is wrong, an error message will be output
-services print the service name, one
per line -volumes print the data volume name, one per line
18、docker-compose create
docker-compose create [options] [SERVICE…]
Create a container for the service.
Options include:
–force-recreate: re-create the container, even if the configuration and image are not changed, incompatible –no-recreate parameter
–no-recreate: if the container already exists, do not need to be recreated, incompatible –force-recreate parameter
–no -build: do not create a mirror, even if it is missing
-build: generate a mirror before creating the container
19、docker-compose exec
docker-compose exec [options] SERVICE COMMAND [ARGS…]
Options include:
-d Separate mode, run commands in the background.
-Privileged Get privileges.
-User USER specifies the user to run.
-T Disable the allocation of TTY, default docker-compose exec allocates TTY.
–Index=index, when a service has multiple containers, you can log in to any service under the service through this parameter, for example: docker-compose exec –index=1 web /bin/bash, the web service contains multiple containers
20、docker-compose port
docker-compose port [options] SERVICE PRIVATE_PORT
Display the public port mapped by a container port.
Options include:
-protocol=proto, specify the port protocol, TCP (default) or UDP
-index=index, if you agree to have multiple containers for the service, specify the serial number of the command object container (default is 1)
21、docker-compose push
docker-compose push [options] [SERVICE…]
The image on which the push service depends.
Options include:
--ignore-push-failures Ignore errors in the process of pushing the image
22、docker-compose stop
docker-compose stop [options] [SERVICE…]
Show the running process of each container.
23、docker-compose unpause
docker-compose unpause [SERVICE…]
Resume services that are in a suspended state.
24、docker-compose version
docker-compose version
Print version information.
Three, Docker-Compose template file
1. Introduction to Docker-Compose template file
Compose allows users to define a group of associated application containers as a project through a docker-compose.yml template file (YAML format).
The Compose template file is a YAML file that defines services, networks, and volumes. The default path of the Compose template file is docker-compose.yml in the current directory. You can use .yml or .yaml as the file extension.
The Docker-Compose standard template file should contain three parts: version, services, and networks. The most important part is services and networks.
version: '2'
services:
web:
image: dockercloud/hello-world
ports:
- 8080
networks:
- front-tier
- back-tier
redis:
image: redis
links:
- web
networks:
- back-tier
lb:
image: dockercloud/haproxy
ports:
- 80:80
links:
- web
networks:
- front-tier
- back-tier
volumes:
- /var/run/docker.sock:/var/run/docker.sock
networks:
front-tier:
driver: bridge
back-tier:
driver: bridge
Compose currently has three versions: Version 1, Version 2, Version 3. Compose distinguishes between Version 1 and Version 2 (Compose 1.6.0+, Docker Engine 1.10.0+). Version 2 supports more instructions. Version 1 will be deprecated in the future.
2、image
image is the image name or image ID of the specified service. If the image does not exist locally, Compose will try to pull the image.
services:
web:
image: hello-world
3、build
In addition to the specified image, the service can also be based on a Dockerfile. The build task is executed when starting with up. The build label is build, and the path to the folder where the Dockerfile is located can be specified. Compose will use the Dockerfile to automatically build the image, and then use the image to start the service container.
build: /path/to/build/dir
can also be a relative path, as long as the context is determined, the Dockerfile can be read.
build: ./dir
Set the context root directory, and then specify the Dockerfile based on this directory.
build:
context: ../
dockerfile: path/of/Dockerfile
The build is a directory, if you want to specify the Dockerfile file, you need to use the dockerfile tag to specify in the child tag of the build tag.
If you specify both the image and build tags, Compose will build the image and name the image the name specified by the image value.
4、context
The context option can be the file path of the Dockerfile or the URL linked to the git repository. When the provided value is a relative path, it will be resolved to the path relative to the composing file. This directory is also the context sent to the Docker daemon.
build:
context: ./dir
5. Dockerfile is built
using dockerfile files, and the build path must be specified
build:
context: .
dockerfile: Dockerfile-alternate
The dockerfile instruction cannot be used with the image at the same time, otherwise Compose will not be sure which instruction to use to generate the final service image.
6、command
Use command to override the command executed by default after the container is started.
command: bundle exec thin -p 3000
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 to fully control the naming of the container, you can use the label to specify:
container_name: app
8、depends_on
When using Compose, the biggest advantage is that there are fewer startup commands, but generally the order in which the project container starts is required. If you start the container directly from top to bottom, it will inevitably fail to start due to container dependency issues. For example, if the application container is started when the database container is not started, the application container will exit because it cannot find the database. The depends_on tag is used to solve the problem of container dependency and startup sequence.
version: '2'
services:
web:
build: .
depends_on:
- db
- redis
redis:
image: redis
db:
image: postgres
The container defined by the above YAML file will first start the redis and db services, and finally start the web service.
9、pid
pid: "host"
sets the PID mode to host PID mode, sharing the process namespace with the host system. Containers using pid tags will be able to access and manipulate the namespaces of other containers and hosts.
10、ports
ports are used to map the port label.
Use the HOST:CONTAINER format or just specify the port of the container, and the host will randomly map the port.
ports:
- "3000"
- "8000:8000"
- "49100:22"
- "127.0.0.1:8001:8001"
When using the HOST:CONTAINER format to map ports, if the container port used is less than 60, you may get an incorrect result, because YAML will parse the number format of xx:yy as hexadecimal. So it is recommended to use string format.
11、extra_hosts
Adding the label of the host name will add some records in the /etc/hosts file.
extra_hosts:
- "somehost:162.242.195.82"
- "otherhost:50.31.209.229"
Check the internal hosts of the container after startup:
162.242.195.82 somehost
50.31.209.229 otherhost
12、volumes
To mount a directory or an existing data volume container, you can directly use the [HOST:CONTAINER] format, or use the [HOST:CONTAINER:ro] format, the latter for the container, the data volume is read-only and can be valid Protect the host's file system.
Compose's data volume specified path can be a relative path, use. Or... to specify a relative directory.
The format of the data volume can be in the following forms:
volumes:
// Just specify a path, Docker will automatically create a data volume (this path is inside the container).
- /var/lib/mysql
// Use absolute path to mount data volume - /opt/data:/var/lib/mysql
// The relative path centered on the Compose configuration file is mounted to the container as the data volume. - ./cache:/tmp/cache
// Use the user's relative path (~/ indicates the directory is /home/<user directory>/ or /root/). - ~/configs:/etc/configs/:ro
// An existing named data volume. - datavolume:/var/lib/mysql
If you do not use the path of the host, you can specify a volume_driver.
volume_driver: mydriver
13、volumes_from
Mount its data volume from another service or container:
volumes_from:
- service_name
- container_name
14、dns
Custom DNS server. It can be a value or a list.
dns:8.8.8.8
dns:
- 8.8.8.8
- 9.9.9.9
15. dns_search
configures the DNS search domain. It can be a value or a list.
dns_search:example.com
dns_search:
- domain1.example.com
- domain2.example.com
16、entrypoint
There is an instruction in the Dockerfile called the ENTRYPOINT instruction, which is used to specify the access point.
Access points can be defined in docker-compose.yml, overriding the definition in Dockerfile:
entrypoint: /code/entrypoint.sh
17、env_file
A file dedicated to storing variables can be defined in docker-compose.yml.
If the configuration file is specified by docker-compose -f FILE, the path in env_file will use the configuration file path.
If a variable name conflicts with the environment directive, the latter shall prevail. The format is as follows:
env_file: .env
or set multiple according to docker-compose.yml:
env_file:
- ./common.env
- ./apps/web.env
- /opt/secrets.env
If there is a build operation in the configuration file, the variable will not enter the build process.
18、cap_add
Increase the kernel capacity of the specified container (capacity).
Let the container have all the capabilities that can be specified:
cap_add:
- ALL
19、cap_drop
Remove the kernel capacity of the specified container.
To remove the NET_ADMIN ability, you can specify:
cap_drop:
- NET_ADMIN
20、cgroup_parent
Created a cgroup group named cgroups_1:
cgroup_parent: cgroups_1
21、devices
Specify the device mapping relationship, for example:
devices:
- "/dev/ttyUSB1:/dev/ttyUSB0"
22、expose
The port is exposed, but not mapped to the host, and only accessible by connected services. Only the internal port can be specified as a parameter, as shown below:
expose:
- "3000"
- "8000"
23、extends
Expand based on other template files. For example, for the webapp service, a basic template file is defined as common.yml:
#common.yml
webapp:
build: ./webapp
environment:
- DEBUG=false
- SEND_EMAILS=false
Write a new development.yml file and use the webapp service in common.yml to expand:
#development.yml
web:
extends:
file: common.yml
service: webapp
ports:
- "8000:8000"
links:
- db
environment:
- DEBUG=true
db:
image: mysql
The latter will automatically inherit the webapp service and environment variable definitions in common.yml.
The limits of extends are as follows:
A. To avoid cyclic dependency.
B, extends will not inherit the container and data volume resources defined in links and volumes_from. It is
recommended to define only some images and environment variables that can be shared in the basic template, and specify them in the extended template. Application variables, links, data volumes and other information
24、external_links
The container linked to the outside of docker-compose.yml can be an external container that is not managed by Compose.
external_links:
- redis_1
- project_db_1:mysql
- project_db_1:postgresql
25. Labels
add Docker metadata (metadata) information to the container. For example, you can add auxiliary information to the container:
labels:
com.startupteam.description: "webapp for a strtup team"
26.
Links link to containers in other services. Use the service name (as an alias at the same time), or "service name: service alias" (such as SERVICE:ALIAS), for example:
links:
- db
- db:database
- redis
The alias will be automatically created in /etc/hosts in the service container. E.g:
172.17.2.186 db
172.17.2.186 database
172.17.2.187 redis
27、log_driver
Specify the log driver type. Currently supports three log driver types:
log_driver: "json-file"
log_driver: "syslog"
log_driver: "none"
28、log_opt
Log-driven related parameters. E.g:
log_driver: "syslog"log_opt:
syslog-address: "tcp://192.168.0.42:123"
29、net
Set the network mode.
net: "bridge"
net: "none"
net: "host"
30、security_opt
Specify the default attributes (user, role, type, level, etc.) of the container template label mechanism. For example, to configure the user name and role name of the label:
security_opt:
- label:user:USER
- label:role:ROLE
31, environment variables
Environment variables can be used to configure the behavior of Docker-Compose.
COMPOSE_PROJECT_NAME
sets the project name added before each container started by Compose. The default is the name of the current working directory.
COMPOSE_FILE
sets the path of the docker-compose.yml template file. The default path is the current working directory.
DOCKER_HOST
sets the address of the Docker daemon. By default, unix:///var/run/docker.sock is used.
If the DOCKER_TLS_VERIFY setting is not empty, the interaction with the Docker daemon is performed through TLS.
DOCKER_CERT_PATH
configures the path of the verification (ca.pem, cert.pem and key.pem) files required for TLS communication. The default is ~/.docker.
Four, Docker-Compose template file example
1. Docker-compose template file writing
The docker-compose.yaml file is as follows:
version: '2'
services:
web1:
image: nginx
ports:
- "6061:80"
container_name: "web1"
networks:
- dev
web2:
image: nginx
ports:
- "6062:80"
container_name: "web2"
networks:
- dev
- pro
web3:
image: nginx
ports:
- "6063:80"
container_name: "web3"
networks:
- pro
networks:
dev:
driver: bridge
pro:
driver: bridge
#volumes:
The docker-compose.yaml file specifies three web services.
2. Start the application
Create a webapp directory, copy the docker-compose.yaml file to the webapp directory, and use docker-compose to start the application.
docker-compose up -d
3. Service access
Access web1, web2, web3 services through a browser:
http://127.0.0.1:6061
http://127.0.0.1:6062
http://127.0.0.1:6063