Executing Multiple Commands in Docker Compose – 在Docker Compose中执行多个命令

最后修改: 2022年 8月 14日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In Docker, developers can build, deploy, and test applications by packaging them in a container along with all of their dependencies. Docker Compose is an essential tool for managing multiple containers using services.

Docker中,开发人员可以通过将应用程序连同其所有依赖项打包到容器中来构建、部署和测试应用程序。Docker Compose是使用服务来管理多个容器的重要工具。

In this tutorial, we’ll understand how to execute multiple commands in a Docker container, managed using Docker Compose. Additionally, we’ll explore different ways to run multiple commands in the Docker container.

在本教程中,我们将了解如何在一个使用Docker Compose管理的Docker容器中执行多个命令。此外,我们将探索在Docker容器中运行多个命令的不同方法。

2. Running Single Command

2.运行单一命令

Docker Compose allows us to execute commands inside a Docker container. During the container startup, we can set any command via the command instruction.

Docker Compose允许我们在Docker容器内执行命令。在容器启动期间,我们可以通过command指令设置任何命令。

Let’s take a look at a docker-compose.yml, which runs a simple command inside a container:

让我们看一下docker-compose.yml,它在容器内运行一个简单的命令。

version: "3"
services:
 server:
   image: alpine
   command: sh -c "echo "baeldung""

In the above docker-compose.yml file, we are executing a single echo command inside the alpine Docker image.

在上面的docker-compose.yml文件中,我们在echo的Docker镜像内执行一个echo命令。

3. Running Multiple Commands

3.运行多条命令

We can use Docker Compose to manage multiple applications by creating services in the docker-compose.yml file. Specifically, we’ll use the && and | operators. Let’s take a look at both.

我们可以通过在docker-compose.yml文件中创建服务来使用Docker Compose管理多个应用程序。具体来说,我们将使用&&|操作符。让我们看看这两个操作符。

3.1. Using the && Operator

3.1.使用&&操作符

We’ll start by creating a simple docker-compose.yml file to demonstrate how Docker Compose runs multiple commands:

我们将首先创建一个简单的docker-compose.yml文件来演示Docker Compose如何运行多个命令。

version: "3"
services:
 server:
   image: alpine
   command: sh -c "echo "baeldung" && echo "docker" "

Here, we used alpine as the base image for the Docker container. Notice the last line where we’re executing two commands: echo baeldung and echo docker. They are split by the && operator.

这里,我们使用alpine作为Docker容器的基础镜像。注意最后一行,我们正在执行两个命令。echo baeldungecho docker。它们被&&操作符分开。

To demonstrate the result, let’s run this image using the docker-compose up command:

为了演示结果,让我们使用docker-compose up命令运行这个图像。

$ docker-compose up
Creating dockercompose_server_1 ... done
Attaching to dockercompose_server_1
server_1  | baeldung
server_1  | docker
dockercompose_server_1 exited with code 0

Here, the output of both echo statements is printed on the stdout.

这里,两个echo语句的输出都被打印在stdout上。

3.2. Using the | Operator

3.2.使用|操作符

We can also use the | operator to run multiple commands in Docker Compose. The syntax of the | operator is a bit different from the && operator.

我们还可以使用|操作符在Docker Compose中运行多个命令。|操作符的语法与&&操作符有些不同。

To illustrate how the | operator works, let’s update our docker-compose.yml:

为了说明|操作符如何工作,让我们更新我们的docker-compose.yml

version: "3"
services:
 server:
   image: alpine
   command:
      - /bin/sh
      - -c
      - |
        echo "baeldung"
        echo "docker"

Here, we added the commands on separate lines. Everything is the same except for the command instruction.

在这里,我们把命令加在不同的行上。除了command指令,其他都是一样的。

This approach is recommended as it keeps our YAML file clean since all commands are in a separate line.

这种方法是值得推荐的,因为它使我们的YAML文件保持干净,因为所有命令都在一个单独的行中。

Let’s again run the Docker container with the docker-compose up command:

让我们再次用docker-compose up命令运行Docker容器。

$ docker-compose up
Creating dockercompose_server_1 ... done
Attaching to dockercompose_server_1
server_1  | baeldung
server_1  | docker
dockercompose_server_1 exited with code 0

As we can see from the above output, both commands are executed one by one.

从上面的输出中我们可以看到,两个命令都是逐一执行的。

4. Conclusion

4.总结

In this article, we demonstrated how to execute multiple commands in Docker Compose. First, we saw how to execute more than one command using the && operator. Later, we used the | operator to achieve similar results.

在这篇文章中,我们演示了如何在Docker Compose中执行多个命令。首先,我们看到了如何使用&&操作符来执行多个命令。后来,我们使用|操作符来实现类似的结果。