Compartir:



Executing Multiple Commands in Docker Compose



Executing Multiple Commands in Docker Compose

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.

Table of contents

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 "ars-electronica""

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

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.

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 "ars-electronica" && 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 ars-electronica 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  | ars-electronica
server_1  | docker
dockercompose_server_1 exited with code 0

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

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 "ars-electronica"
        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  | ars-electronica
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.

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.
Ariel Sperduti avatar

Full Stack web development, Ingeniero Electronico

Ir Arriba!