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:
[COMMAND] and [ARG...] after the image name are what reach the container.
Writing the script
Create a hello.sh that reads its first argument:
CMD vs ENTRYPOINT
A first attempt is often a Dockerfile that sets the script as CMD:
This uses the Alpine base image, copies hello.sh in, makes it executable and sets it as the default command. Build it as 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:
Now this runs the script with the argument and prints Hello, World:
The same goes for any other value:
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:
Then run:
This prints Hello, Docker Enthusiast.
To handle any number of arguments, loop over "$@":
Using environment variables instead
For more complex input, pass environment variables with -e or --env-file:
| Component | Description | Example |
| Shell Script | Script file containing command sequence | #!/bin/sh echo "Hello, $1" |
| Dockerfile | Instructions to create Docker image | FROM alpine:latest ... |
| Build Command | Command to build Docker image | docker build -t helloscript . |
| Run Command | Execute container with arguments | docker run helloscript Alice |
| ENTRYPOINT | Dockerfile directive to set initial command | ENTRYPOINT ["/hello.sh"] |
| CMD | Dockerfile directive to set default parameters | CMD ["default"] |
Related reading
- How to pass Docker CLI --gpus Options in Kubernetes or enable GPU support without installing nvidia-docker2 Docker 19.03
- How to pass docker run parameter via kubernetes pod
- How to pass environment variable to docker-compose up
- How to pass image pull secret while using 'kubectl run' command?
- How to perform kaniko Docker build and push in separate GitLab CI stages?
- How to persist data in a dockerized postgres database using volumes
- How to persist data using a postgres database, Docker, and Kubernetes?
- How to prevent docker from starting a container automatically on system startup?
System design interview problems on this topic
- Design a Container Orchestration System: solution and walkthrough
- Design a Code Deployment System: solution and walkthrough
- Design GitHub Actions: solution and walkthrough
- Design a Serverless Architecture Framework: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.