Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:

>> GET ACCESS NOW

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:

>> GET ACCESS NOW

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.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:

>> GET ACCESS NOW

Generic footer banner
Comments are closed on this article!