1. Overview

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.

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.

2. Running Single Command

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

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

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.

3. Running Multiple Commands

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.

3.1. Using the && Operator

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

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.

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

$ 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.

3.2. Using the | Operator

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.

To illustrate how the | operator works, let’s update our 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.

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

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

$ 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

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.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.