How to pass arguments to Shell Script through docker run
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Shell scripting is a fundamental skill for automating tasks on Unix-like systems. When using Docker, you might need to execute these shell scripts within a container and pass arguments to them. This article provides a detailed guide on passing arguments to shell scripts in a Docker container using the docker run command.
Understanding Docker and Shell Scripts
Docker is a platform that uses containerization to execute applications within isolated environments. Shell scripts are files containing a sequence of commands for the shell to execute.
Why Pass Arguments?
Passing arguments to shell scripts allows for dynamic execution of the script with varying inputs without the need to modify the script itself. This ability enhances automation and flexibility, crucial in environments like CI/CD pipelines.
Basic Structure
To pass arguments to a shell script within a Docker container, you usually follow this pattern:
Here, [COMMAND] often refers to the shell or script you wish to execute, and [ARG...] represents the arguments you want to pass.
Steps to Pass Arguments to Shell Scripts in Docker
Step 1: Create a Docker Image
- Write the Shell ScriptCreate a shell script that can accept arguments. For example, create a
hello.shscript:
- Create a DockerfileBuild a Docker image that contains this script:
This Dockerfile uses the Alpine Linux base image, copies the hello.sh script to the container, makes it executable, and sets it as the default command.
Step 2: Build the Docker Image
Execute the following command to build the Docker image, naming it helloscript:
Step 3: Run the Docker Container
Execute the Docker container with arguments:
In this instance, the script will output Hello, World.
Handling Multiple Arguments
Modify the hello.sh script to handle multiple arguments:
Run it as follows:
This outputs: Hello, Docker Enthusiast.
Using Entrypoint with Arguments
Docker provides the ENTRYPOINT instruction in the Dockerfile, which can be combined with CMD to specify both the entrypoint script and default arguments. This combination allows more structured argument handling.
Example of a Dockerfile with ENTRYPOINT:
You can then run the container with:
The script now runs directly with the argument provided.
Summary Table
| 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"] |
Advanced Topics
Using Environment Variables
For more complex argument handling, consider using environment variables. You can pass environment variables using -e flag or --env-file option.
Argument Introspection
In more sophisticated scripts, you may want to introspect provided arguments. Consider updating the script to loop through all arguments:
Run it with multiple arguments to see all entries:
Conclusion
Passing arguments to shell scripts through Docker is an essential technique for writing flexible and reusable automation scripts. By correctly structuring your Dockerfile and understanding the docker run command's syntax, you can effectively pass parameters to your scripts, enhancing their utility and adaptability in various environments.
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 Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.