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.
But, to avoid running the second command on the host shell, we have to use the -c option of the sh command to execute multiple commands simultaneously.
Let’s run the whoami and date commands using sh -c:
$ docker run centos:latest sh -c "whoami && date"
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"
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:
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
Step 2/2 : ENTRYPOINT ["sh", "-c", "whoami && date"]
---> Running in dd027b5ba1e9
Removing intermediate container dd027b5ba1e9
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
Now, we can verify the execution of the commands by looking at the baeldung_run container’s log files:
$ docker logs -f baeldung_run
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:
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.
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.