Docker from getting started to practice series 4 - docker container choreographer Docker Compose

Posted by reckdan on Mon, 27 Dec 2021 14:52:34 +0100

Introduction to Compose

concept

Docker Compose project is the official open source project of docker, which comes from the previous Fig project. It is responsible for the rapid arrangement of docker container clusters. The project address is: https://github.com/docker/compose/releases

It is a docker application tool that defines and runs multiple containers. Using compose, you can configure your own services through the YMAL file, and then you can use the configuration file to create and run all services through a command.

The Compose project is written in Python and calls the API provided by the Docker service to manage the container. Therefore, as long as the operating platform supports Docker API, composition can be used for orchestration management.

form

Docker compose divides the managed containers into three layers: project, service and container.

The default management object of Compose is the project, which can easily manage the life cycle of a group of containers in the project through subcommands.

All files (docker-compose.yml, extensions files or environment variable files, etc.) in docker compose running directory form a project. Unless otherwise specified, the project name is the current directory name. A project can contain multiple services, and each service defines the image, parameters and dependencies of container operation. A service can include multiple container instances.

We know that using a Dockerfile template file allows users to easily define a separate application container. In work, we often encounter the situation that multiple containers need to cooperate with each other to complete a task. For example, to deploy a Web project, in addition to the Web service container, you often need to add the back-end database service container, and even the load balancing container.

Compose just meets this requirement. It allows users to use a separate docker - compose YML template file (YAML format) to define a set of associated application containers as a project.

  • Service: an application container can actually include several container instances running the same image. Each service has its own name, image used, mounted data volume, network to which it belongs, what other services it depends on, etc., that is, taking the container as the granularity, users need the tasks completed by Compose.

  • Project: a complete business unit composed of a group of associated application containers, which is located in docker-compose.com Defined in the YML file. That is, a configuration file of compose can be parsed into a project. Compose obtains all container management and deployment operations required by the configuration file by analyzing the specified configuration file.

The project configuration file of docker compose defaults to docker compose YML, you can use the environment variable compose_ The file or - f parameter defines a custom configuration file that defines multiple dependent services and the container in which each service runs.

Installation and uninstallation

Compose now fully supports Linux, Mac OS and Windows. We need to install Docker before installing compose. Next, we install the compiled binary package in the Linux system.

Compose supports three platforms: Linux, macOS and Windows 10.

Compose can be installed through Python's package management tool pip, downloaded and compiled binaries directly, and even run directly in the Docker container.

Docker Desktop for Mac/Windows comes with docker compose binary files, which can be used directly after docker is installed.

Binary package installation

The installation on Linux is also very simple, from Official GitHub Release Download the compiled binary file directly.

For example, download the corresponding binary package directly on a Linux 64 bit system.

sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" > /usr/local/bin/docker-compose
# Domestic users can use the following methods to speed up the download
sudo curl -L "https://download.fastgit.org/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" > /usr/local/bin/docker-compose
# Apply executable permissions to the binary
sudo chmod +x /usr/local/bin/docker-compose

test

docker-compose --version

uninstall

If it is installed in binary package mode, delete the binary file.

sudo rm /usr/local/bin/docker-compose

docker-compose.yml file details

concept

Official documents: https://docs.docker.com/compose/compose-file/

Docker Compose allows users to use Docker Compose YML file (YAML format) to define a group of associated containers as a project. A project contains multiple services. Each service defines the images, parameters, dependencies, etc. required to create a container.

Unless otherwise specified, the project name is docker compose The name of the directory where the YML file is located.

Docker Compose template file the top-level configurations we need to pay attention to include version, services, networks and volumes. In addition to version, there are many lower level configurations under other top-level configurations. We will also introduce them in detail later. Let's see what these top-level configurations mean first:

  • Version: describes the version information of the Compose file. The latest version is 3.8 and the corresponding Docker version is 19.03 0+;
  • Services: define multiple services. Each service defines the images, parameters, dependencies, etc. required to create a container;
  • networkds: define multiple networks. Containers in the same network can communicate directly through the container name according to the DNS server;
  • volumes: a data volume used to mount directories.

case

In the configuration file, all containers are defined through services, and then the docker Compose script is used to start, stop and restart containers. It is very suitable for the scenario of combined use of multiple containers for development. Let's start with a simple composition case study.

Write docker compose YML file.

# Create directory
mkdir -p /usr/local/docker-nginx
# Switch to the specified directory
cd /usr/local/docker-nginx/
# Write docker compose YML file
vi docker-compose.yml

Add the following to the file:

# Describes the version information of the Compose file
version: "3.8"
# You can define multiple services
services:
  nginx: # Service name
    image: nginx # Mirror required to create container
    container_name: mynginx # Container name, default to "project name service entry name serial number"
    ports: # Port mapping relationship between host and container
      - "80:80" # Left host port: right container port
    networks: # Configure the network to which the container is connected, and reference the entry under the top-level networks
      - nginx-net

# You can define multiple networks. If it is not declared, a bridge network with the network name "project name _default" will be created by default
networks:
  nginx-net: # Entry name of a specific network
    name: nginx-net # Network name, default to "project name network entry name"
    driver: bridge # Network mode, the default is bridge

Use docker compose up to create and start all services. In this simple case, there is only one Nginx. We will follow up with some complex exercises:

# Foreground start
docker-compose up
# Background start
docker-compose up -d

The ip address of the browser accessing its own server: http://192.168.138.8/ The results are as follows:

Use docker compose down to stop and delete containers and networks.

version

Describes the version information of the Compose file. The latest version is 3.8 and the corresponding Docker version is 19.03 0+. For details on each version, refer to: https://docs.docker.com/compose/compose-file/compose-versioning/

The following is the Docker version corresponding to the version information of the Compose file.

Compose file formatDocker Engine release
3.819.03.0+
3.718.06.0+
3.618.02.0+
3.517.12.0+
3.417.09.0+
3.317.06.0+
3.217.04.0+
3.11.13.1+
3.01.13.0+
2.417.12.0+
2.317.06.0+
2.21.13.0+
2.11.12.0+
2.01.10.0+
1.01.9.1.+

services

Just now we mentioned docker - compose The YML file contains many lower level configuration items. Let's take a look at some common configuration items in detail, starting with the top-level configuration services.

Services is used to define multiple services. Each service defines the images, parameters, dependencies, etc. required to create a container, just like passing command-line parameters to docker run. Similarly, the definition of network and data volume is the same.

For example, we used the docker run command to build a MySQL application container. The command is as follows:

docker run -d --name mysql8 -p 3306:3306 -v /opt/data/docker_mysql/conf:/etc/mysql/conf.d -v /opt/data/docker_mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root mysql:8

Use docker compose YML can be defined as follows:

# Describes the version information of the Compose file
version: "3.8"
# You can define multiple services
services:
  mysql: # Service name
    image: mysql:8 # Mirror required to create container
    container_name: mysql8 # Container name, default to "project name service entry name serial number"
    ports: # Port mapping relationship between host and container
      - "3306:3306" # Left host port: right container port
    environment: # Environment variables required when creating containers
      MYSQL_ROOT_PASSWORD: root
    volumes:
      - "/opt/data/docker_mysql/conf:/etc/mysql/conf.d"
      - "/opt/data/docker_mysql/data:/var/lib/mysql"

Then, you can complete a series of operations such as creating, stopping or deleting containers through dokcer compose related commands.

image

Specify the mirror name label or mirror ID required to create the container. If the image does not exist locally, it will be pulled remotely.

services:
  web:
    image: hello-world

build

In addition to building the container based on the specified image, it can also be built based on the Dockerfile file. The build task will be performed when using the up command.

The path of the folder where the Dockerfile is located can be specified through the build configuration item. Compose will use Dockerfile to automatically build the image, and then use the image to start the service container.

The build configuration item can use either an absolute path or a relative path.

# Absolute path, under which the image is built based on the file named Dockerfile
/usr/local/docker-centos
# Relative path, relative to the current docker compose The directory where the YML file is located, and the image is built based on the file named Dockerfile
.

Next, let's take a slightly more complicated exercise. Through the basic image centos:7, after installing jdk and tomcat in the image, make it a new image mycentos:7.

Create a directory and write a Dockerfile file.

# Create directory
mkdir -p /usr/local/docker-centos
# Switch to the specified directory
cd /usr/local/docker-centos/
# Write Dockerfile file
vi Dockerfile

Dockerfile file contents are as follows:

# Indicates that the new image is from the centos:7 base image
FROM centos:7
# The author information is declared through the image label
LABEL maintainer="jourwon.com"
# Set working directory
WORKDIR /usr/local
# Create the specified directory after the new image is successfully built
RUN mkdir -p /usr/local/java && mkdir -p /usr/local/tomcat
# Copy the file to the image and unzip it
ADD jdk-11.0.6_linux-x64_bin.tar.gz /usr/local/java
ADD apache-tomcat-9.0.37.tar.gz /usr/local/tomcat
# Expose the 8080 listening port when the container is running to the outside
EXPOSE 8080
# Set Java in container_ Home environment variable
ENV JAVA_HOME /usr/local/java/jdk-11.0.6/
ENV PATH $PATH:$JAVA_HOME/bin
# Start tomcat and view tomcat log information when starting the container
CMD ["/usr/local/tomcat/apache-tomcat-9.0.37/bin/catalina.sh", "run"]

Upload the required resource package jdk and tomcat to the same directory of Dockerfile.

Create a directory and write docker - compose YML file.

# Describes the version information of the Compose file
version: "3.8"
# You can define multiple services
services:
  mycentos: # Service name
    build: . # Relative to the current docker compose The directory where the YML file is located, and the image is built based on the file named dockerfile alternate
    container_name: mycentos7 # Container name, default to "project name service entry name serial number"
    ports: # Port mapping relationship between host and container
      - "8080:8080" # Left host port: right container port

Then, you can complete a series of operations such as creating, stopping or deleting containers through dokcer compose related commands.

context

This option can be the absolute / relative path of Dockerfile file or the URL of remote Git warehouse. When the provided value is a relative path, it is relative to the current docker compose The directory where the YML file is located.

build:
  context: . # Relative to the current docker compose The directory where the YML file is located, and the image is built based on the file named Dockerfile

dockerfile

Generally, by default, the image is built based on the file named dockerfile. Of course, it can also be a custom file name and declared with dockerfile. However, this option can only declare the file name, and the path of the file should still be declared through centext.

build:
  context: . # Relative to the current docker compose Directory of YML file
  dockerfile: Dockerfile-alternate # Build an image based on a file named dockerfile alternate

container_name

The default generated name format of containers created by Compose is: project name_ Service entry name_ Serial number. If you want to use a custom name, use container_name declaration.

services:
  mycentos:
    build: .
    container_name: mycentos7 # Container name, default to "project name service entry name serial number"

Because the Docker container name must be unique, if a custom name is specified, the service cannot be extended to multiple containers. Doing so may result in errors.

About serial number

What is the serial number for? Just look at the following example, docker compose The contents of the YML file are as follows:

# Describes the version information of the Compose file
version: "3.8"
# You can define multiple services
services:
  helloworld: # Service name
    image: hello-world

Then specify the helloworld service to start three at a time through -- scale.

docker-compose up -d --scale helloworld=3

As can be seen from the following figure, three containers are created, and the last sequence number of container name is accumulated from 1, which is the function of sequence number. Therefore, if a custom name is specified, the service cannot be extended to multiple containers.

depends_on

The biggest advantage of using Compose is to do more with the least number of commands. However, there are requirements for the starting sequence of general project containers. If you start the container directly from top to bottom, it will inevitably fail to start because of container dependency. For example, if the Web application container is started without starting the database container, the application container will exit because the database cannot be found. depends_on is a configuration item used to solve the problems of container dependency and startup sequence.

version: "3.8"
services:
  web:
    build: .
    depends_on:
      - db
      - redis
  redis:
    image: redis
  db:
    image: mysql

The container defined in the above YAML file will start db and redis services first, and then start the web service.

ports

The port exposed by the container. Format: host port on the left: container port on the right.

ports:
  - "80:80"
  - "8080:8080"

expose

The port exposed by the container is not mapped to the host, and only services that can be connected are allowed to access.

expose:
  - "80"
  - "8080"

restart

The container restart strategy is simply understood as whether the container should be started together after Docker restart:

  • no: the default restart policy. The container will not be restarted under any circumstances;
  • On failure: when the container exits abnormally, for example, if the exit status is not 0 (abnormal exit), the container will be restarted;
  • Always: the container is always restarted. Even if the container is manually stopped, the container will start together when Docker is restarted;
  • Unless stopped: the container is always restarted. Unless the container is stopped (manually or otherwise), the container will not be started when Docker is restarted.
services:
  nginx:
    image: nginx
    container_name: mynginx
    ports:
      - "80:80"
    restart: always

environment

Add environment variables. You can use arrays or dictionaries. Boolean related values (true, false, yes, no) need to be enclosed in quotation marks to ensure that the YML parser does not convert them to true or false.

environment:
  RACK_ENV: development
  SHOW: 'true'
  SESSION_SECRET:

Or the following format:

environment:
  - RACK_ENV=development
  - SHOW=true
  - SESSION_SECRET

env_file

Get environment variables from files. You can specify one or more files whose priority is lower than the environment variables specified by environment.

env_file:
  - /opt/runtime_opts.env # Absolute path
  - ./common.env # Relative path, relative to the current docker compose Directory of YML file
  - ./apps/web.env # Relative path, relative to the current docker compose Directory of YML file

Note: each line in the env file needs to be in the format of key = value. Lines beginning with # are treated as comments and ignored. Blank lines are also ignored.

command

Overrides the default command when the container is started.

command: echo "helloworld"

The command can also be a list.

command: ["echo", "helloworld"]

volumes

Data volume is used to realize directory mounting, and supports specified directory mounting, anonymous mounting and named mounting.

  • The format of the specified directory mount is: Host Directory on the left: container directory on the right, or host directory on the left: container directory on the right: read / write permission;
  • The anonymous mounting format is: container directory, or container Directory: read-write permission;
  • The named mount format is: data volume entry name: container directory, or data volume entry name: container Directory: read-write permission.
# Describes the version information of the Compose file
version: "3.8"
# You can define multiple services
services:
  mysql: # Service name
    image: mysql:8 # Mirror required to create container
    container_name: mysql8 # Container name, default to "project name service entry name serial number"
    ports: # Port mapping relationship between host and container
      - "3306:3306" # Left host port: right container port
    environment: # Environment variables required when creating containers
      MYSQL_ROOT_PASSWORD: root
    volumes:
      # Absolute path
      - "/opt/data/docker_mysql/data:/var/lib/mysql"
      # Relative path, relative to the current docker compose Directory of YML file
      - "./conf:/etc/mysql/conf.d"
      # Anonymous mount, anonymous mount only needs to write the container directory, and the corresponding directory outside the container will be generated in / var/lib/docker/volume
      - "/var/lib/mysql"
      # Named mount is to give a name to the data volume, and the corresponding directory outside the container will be generated in / var/lib/docker/volume
      - "mysql-data-volume:/var/lib/mysql"

# Define data volumes, which can be multiple
volumes:
  mysql-data-volume: # The entry name of a specific data volume
    name: mysql-data-volume # Data volume name, default to "project name data volume entry name"

network_mode

Set the network mode, similar to the usage of the parameter -- net host or -- network host added during docker run.

network_mode: "bridge"
network_mode: "host"
network_mode: "none"
network_mode: "service:[service name]"
network_mode: "container:[container name/id]"

networks

Configure the network to which the container is connected, and reference the entry under the top-level networks.

# You can define multiple services
services:
  nginx: # Service name
    networks: # Configure the network to which the container is connected, and reference the entry under the top-level networks
      - nginx-net # Entry name of a specific network

# You can define multiple networks. If it is not declared, a bridge network with the network name "project name _default" will be created by default
networks:
  nginx-net: # Entry name of a specific network
    name: nginx-net # Network name, default to "project name network entry name"
    driver: bridge # Network mode, the default is bridge

aliases

Alias for this service on the network. Other containers on the same network can connect to the service container using the service name or this alias. The same service can have different aliases on different networks.

# You can define multiple services
services:
  nginx: # Service name
    networks: # Configure the network to which the container is connected, and reference the entry under the top-level networks
      nginx-net: # Entry name of a specific network
        aliases: # Service alias, which can be multiple
          - nginx1 # Other containers on the same network can connect to the service container using the service name or this alias

# You can define multiple networks. If it is not declared, a bridge network with the network name "project name _default" will be created by default
networks:
  nginx-net: # Entry name of a specific network
    name: nginx-net # Network name, default to "project name network entry name"
    driver: bridge # Network mode, the default is bridge

volumes

Through the study of top-level configuration of services, you should have understood what the top-level configuration of volumes is. Here we will explain the different methods of configuration in detail.

The default name is used when creating a volume with the following data volume declaration: "project name data volume entry name".

# Describes the version information of the Compose file
version: "3.8"
# You can define multiple services
services:
  mysql:
    image: mysql:8
    container_name: mysql8
    ports:
      - "3306:3306"
    environment
      MYSQL_ROOT_PASSWORD: root
    volumes:
      # Named mount is to give a name to the data volume, and the corresponding directory outside the container will be generated in / var/lib/docker/volume
      - "mysql-data-volume:/var/lib/mysql"

# Define data volumes, which can be multiple
volumes:
  mysql-data-volume: # The entry name of a specific data volume

Data volume declarations in the following ways use custom names when creating volumes.

# Describes the version information of the Compose file
version: "3.8"
# You can define multiple services
services:
  mysql:
    image: mysql:8
    container_name: mysql8
    ports:
      - "3306:3306"
    environment
      MYSQL_ROOT_PASSWORD: root
    volumes:
      # Named mount is to give a name to the data volume, and the corresponding directory outside the container will be generated in / var/lib/docker/volume
      - "mysql-data-volume:/var/lib/mysql"

# Define data volumes, which can be multiple
volumes:
  mysql-data-volume: # The entry name of a specific data volume
    name: mysql-data-volume # Data volume name, default to "project name data volume entry name"

networks

Through the explanation of top-level configuration services, we have actually understood what is the top-level configuration of networks. Here we will explain the different ways of configuration in detail.

If the network is not declared, each project will create a bridge network with the network name "project name _default" by default.

# Describes the version information of the Compose file
version: "3.8"
# You can define multiple services
services:
  nginx:
    image: nginx
    container_name: mynginx
    ports:
      - "80:80"

# You can define multiple networks. If it is not declared, a bridge network with the network name "project name _default" will be created by default
#networks:

The following network declaration will use the default name when creating a network: "project name network entry name", and the network mode is bridge by default.

# Describes the version information of the Compose file
version: "3.8"
# You can define multiple services
services:
  nginx:
    image: nginx
    container_name: mynginx
    ports:
      - "80:80"
    networks: # Configure the network to which the container is connected, and reference the entry under the top-level networks
      nginx-net:

# Define a network, which can be multiple
networks:
  nginx-net: # Entry name of a specific network

The following network declarations will use a custom name when creating a network. You can also select the network mode through the driver. The default is bridge.

# Describes the version information of the Compose file
version: "3.8"
# You can define multiple services
services:
  nginx:
    image: nginx
    container_name: mynginx
    ports:
      - "80:80"
    networks: # Configure the network to which the container is connected, and reference the entry under the top-level networks
      nginx-net:

# Define a network, which can be multiple
networks:
  nginx-net: # Entry name of a specific network
    name: nginx-net # Network name, default to "project name network entry name"
    driver: bridge # Network mode, the default is bridge

Compose common commands

Official documents: https://docs.docker.com/compose/reference/overview/

In order to use Compose more skillfully, the following common commands let you practice more before practice makes perfect.

docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]

Some command options are as follows:

  • -f. -- File: Specifies the composition template file to use. The default is docker composition yml, which can be specified multiple times and multiple ymls;
  • -p. -- Project Name: Specifies the project name. Docker compose.com is used by default The name of the directory where the YML file is located;
  • -v: Print the version and exit;
  • --Log level: defines the log level (DEBUG, INFO, WARNING, ERROR, CRITICAL).

help

Docker compose - help view help.

[root@localhost ~]# docker-compose -help
Define and run multi-container applications with Docker.

Usage:
  docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
  docker-compose -h|--help

Options:
  -f, --file FILE             Specify an alternate compose file
                              (default: docker-compose.yml)
  -p, --project-name NAME     Specify an alternate project name
                              (default: directory name)
  -c, --context NAME          Specify a context name
  --verbose                   Show more output
  --log-level LEVEL           Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
  --no-ansi                   Do not print ANSI control characters
  -v, --version               Print version and exit
  -H, --host HOST             Daemon socket to connect to

  --tls                       Use TLS; implied by --tlsverify
  --tlscacert CA_PATH         Trust certs signed only by this CA
  --tlscert CLIENT_CERT_PATH  Path to TLS certificate file
  --tlskey TLS_KEY_PATH       Path to TLS key file
  --tlsverify                 Use TLS and verify the remote
  --skip-hostname-check       Don't check the daemon's hostname against the
                              name specified in the client certificate
  --project-directory PATH    Specify an alternate working directory
                              (default: the path of the Compose file)
  --compatibility             If set, Compose will attempt to convert keys
                              in v3 files to their non-Swarm equivalent
  --env-file PATH             Specify an alternate environment file

Commands:
  build              Build or rebuild services
  config             Validate and view the Compose file
  create             Create services
  down               Stop and remove containers, networks, images, and volumes
  events             Receive real time events from containers
  exec               Execute a command in a running container
  help               Get help on a command
  images             List images
  kill               Kill containers
  logs               View output from containers
  pause              Pause services
  port               Print the public port for a port binding
  ps                 List containers
  pull               Pull service images
  push               Push service images
  restart            Restart services
  rm                 Remove stopped containers
  run                Run a one-off command
  scale              Set number of containers for a service
  start              Start services
  stop               Stop services
  top                Display the running processes
  unpause            Unpause services
  up                 Create and start containers
  version            Show the Docker-Compose version information

config

Docker compose config - Q verify docker compose YML file. When the configuration is correct, no content is output. When the configuration is wrong, error information is output.

pull

Docker compose pull pulls the image that the service depends on.

# Pull the image of all service dependencies in the project
docker-compose pull
# Image of nginx service dependency in pull project
docker-compose pull nginx
# Pull progress information is not printed during image pulling
docker-compose pull -q

up

Docker compose up creates and starts containers for all services. Specify multiple yml plus - f options. Run in daemon mode with the - d option.

# Foreground start
docker-compose up
# Background start
docker-compose up -d
# -f specifies the composition template file to use. The default is docker composition yml, which can be specified multiple times and multiple ymls
docker-compose -f docker-compose.yml up -d 

logs

Docker compose logs view the output logs of the service container. By default, docker compose will use different colors to distinguish different service outputs. You can turn off color by -- no color.

# Output logs. Different service outputs are distinguished by different colors
docker-compose logs
# Trace log output
docker-compose logs -f
# Turn off color
docker-compose logs --no-color

ps

Docker compose PS lists the containers of all services in the project.

# Lists the containers for all services in the project
docker-compose ps
# Lists the containers for the specified services in the project
docker-compose ps nginx

run

Docker compose run executes a command on the specified service container.

# Execute echo "helloworld" on the container of the specified service in the project
docker-compose run nginx echo "helloworld"

exec

Docker compose exec enters the service container.

# Enter the container of the specified service in the project
docker-compose exec nginx bash
# When a service has multiple containers, you can enter any container under the service through the -- index parameter
docker-compose exec --index=1 nginx bash

pause

Docker compose pause pauses the service container.

# Container that suspends all services in the project
docker-compose pause
# Pauses the container for the specified service in the project
docker-compose pause nginx

unpause

Docker compose unpause recovery service container.

# Restore containers for all services in the project
docker-compose unpause
# The container for the specified service in the recovery project
docker-compose unpause nginx

restart

Docker compose restart restarts the service container.

# Restart the container for all services in the project
docker-compose restart
# Restart the container of the specified service in the project
docker-compose restart nginx

start

Docker compose start starts the service container.

# Start the container for all services in the project
docker-compose start
# Start the container for the specified service in the project
docker-compose start nginx

stop

Docker compose stop stops the service container.

# Container that stops all services in the project
docker-compose stop
# Stops the container for the specified service in the project
docker-compose stop nginx

kill

Docker compose kill stops the container of the specified service by sending a SIGKILL signal.

# Stop the container of the specified service in the project by sending SIGKILL signal
docker-compose kill nginx

rm

Docker compose RM deletes the service (stopped) container.

# Delete containers for all (stopped) services
docker-compose rm
# Stop all service containers before deleting all service containers
docker-compose rm -s
# Delete directly without asking whether to delete
docker-compose rm -f
# Delete the data volume mounted by the service container
docker-compose rm -v
# Delete the container for the specified service in the project
docker-compose rm -sv nginx

down

Stop and delete containers, networks, mirrors, data volumes for all services.

# Stop and delete the container and network of all services in the project
docker-compose stop
# Stop and delete the container, network and image of all services in the project
docker-compose down --rmi all
# Stop and delete the containers, networks and data volumes of all services in the project
docker-compose down -v

create

Docker compose create creates a container for the service, which is not recommended. It is recommended to use docker compose up to realize this function.

scale

Docker compose scale sets the number of containers the specified service runs, which is not recommended.

It is recommended to use the parameter -- scale service=num to set the quantity.

# Specify the helloworld service to start three at a time through -- scale
docker-compose up -d --scale helloworld=3

images

Docker compose images prints the image corresponding to the service container.

# Print the image corresponding to the container of all services
docker-compose images
# Prints the image corresponding to the container of the specified service
docker-compose images nginx

port

Docker compose port prints the host port mapped to a port of the specified service container.

[root@localhost docker-nginx]# docker-compose port nginx 80
0.0.0.0:80

top

Docker compose top displays the running processes.

# Displays the running process of the container for all services in the project
docker-compose top
# Displays the running process of the container for the specified service in the project
docker-compose top nginx

summary

The overall use steps of Docker Compose are relatively simple. The three steps are:

  • Use Dockerfile file to define the application environment;
  • Use docker compose The YML file defines the services that make up the application so that they can run together in an isolated environment;
  • Finally, execute the docker compose up command to create and start all services.

Although docker Compose There are a lot of contents in YML file explanation and Compose common commands, but if you want to get started quickly and use Compose, you only need to understand some of them. In the later stage, you can further study in the project production environment according to your own situation.

Topics: Docker docker compose