Compose profile
The Compose project is Docker’s official open source project, which is responsible for the rapid choreography of Docker container clusters. Its code is open source on https://github.com/docker/compose.
The Compose location is an application that defines and runs multiple Docker containers. Its predecessor is the Fig open source project.
Using a Dockerfile template, you can easily define a separate application container. However, in daily work, there are often situations where multiple containers need to work together to complete a task. For example, to implement a Web project, in addition to the Web service container itself, you often need to add the back-end database service container, and even load balancing container.
Compose fits that need. It allows users to define a set of associated application containers as a project from a single docker-comemage. yml template file.
Compose has two important concepts:
- Service: A container for an application that can actually contain several instances of the container running the same image.
- Project: A complete business unit consisting of a set of associated application containers.
Compose’s default management object is a project, which provides easy lifecycle management through subcommands for a set of containers in a project.
The Compose project is written in Python and its implementation calls the API provided by the Docker service to manage the container.
Installation and uninstallation
Compose supports Linux, macOS, and Windows10.
Compose can be installed via Python’s package-management tool PIP, downloaded directly from compiled binaries, or even run directly from a Docker container.
Docker for Mac, Docker for Windows docker-compose binary file, Docker installation can be directly used.
docker-compose --version
Copy the code
Linux requires a separate binary or PIP installation.
Linux installation docker – compose
Binary package
Installing on Linux is simple, as you can download the compiled binaries directly from the official GitHub Release. For example, download the binary package directly on a Linux 64-bit system.
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose # Grant executable permission
Copy the code
PIP install
If your computer’s architecture is ARM(for example, raspberry PI), a PIP installation is recommended.
sudo pip install -U docker-compose
Copy the code
use
Scenario: The most common projects are Web sites. Most Web sites rely on services provided by third parties (such as DB and cache). In this example, Dubbo-admin relies on ZooKeeper.
Compose builds the Dubo-admin service
The master branch of Dubo-admin is available on Github
git clone -b master https://github.com/apache/incubator-dubbo-ops.git
Copy the code
Modify the application configuration in the admin to change the zookeeper address to zookeeper: / / zookeeper: 2181
Compile and package using Maven
mvn clean package -Dmaven.test.skip=true
Copy the code
Write a Dockerfile file in dubobo-admin with the content as
# FROM, which means to use the JDK8 environment as the base image, if the image is not local will be downloaded FROM DockerHub
FROM openjdk:8-jdk-alpine
# the author
MAINTAINER Simon<[email protected]>
VOLUME /tmp
# ADD, copy and rename the fileThe ADD. / target/dubbo - admin - 0.0.1 - the SNAPSHOT. Jar app. The jarEgd system attribute pointing to /dev/urandom as ENTRYPOINT to shorten Tomcat startup time
ENTRYPOINT ["java"."-Djava.security.egd=file:/dev/./urandom"."-jar"."/app.jar"]
Copy the code
Use docker build-t dubo-admin :1.0. Command to build.
Write the docker-comemage. yml file in your project root directory, which is the main template file for Compose.
version: '3.4'
services:
zk_server:
image: Zookeeper: 3.4
ports:
- 2181: 2181
dubbo-admin:
image: Dubbo - admin: 1.0
links:
- zk_server:zookeeper
depends_on:
- zk_server
ports:
- 7001: 7001
Copy the code
Do this in the docker-comemess. yml directory:
docker-compose up
Copy the code
For verification, visit http://server IP address :7001 in the browser. The user name and password are root/root guest/guest
Compose command description
Command object and format
Docker-compose [COMMAND] –help or docker-compose help [COMMAND] to view the usage format of a particular COMMAND.
The basic usage format of the docker-compose command is:
docker-compose [-f=<arg>... ] [options] [COMMAND] [ARGS...]Copy the code
Command options
- -f,–file file Specifies the template file. The default is docker-comemage. yml and can be specified multiple times.
- -p,–project-name name Specifies the project name. By default, the name of the directory is used as the project name.
- X-networking uses Docker’s pluggable network backend feature
- –x-network-driver Driver Specifies the network back-end driver. The default driver is Bridge
- –verbose Displays more debugging information.
- -v,–version Prints the version and exits.
build
Docker-compose build [options] [SERVICE...] . Build (rebuild) the service container in the project. You can always run the Docker-compose build in the project directory to rebuild the service. Options include: * --force-rm removes temporary containers during the build process. * --no-cache Does not use the cache during the build process (which lengthens the build process). * --pull always tries to pull an image of the updated version.Copy the code
logs
Docker-compose logs [options] [SERVICE...] . View the output of the service container. By default,docker-compose will use different colors for different service outputs. You can turn off the color by --no-color.Copy the code
port
The format is docker-compose port [options] SERVICE PRIVATE_PORT. Prints the public ports mapped by a container port. Options: --protocol=proto Specifies the port protocol, TCP (default) or UDP. --index=index If multiple containers exist for the same service, specify the serial number of the command object container (default: 1).Copy the code
ps
Docker-compose ps [options] [SERVICE...] docker-compose ps [options] Lists all current containers in the project. Option: -q Prints only the container ID.Copy the code
pull
Docker-compose pull [options] [SERVICE...] . Pull the mirror on which the service depends. Options: --ignore-pull-failures Ignores errors during mirror pull.Copy the code
restart
Docker-compose restart [options] [SERVICE...] Restart services in the project. Options: -t,--timeout timeout Specifies the timeout to stop the container before restarting (default: 10 seconds).Copy the code
rm
Docker-compose rm [options] [SERVICE...] Delete all (stopped) service containers. It is recommended to stop the container by executing the docker-compose stop command. Options:-f,--force Forces the container to be deleted directly, including the container in the non-stopped state. In general, try not to use this option. -v Deletes data volumes attached to a container.Copy the code
run
Docker-compose run[options] [-p PORT...] docker-compose run[options] [-p PORT...] [-eKEY=VAL... ] SERVICE [COMMAND] [ARGS...] Executes a command on the specified service. Example: docker-compose run Ubuntu ping docker.comCopy the code
scale
Docker-compose scal [options] [SERVICE=NUM...] Sets the number of containers on which the specified service runs. For example: docker-compose scale web=3 db=2 will start 3 containers running web services and 2 containers running DB services.Copy the code
up
This is a powerful command that attempts to automate a series of operations including building an image, (re) creating a service, starting a service, and associating a service-related container. Linked services will be automatically started unless they are already running. Options:-dRun the service container in the background. --no-color does not use color to distinguish between console outputs of different services. --no-deps does not start containers linked to the service. -- force-set forces recreate containers and cannot be used with -- no-set. -- no-set If a container already exists, it is not recreated and cannot be used with -- force-set. --no-build does not automatically build the missing service image. -t,--timeout timeout Specifies the timeout period for stopping the container (default: 10 seconds).Copy the code
Other commands are as follows:
The command | instructions |
---|---|
version | The docker-compose version format is docker-compose version |
config | Verify that the Compose format is correct and display the configuration if correct, and the reason for the error if not. |
exec | Enter the specified container |
images | Lists the images contained in the Compose file |
down | Stop the container started by the up command and remove the network. |
help | Get the help of a command |
kill | Force a stop to the service container by sending SIGKILL |
pause | Docker-compose pause [SERVICE…] To pause a service container. |
push | Push service dependent images to the Docker image repository |
start | Docker-compose start[SERVICE…] To start an existing service container. |
stop | Stop an existing service container. |
top | View the processes running in each service container |
unpause | Docker-compose unpause [SERVICE…] To resume services in the suspended state. |
Compose template file
The template file is the core of Compose and involves many directive keywords, most of which have similar meanings to docker run parameters.
The default template file name is docker-comemage. yml in YAML format.
Note that each service must automatically build the generated image by specifying the image via the image directive or the build directive (requiring a Dockerfile) etc.
If you use the build directive, the options set in the Dockerfile (for example :CMD,EXPOSE,VOLUME,ENV, etc.) will be automatically retrieved without needing to be set again in docker-comedy.yml. The following describes the usage of common commands.
version: '3'
services:
webapp:
build:
context: ./dir
dockerfile: Dockerfile-alternate
args:
buildno: 1
Copy the code
build
Specifies the path to the folder where the Dockerfile resides (either absolute or relative to the docker-comedy.yml file). Compose will use it to automatically build the image and then use it. Use the context directive to specify the path to the folder where the Dockerfile resides. Use the Dockerfile directive to specify the file name of the Dockerfile. Use the arG directive to specify the variables used to build the imageCopy the code
command
Overrides the commands executed by default when the container is started.
command:echo "hello world"
Copy the code
Container_name
Instruction container name. The default format will be project name _ service name _ serial number.
container_name:docker-web-container
Copy the code
configs
Swarm mode is used for Swarm mode only.
deploy
Swarm mode is used for Swarm mode only.
devices
Specify the device mapping relationship.
devices:
- "/dev/ttyUSB1:/ttyUSB0"
Copy the code
depends_on
Resolve container dependencies and startup priorities.
dns
User-defined DNS server. It can be a value or it can be a list.
DNS: 8.8.8.8 DNS: -8.8.8.8-114.114.114.114Copy the code
environment
Set environment variables. You can use either an array or a dictionary. Variables given a name only automatically get the value of the corresponding variable on the Compose host, which can be used to prevent unnecessary data leakage.
environment:
RACK_ENV: development
SESSION_SECRET:
environment:
- RACK_ENV=development
- SESSION_SECRET
Copy the code
expose
Ports are exposed, but not mapped to the host and only accessed by connected services. Only the internal port can be specified
expose:
- "3000"
- "8000"
Copy the code
extra_hosts
Like the –add-host parameter in Docker, specifies additional host name mapping information. An entry is added to the /etc/hosts file in the service container after the service is started. 8.8.8.8 googledns
extra_hosts:
- "Googledns: 8.8.8.8"
Copy the code
healthcheck
Run commands to check whether the container is healthy.
healthcheck:
test: ["CMD"."curl"."-f"."http://localhost"]
interval:1m30s
timeout:10s
retries:3
Copy the code
image
Specifies the image name or image ID. Compose will try to pull the image if it doesn’t already exist locally
image:session-web:latest
Copy the code
lables
Add Docker metadata information to the container. For example, you can add auxiliary description information for containers.
labels:
com.study.department:"devops department"
com.study.release:"v1.0"
Copy the code
links
Connect to other containers. Note: This directive is not recommended. Docker network should be used to establish the network and Docker run –network should be used to connect the specific network. Or use version:’2′ and later docker-comemage.yml directly to define and use a custom network.
network_mode
Set the network mode. Use the same value as the –network argument to Docker run.
network_mode:"bridge"
network_mode:"host"
network_mode:"none"
Copy the code
networks
Configure the network connected to the container.
version:"3"
services:
some-service:
networks:
- some-network
networks:
some-network:
Copy the code
ports
Expose port information. Use the HOST port :CONTAINER format, or just specify the port of the CONTAINER (the HOST will randomly select the port).
ports:
- "3000"
- "8000:8000"
Copy the code
volumes
You can set the path for mounting a data volume. You can also set the host path.
volumes:
- /var/lib/mysql
- cache/:/tmp/cache
- ~/configs:/etc/configs/:ro
Copy the code
ulimits
Specifies the ulimits value for the container. For example, specify the maximum number of processes to be 65535, the number of file handles to be 20000(soft limit, the application can change at any time, cannot exceed the hard limit) and 40000(system hard limit, can only be increased by root user).
ulimits:
nproc: 65535
nofile:
soft: 20000
hard: 40000
Copy the code
In addition, Including domainname, entrypoint, hostname, ipc, mac_address, ring, read_only, eaccelerator.shm_size, restart, stdin_open, tty, user, working_dir And other instructions, basically with the corresponding parameter in docker run function is the same.
Specifies the entry file to execute after the service container is started
entrypoint: /code/entrypoint.sh
Copy the code
Specifies the user name to run the application in the container
user:nginx
Copy the code
Specifies the working directory in the container
working_dir: /code
Copy the code
Specifies the domain name, host name, and MAC address to be searched for in a container
domainname:your_website.com
hostname:test
mac_address:08-00-27-00-0C-0A
Copy the code
Allows privileged commands to run in containers
privileged:true
Copy the code
Specifies that the container exit restart policy is always restart. Always or unless-stopped is recommended in a production environment
restart:alwarys
Copy the code
Mounting the root file system of the container in read-only mode means that the contents of the container cannot be modified
read_only:true
Copy the code
Turn on standard input to accept external input
stdin_open:true
Copy the code
Simulate a dummy terminal
tty:true
Copy the code
The Compose template file supports dynamic reading of host system environment variables and variables in the. Env file in the current directory. For example, the Compose file below will read the value of the variable ${MONGO_VERSION} from the environment in which it is running and write it to the executing instruction.
version: "3"
services:
db:
image: "mongo:${MONGO_VERSION}"
Copy the code
MONGO_VERSION=3.2 docker-compose up will launch a mongo:3.2 mirror container. If a. Env file exists in the current directory, the docker-compose command will read variables from the file.