Generic Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

Docker is a useful tool for packaging applications in an isolated environment. It simplifies the process of deploying applications on multiple platforms. The docker run command is used to start a new container from a Docker image. By default, the docker run command only executes a single command in the container. However, there are situations where we may need to run multiple commands in a single docker run command.

In this tutorial, we'll discuss how to run multiple commands on the startup of a Docker container.

2. Using the docker run Command

To execute multiple commands in the docker run command, we can use the && operator to chain the commands together. The && operator executes the first command, and if it's successful, it executes the second command.

For example, in order to run the whoami and date commands in a single run command, we can use the following command:

$ docker run centos:latest whoami && date
root
Sun Dec 18 10:09:30 UTC 2022

In the output above, we can see that both the whoami and date commands were executed in the correct order.

Alternatively, we can use the -c option with the shell to execute multiple commands simultaneously. The sh -c option allows us to pass a string containing multiple commands as an argument. Let's run the whoami and date commands using sh -c:

$ docker run centos:latest sh -c "whoami && date"
root
Sun Dec 18 10:10:12 UTC 2022

This executes both the whoami and date commands in a single run command. We can also use the ; operator with the -c option of sh to run multiple commands. In addition, let's use the -w option to specify the working directory for the command to execute in the Docker container:

$ docker run -w /home centos:latest sh -c "whoami ; pwd"
root
/home

Here, we can see that both whoami and pwd commands executed, but this time /home is the default working directory of the docker container to execute these commands.

3. Using CMD/ENTRYPOINT in the Dockerfile

In addition to running multiple commands in the run command, we can also specify multiple commands in the CMD/ENTRYPOINT section of a Dockerfile.

The CMD and ENTRYPOINT of the Dockerfile define the default commands to execute on container launch. If we add more than one command to the ENTRYPOINT and CMD sections, Docker runs them sequentially.

Let's look at the Dockerfile to specify multiple commands in the ENTRYPOINT section:

FROM centos 
ENTRYPOINT ["sh", "-c", "whoami && date"]

In order to run the container, we need to first build the image:

$ docker build -f Dockerfile -t baeldung_run .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM centos
 ---> 5d0da3dc9764
Step 2/2 : ENTRYPOINT ["sh", "-c", "whoami && date"]
 ---> Running in dd027b5ba1e9
Removing intermediate container dd027b5ba1e9
 ---> a43dfa09d48b
Successfully built a43dfa09d48b
Successfully tagged baeldung_run:latest

Let's run a container using the above baeldung_run:latest image:

$ docker run -itd --name baeldung_run baeldung_run 
b2a8ff012797d6110fd73dfffbf3c39e081f111dc50aac5d9d62fa73845b8a59

Now, we can verify the execution of the commands by looking at the baeldung_run container's log files:

$ docker logs -f baeldung_run
root
Sun Dec 18 10:13:44 UTC 2022

We can see from the above output that both commands were executed successfully.

We can also run multiple commands using the CMD directives. Let's look at the Dockerfile with the CMD directives:

FROM centos 
CMD ["sh", "-c", "whoami && date"]

Again, we need to first create the Docker image and then run a container. In the container logs, we'll see the same output that was generated using the ENRTYPOINT directive.

We should note that the CMD and ENTRYPOINT directives can't be used as a replacement for each other. They both serve different purposes. But, running multiple commands in ENTRYPOINT and CMD follows the same syntax. 

4. Conclusion

In this article, we've discussed how to run multiple commands in a running Docker container.

First, we learned to execute multiple commands using the docker run command. After that, we explored the same using the ENTRYPOINT/CMD directives in Dockerfile.

Generic bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
Comments are closed on this article!