How to Execute Multiple Commands with Docker Compose

Developers can build, deploy, and test applications by packaging them in a container along with all of their dependencies by using Docker. It provides different tools for managing multiple Docker containers with the help of different services, such as the Docker compose command.

In this guide, we will talk about the way to execute multiple commands with Docker Compose.

How to Run Multiple Commands with Docker Compose?

Docker compose enables users to run different commands in the container. It also allows users to manage multiple applications by generating services inside the docker-compose.yml file. To do so, the logical operators are used, such as:

Using “&&” Operator

To run multiple commands using the && operator, first, create a docker-compose.yml file which includes the instructions how Docker compose will execute the multiple commands simultaneously. For instance, we have created the .yml file and added the below-given instructions:

version: '3'

    image: alpine
    command: sh -c "echo Welcome to && echo LinuxHint && echo World!"

In the above-provided code block, we have demonstrated the alpine as a base image for the Docker container. Then, we specified the three commands, “echo Welcome to”, “echo LinuxHint” and “echo World!” respectively which are separated by the && operator:

After that, run the docker-compose up command in the terminal to start and execute an entire application on a standalone host that includes multiple services:

docker-compose up

It can be seen that, all the echo statements have been printed on the terminal successfully:

Using “|” Operator

Another easiest way for executing multiple commands with Docker compose is the “|” operator. It works the same as the “&&” operator, however, its syntax is a little bit different from the “&&” operator. To understand the working of the “|” operator, we will update the previously created docker-compose.yml file. As follows:

version: "3"
   image: alpine
    - /bin/sh
    - -c
    - |
        echo "Welcome to"
        echo "LinuxHint"

As you can see, we have specified commands on separate lines and commands instructions are added:

Then, use the docker-compose up command to execute the Docker container:

docker-compose up

As a result, all the commands have been executed one by one:

Note: The “|” operator is best for keeping YAML files clean since all the commands are in a separate line.


Docker compose allows users to execute multiple commands in the container and users can also manage multiple applications by generating services inside the docker-compose.yml file. To execute multiple commands simultaneously with Docker Compose, the “&&” and “|” operators are used. Both operators work the same however their syntax is different from each other. This article has demonstrated the different operators to run multiple commands with Docker Compose.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.