Use Case: You need to run a custom shell script on your Docker container with arguments passed to the script. These arguments decide how the script runs inside the container.

We will look into running custom shell scripts inside a Docker container with command line arguments in this guide.

The key Dockerfile instructions used for this use case are

  1. ENTRYPOINT: Here you will specify the command that has to be executed when the container starts. The default ENTRYPOINT command is /bin/sh -c
  2. CMD: It acts as an argument for ENTRYPOINT.

一、Executing Commands Using CMD Vs ENTRYPOINT

Let’s take an example of the following Dockerfile. It installs http-tools and starts the ab (apache benchmark) utility using CMD and Entrypoint. Both do the same job.

Using CMD

1FROM centos:7
2MAINTAINER Devopscube
3RUN yum -y update && \
4    yum -y install httpd-tools && \
5    yum clean all
6CMD ["ab"]

Using ENTRYPOINT:

1FROM centos:7
2MAINTAINER Devopscube
3RUN yum -y update && \
4    yum -y install httpd-tools && \
5    yum clean all
6ENTRYPOINT ["ab"]

Now if you run the container from the above Dockerfile images, it will throw the following error.

1➜  docker run demo
2ab: wrong number of arguments
3Usage: ab [options] [http[s]://]hostname[:port]/path
4Options are:
5    -n requests     Number of requests to perform

The reason is, ab command requires an http endpoint as an argument to start the service.

We have two ways to get around this problem. Hardcode the HTTP endpoint argument as shown in the below examples.

Using CMD: The ab executable and HTTP URL arguments are added in separate square brackets.

1FROM centos:7
2MAINTAINER Devopscube
3RUN yum -y update && \
4    yum -y install httpd-tools && \
5    yum clean all
6CMD ["ab"] ["http://google.com/"]

Using ENTRYPOINT: The executable and argument are separated by commas in the same square bracket.

1FROM centos:7
2MAINTAINER Devopscube
3RUN yum -y update && \
4    yum -y install httpd-tools && \
5    yum clean all
6ENTRYPOINT ["ab" , "http://google.com/" ]

Here is the key difference between CMD and ENTRYPOINT

Using CMD:

Just add the full ab command at the end of the docker run command. It will override the whole CMD specified in the Dockerfile.

****Dockerfile:****

1FROM centos:7
2MAINTAINER Devopscube
3RUN yum -y update && \
4    yum -y install httpd-tools && \
5    yum clean all
6CMD ["ab"]

Docker Command:

1docker run ab-demo ab http://google.com/

Using ENTRYPOINT:

You cannot override the whole ENTRYPOINT like you do with CMD

So if you want to pass the URL argument to ENTRYPOINT, you need to pass the URL alone. The reason is we have the ab command as part of the ENTRYPOINT definition.

And the URL you pass in the run command will be appended to the ENTRYPOINT script. In this case, CMD instruction is not required in the Dockerfile.

Dockerfile:

1FROM centos:7
2MAINTAINER Devopscube
3RUN yum -y update && \
4    yum -y install httpd-tools && \
5    yum clean all
6ENTRYPOINT ["ab"]

Docker Command:

1docker run ab-demo http://google.com/

You can also use both CMD and ENTRYPOINT instructions to achieve this. Here is how the Dockerfile looks.

1FROM centos:7
2MAINTAINER Devopscube
3RUN yum -y update && \
4    yum -y install httpd-tools && \
5    yum clean all
6ENTRYPOINT ["ab"]
7CMD ["http://dummy-url.com/"]

When ENTRYPOINT and CMD used in the same Dockerfile, everything in the CMD instruction will be appended to the ENTRYPOINT as an argument.

If you run a container using the above Dockerfile, at container start, ab script will get executed with the dummy-url.com as an argument.

How To Run Custom Script Inside Docker

In this example, we have a custom shell script which accepts three command line arguments ($1, $2 & $3). If you pass true as the the first argument, the script will run in a infinite loop. Other two arguments are just to print the values.

Step 1: Create a script.sh file and copy the following contents.

1#!/bin/bash
2set -x
3while $1
4do
5    echo "Press [CTRL+C] to stop.."
6    sleep 5
7    echo "My second and third argument is $2 & $3"
8done

Step 2: You should have the script.sh is the same folder where you have the Dockerfile.

Create the Dockerfile with the following contents which copy the script to the container and runs it part of the ENTRYPOINT using the arguments from CMD.

We are passing true as the first argument, so the script will run in an infinite loop echoing batman and superman arguments as outputs.

1FROM centos:7
2MAINTAINER Devopscube
3RUN yum -y update && \
4    yum -y install httpd && \
5    yum clean all
6COPY ./script.sh /
7RUN chmod +x /script.sh
8ENTRYPOINT ["/script.sh"]
9CMD ["true", "batman", "superman"]

Step 3: Lets build this Dockerfile with image name script-demo.

1docker build -t script-demo .

Step 4: Now lets create a container named demo using script-demo image.

1docker run --name demo -d script-demo

You can check the container logs using the following command.

1docker logs demo -f

Step 4: You can also pass the CMD arguments at the end of docker run command. It will override the arguments passed in the Dockerfile. For example,

1docker run --name demo -d script-demo false spiderman hulk

Here "false spiderman hulk" will override "true", "batman", "superman" present in the docker image