Docker
Shell Scripting
Command Line
Docker Run
Passing Arguments

How to pass arguments to a shell script through docker run

Codemia Knowledge Hub ·

Make the script the image's ENTRYPOINT. Anything you write after the image name in docker run is then passed to the script as $1, $2 and so on. Passing arguments lets one image run the same script with different inputs, which is useful in automation such as CI/CD pipelines.

The general form is:

bash
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

[COMMAND] and [ARG...] after the image name are what reach the container.

Writing the script

Create a hello.sh that reads its first argument:

bash
#!/bin/sh
echo "Hello, $1"

CMD vs ENTRYPOINT

A first attempt is often a Dockerfile that sets the script as CMD:

dockerfile
1FROM alpine:latest
2COPY hello.sh /hello.sh
3RUN chmod +x /hello.sh
4CMD ["/hello.sh"]

This uses the Alpine base image, copies hello.sh in, makes it executable and sets it as the default command. Build it as helloscript:

bash
docker build -t helloscript .

With CMD, however, arguments given to docker run replace the default command instead of being appended to it. Running docker run helloscript World against this image would try to execute World as the command rather than pass it to hello.sh.

ENTRYPOINT fixes this. Use it instead of CMD and rebuild:

dockerfile
1FROM alpine:latest
2COPY hello.sh /hello.sh
3RUN chmod +x /hello.sh
4ENTRYPOINT ["/hello.sh"]

Now this runs the script with the argument and prints Hello, World:

bash
docker run helloscript World

The same goes for any other value:

bash
docker run helloscript Alex

You can still pair ENTRYPOINT with CMD to supply default arguments that docker run arguments override.

Passing multiple arguments

Change hello.sh to read more positional parameters:

bash
#!/bin/sh
echo "Hello, $1 $2 $3"

Then run:

bash
docker run helloscript Docker Enthusiast

This prints Hello, Docker Enthusiast.

To handle any number of arguments, loop over "$@":

bash
1#!/bin/sh
2echo "Script arguments:"
3for arg in "$@"
4do
5    echo $arg
6done
bash
docker run helloscript one two three

Using environment variables instead

For more complex input, pass environment variables with -e or --env-file:

bash
docker run -e NAME=Sunny helloscript
ComponentDescriptionExample
Shell ScriptScript file containing command sequence#!/bin/sh echo "Hello, $1"
DockerfileInstructions to create Docker imageFROM alpine:latest ...
Build CommandCommand to build Docker imagedocker build -t helloscript .
Run CommandExecute container with argumentsdocker run helloscript Alice
ENTRYPOINTDockerfile directive to set initial commandENTRYPOINT ["/hello.sh"]
CMDDockerfile directive to set default parametersCMD ["default"]

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design