Running Custom Scripts In Docker With Arguments – ENTRYPOINT Vs CMD

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

In this guide, we will look int to running custom shell scripts inside a Docker container with command line arguments.

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

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


    Using ENTRYPOINT: 

  2. If you build this image and run the container, it will throw the following error. The reason is, ab command requires a http endpoint as an argument to start the service.
  3. We have two ways to get around this problem. Hardcode the http endpoint argument as shown below.
    Using CMD:



  4. Or, you can pass the ab command with the http endpoint at the end of the docker run command. 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.


    Docker Command:

    You cannot override the whole ENTRYPOINT. So if you want to pass URL argument for ab using ENTRYPOINT, you just need to pass the URL as the ab command is part of ENTRYPOINT and the URL you pass in the run command will be overridden by CMD and gets appended to the ENTRYPOINT script. In this case, CMD instruction is not required in the Dockerfile

    Docker Command:

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

    When ENTRYPOINT and CMD used in the same Dockerfile, everything in the CMD instruction will be appended to the ENTRYPOINT as argument. If you run a container using the above Dockerfile, at container start, ab script will get executed with the dummy-url 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 file and copy the following contents.

Step 2: You should have the is the same folder where you have the Dockerfile. Create the Dockerfile with the following contents which copies the script to container and runs it it 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.

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

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

You can check the container logs using the following command.

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,

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

Free DevOps Resources

Get DevOps news, tutorials and resources in your inbox. A perfect way If you want to get started with devops. Like you, we dont like spam.