Common commands of docker-compose (with docker-compose installation tutorial)

1. Introduction to docker-compose

1. Introduction to docker-compose

More official statement:

  • The Docker-Compose project is an official open source project of Docker, which is responsible for the rapid orchestration of Docker container clusters. Docker-Compose divides the managed containers into three layers, namely project, service and container. docker-compose.ymlAll files ( , extendsfiles or environment variable files, etc.) in the Docker-Compose running directory form a project, and the project name is the current directory name if there is no special designation. A project can contain multiple services, and each service defines the image, parameters, and dependencies of the container running. A service can include multiple container instances. Docker-Compose does not solve the problem of load balancing, so other tools are needed to realize service discovery and load balancing.
  • The default project configuration file of Docker-Compose is that the configuration file docker-compose.ymlcan be customized through environment variables COMPOSE_FILEor -fparameters, which defines multiple dependent services and the containers that 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 certain 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, and even a load balancing container. docker-compose.ymlCompose allows users to define a set of associated application containers as a project through a single template file (YAML format). The Docker-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 the Docker API, Compose can be used for orchestration management on it.

In layman's terms:

  • When we use Docker to run an application, there are usually multiple containers involved. For example, an application might require a database container, a web server container, and a cache container. These containers need to communicate with each other and coordinate their work.
  • Docker Compose is like a commander, it can help us start, stop and manage these containers at once to ensure that they can work together. You can think of the Docker Compose file as a blueprint that tells Docker how to build and configure the container farm.
  • Let's say your application requires a database container and a web server container. You can use a Docker Compose file to describe these two containers, specifying configuration information such as mirrors, port mappings, and environment variables they use. Then, you only need to run one command, and Docker Compose will automatically create and manage these two containers for you.
  • Docker Compose files are written in a language called YAML, which is very concise and easy to understand. You only need to define the services (containers) you need, and the relationship and configuration between them. Docker Compose will create, connect and manage these containers based on this file to ensure that they can communicate with each other and run smoothly.
  • This approach makes it easier for us to manage complex application environments because we can centralize the configuration of all containers in one file and use simple commands to start, stop, and manage them. Instead of manually running Docker commands one by one to manage each container.

All in all, Docker Compose is a tool that simplifies the management of multi-container Docker applications. It uses a configuration file to define and manage how multiple containers run, making application deployment and management easier and more efficient.

2. docker-compose installation

  • Install Docker Compose by running the following command:
# 要安装其他版本的 Compose,请替换 1.24.1
curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

Note: If you use the above command directly, on the one hand, the download speed will be very slow; on the other hand, it may prompt that the download fails when a part of the file is downloaded. So based on the above reasons, we can download the above file in the browser, then transfer the file to the virtual machine (I am Ubuntu here), and then use the following command to rename the file:

mv /root/docker-compose-linux-x86_64 /usr/local/bin/docker-compose

Explanation: in the above command /root/docker-compose-linux-x86_64is the path where the downloaded file is stored in my virtual machine, and docker-compose-linux-x86_64is the name of the downloaded file.

  • After entering the above command, wait for the download to complete. Apply executable permissions to the binary:
chmod +x /usr/local/bin/docker-compose
  • Create a soft link:
ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
  • Test whether the installation is successful:
# 查看 docker-compose 版本
docker-compose --version

insert image description here

3. docker-compose uninstall

  • If it is installed as a binary package, just delete the binary file:
rm /usr/local/bin/docker-compose
  • If it is installed through the Python pip tool, execute the following command to delete:
pip uninstall docker-compose

2. docker-compose common commands

1. docker-compose command format

  • We can use the following command to view:
docker-compose --help

insert image description here

  • You can see that the command format of docker-compose is:
docker compose [OPTIONS] COMMAND

2. docker-compose up

  • Used to start multiple containers orchestrated with Docker Compose. docker-compose.ymlIt will create and run multiple containers according to the specified configuration file (the default ), making them work together.

insert image description here

  • As you can see, the command format is:
docker compose up [OPTIONS] [SERVICE...]
  • Parameter Description:
parameter illustrate
-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 the service is linked to
–force-recreate Force container to be recreated, cannot be used with --no-recreate
–no-recreate Do not recreate the container if it already exists, cannot be used with --force-recreate
–no-build Do not automatically build missing service images
–build Build the service image before starting the container
–abort-on-container-exit Stop all containers, if any container is stopped, cannot be used with -d
-t,-–timeout int Timeout when stopping the container (default 10 seconds)
–remove-orphans Delete containers that are not defined in the compose file in the service
-f Specify the Compose template file to use, the default is docker-compose.yml, which can be specified multiple times
  • Example:
# 启动所有服务
docker-compose up

# 在后台启动所有服务
docker-compose up -d

# 在后台所有启动服务,指定编排文件
docker-compose -f docker-compose.yml up -d

Note: Enter the command directly in a directory docker-compose psor docker-compose up, an error may be reported: no configuration file provided: not found. docker-compose.ymlThis error message indicates that the Docker Compose configuration file ( or ) cannot be found in the current directory docker-compose.yaml. docker-composeBy default, the command will search for configuration files in the current directory to perform corresponding operations. So, the solution is very simple: just switch to the directory where the docker-compose configuration file is located (or the installation directory of docker-compose).

3. docker-compose ps

  • Used to list the status of containers managed by Docker Compose. It displays the details of the containers associated with the service defined in the configuration file (a service consists of one or more containers).

insert image description here

  • As you can see, the command format is:
docker compose ps [OPTIONS] [SERVICE...]
  • Example:
# 基本用法
docker-compose ps

By default, it looks for a configuration file named docker-compose.yml or docker-compose.yaml in the current directory and displays status information for containers associated with services defined in that file.

insert image description here

Explanation: As shown in the figure above, if you directly enter the command docker-compose psor in a certain directory docker-compose up, an error may be reported: no configuration file provided: not found. docker-compose.ymlThis error message indicates that the Docker Compose configuration file ( or ) cannot be found in the current directory docker-compose.yaml. docker-composeBy default, the command will search for configuration files in the current directory to perform corresponding operations. So, the solution is very simple: just switch to the directory where the docker-compose configuration file is located (or the installation directory of docker-compose).

# 指定配置文件
docker-compose -f <file> ps

Use -fthe option to specify which configuration file to use. For example, to use my-compose.ymla configuration file named , you would run docker-compose -f my-compose.yml ps.

# 显示详细信息
docker-compose ps -a

Use -athe option to display details for all containers, including stopped ones. By default it only shows running containers.

4. docker-compose stop

  • Stop a running container and docker-compose startstart it again with .

insert image description here

  • As you can see, the command format is:
docker compose stop [OPTIONS] [SERVICE...]

5. docker-compose down

  • Used to stop and remove docker-compose upcontainers, networks and volumes created by .
  • Using docker-compose downthe command does the following:
    • Stop docker-compose upthe container started by .
    • Remove these containers.
    • Remove associated network.
    • Remove associated volumes.

insert image description here

  • As you can see, the command format is:
docker compose down [OPTIONS]
  • Parameter Description:
parameter illustrate
–rmi type Remove the associated image. typeThe argument can be "all" (remove all images), "local" (remove only local images), or "none" (do not remove images). By default, images are not deleted.
-vor--volumes Remove associated volumes. By default, docker-compose downvolumes are not deleted, use this option to remove associated volumes.
--remove-orphans Remove orphaned containers. If docker-compose.ymlservices are defined in the file, but they are no longer running, use this option to remove these orphaned containers.

6. docker-compose logs

  • Used to view docker-composelogs for services started by .

insert image description here

  • As you can see, the command format is:
docker compose logs [OPTIONS] [SERVICE...]
  • Parameter Description:
parameter illustrate
-for--follow Output log information in real time, similar to tail -fthe function of the command. You can continue to view the output of the log until manually stopped.
--tail <num> Only the last <num>line of log information is displayed. Positive integers can be used as arguments, eg --tail 100will display only the last 100 rows.
-tor--timestamps Display timestamp information.
--no-color Disable output color coding, display logs in plain text.
  • Usage example:

Run the command in the project directory: Open a terminal, go to docker-compose.ymlthe project directory containing the file, and run docker-compose logsthe command.

cd /path/to/project
docker-compose logs

Specify service name: By default, docker-compose logslogs from all services are displayed. If you only want to see logs for a specific service, you can specify the service name as an argument.

docker-compose logs <service1> <service2>

where <service1>and <service2>are the names of the services whose logs you want to view.

7. docker-compose build

  • Build (rebuild) the service container in the project.

insert image description here

  • As you can see, the command format is:
docker compose build [OPTIONS] [SERVICE...]
  • Parameter Description:
parameter illustrate
-for--file Specify a custom Compose file instead of using the default docker-compose.ymlone.
  • Usage example:

Run the command in the project directory: Open a terminal, go to docker-compose.ymlthe project directory containing the file, and run docker-compose buildthe command.

cd /path/to/project
docker-compose build

Specify service name: By default, docker-compose buildall services in the project will be mirrored. If you only want to mirror a specific service, you can specify the service name as an argument.

docker-compose build <service1> <service2>

where <service1>and <service2>are the names of the services to be mirrored.

8. docker-compose pull

  • Pull the image that the service depends on.

insert image description here

  • As you can see, the command format is:
docker compose pull [OPTIONS] [SERVICE...]
  • Parameter Description:
parameter illustrate
–ignore-pull-failures Ignore errors during image pull
-qor--quiet Only the progress information during the image pulling process is displayed, and the detailed output is not displayed.

9. docker-compose restart

  • Used to restart docker-composeservice containers started by .

insert image description here

  • As you can see, the command format is:
docker compose restart [OPTIONS] [SERVICE...]

10. docker-compose rm

  • Delete all (stopped) service containers.

insert image description here

  • As you can see, the command format is:
docker compose rm [OPTIONS] [SERVICE...]
  • Parameter Description:
parameter illustrate
-for--force Forcefully delete a container, even if it is running or attached to a terminal.
-sor--stop Stop the containers before removing them.
-v Delete the data volume mounted by the container
  • docker-compose rmDelete all (stopped) service containers. It is recommended to execute the command first docker-compose stopto stop the container.
  • Deleting a container with docker-compose rmthe command will not delete associated images, networks or volumes. To delete these related resources, you can use other Docker commands, such as docker image rmto delete an image, docker network rmto delete a network, docker volume rmto delete a volume.

11. docker-compose start

  • Used to start docker-composeservice containers managed by .

insert image description here

  • As you can see, the command format is:
docker compose start [SERVICE...]
  • docker-compose startThe command will start the service container in background mode, i.e. the container will run in the background and no output will be displayed in the terminal. If you want to view the log output of the container, you can use docker-compose logsthe command.
  • This command is useful for restarting stopped service containers, especially after updating configuration or images for changes to take effect. If you just want to stop the service container without reloading the configuration or image, you can use docker-compose stopthe command.

12. docker-compose run

  • Execute a command on the specified service.

insert image description here

  • As you can see, the command format is:
docker compose run [OPTIONS] SERVICE [COMMAND] [ARGS...]
  • Usage example:

Run the command in the project directory: Open a terminal, go to docker-compose.ymlthe project directory containing the file, and run docker-compose runthe command.

cd /path/to/project
docker-compose run <service> <command>

where <service>is docker-compose.ymlthe service name defined in the file specifying the container in which to run the command. <command>is the command to execute in the container.

Attach to Terminal: By default, docker-compose runthe command starts a new container, runs the command in it, and then exits. If you want to operate interactively in the container, you can use -itthe option to attach the command to the terminal.

docker-compose run -it <service> <command>

In this case, the command will run in the container's interactive mode and can interact with the container.

13. docker-compose scale

  • docker-compose scaleCommands are used to scale up or down the number of service instances in Docker Compose.

  • Use docker-compose scalethe command to specify the number of service instances to start, thereby creating multiple container instances to handle load or increase service availability.

  • Usage example:

Run the command in the project directory: Open a terminal, go to docker-compose.ymlthe project directory containing the file, and run docker-compose scalethe command.

cd /path/to/project
docker-compose scale <service1>=<num1> <service2>=<num2> ...

Among them, <service1>, <service2>are docker-compose.ymlthe service names defined in the file, which are used to specify the service whose number of instances is to be scaled up or down. <num1>, <num2>is the number of instances to set.

docker-compose scale web=3 worker=2

This example will scale webthe service to 3 instances and workerthe service to 2 instances.

Dynamic scaling: If you want to automatically adjust the load balancing strategy when expanding the number of containers, you can docker-compose.ymluse an appropriate load balancer (such as Nginx or Traefik) for the service in the file.

  • It should be noted that the scaling up and down of the service is based on the service container image defined in the launch configuration. If docker-compose.ymlmultiple containers are defined in the file, each container will use the same image to create an identical copy of the original service.

14. docker-compose pause

  • docker-compose pauseis a Docker Compose command to pause a running service container.
  • Use docker-compose pausethe command to pause a running service container, causing it to stop executing. This will suspend all processes in the container, including applications and services.

insert image description here

  • As you can see, the command format is:
docker compose pause [SERVICE...]

15. docker-compose kill

  • docker-compose killis a Docker Compose command to stop a running service container. It terminates the container immediately, similar to forcing a Ctrl+C interrupt.
  • Use docker-compose killthe command to forcibly terminate a running service container, even though processes inside the container may not have completed or are currently executing.

insert image description here

  • As you can see, the command format is:
docker compose kill [OPTIONS] [SERVICE...]

16. dokcer-compose config

  • docker-compose configis a Docker Compose command that validates and displays docker-compose.ymlthe service configuration defined by the file.
  • Use docker-compose configthe command to check the syntax correctness of a Docker Compose configuration file and to display the defined services and their configuration. It can help you see if the configuration is correct before running Docker Compose.

insert image description here

  • As you can see, the command format is:
docker compose config [OPTIONS] [SERVICE...]

17. docker-compose create

  • docker-compose createcommand to create containers for the services defined in the Docker Compose file, but not to start those containers. It is mainly used to pre-create containers so that they can be started later via docker-compose startthe or command.docker-compose up

insert image description here

  • As you can see, the command format is:
docker compose create [OPTIONS] [SERVICE...]

18. docker-compose exec

  • Used to execute commands in a running service container. It allows you to interact with a running container and execute commands inside it.
  • Use docker-compose execthe command to enter the specified service container and execute the given command in the context of the container.

insert image description here

  • As you can see, the command format is:
docker compose exec [OPTIONS] SERVICE COMMAND [ARGS...]
  • Usage example:
# 这个例子将会进入名为 web 的服务容器,并在容器内执行 ls -l 命令来列出文件和目录。
docker-compose exec web ls -l

19. docker-compose port

  • Used to view port mappings for service containers managed by Docker Compose. It allows you to see the mapping between the exposed ports of the service container and the ports on the host.
  • It is convenient to view the port mapping of the service container using docker-compose portthe command to know the accessible address of the application inside the service container.

insert image description here

  • As you can see, the command format is:
docker compose port [OPTIONS] SERVICE PRIVATE_PORT
  • Usage example:
# 这个例子将会显示名为 web 的服务容器绑定到宿主机上的端口 80 的映射。
docker-compose port web 80

20. docker-compose push

  • Used to push the image of the service defined in the Docker Compose file to the Registry. It allows you to push locally built images to remote registries for use by other people or environments.
  • Use docker-compose pushthe command to easily upload images to the registry for deployment and use in other environments.

insert image description here

  • As you can see, the command format is:
docker compose push [OPTIONS] [SERVICE...]
  • Usage example:
# 这个例子将会推送名为 web 的服务的镜像到注册表。
docker-compose push web

21. docker-compose unpause

  • Used to unpause one or more service containers managed by Docker Compose. It allows you to resume the normal operation of a suspended service container.
  • Use docker-compose unpausethe command to resume a suspended service container so that it continues to perform what it should.

insert image description here

  • As you can see, the command format is:
docker compose unpause [SERVICE...]
  • Usage example:
# 这个例子将会取消暂停名为 web 和 db 的服务容器。
docker-compose unpause web db

Guess you like

Origin blog.csdn.net/m0_51913750/article/details/131743170