Docker
Shell Scripting
Command Line
Docker Run
Passing Arguments

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.

Practice system design

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:

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

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

  1. Write the Shell Script
    Create a shell script that can accept arguments. For example, create a hello.sh script:
bash
   #!/bin/sh
   echo "Hello, $1"
  1. Create a Dockerfile
    Build a Docker image that contains this script:
dockerfile
1   FROM alpine:latest
2   COPY hello.sh /hello.sh
3   RUN chmod +x /hello.sh
4   CMD ["/hello.sh"]

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:

bash
docker build -t helloscript .

Step 3: Run the Docker Container

Execute the Docker container with arguments:

bash
docker run helloscript World

In this instance, the script will output Hello, World.

Handling Multiple Arguments

Modify the hello.sh script to handle multiple arguments:

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

Run it as follows:

bash
docker run helloscript Docker Enthusiast

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:

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

You can then run the container with:

bash
docker run helloscript Alex

The script now runs directly with the argument provided.

Summary Table

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"]

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.

bash
docker run -e NAME=Sunny helloscript

Argument Introspection

In more sophisticated scripts, you may want to introspect provided arguments. Consider updating the script to loop through all arguments:

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

Run it with multiple arguments to see all entries:

bash
docker run helloscript one two three

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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

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

View the course
Track 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.

Practice system design

All Rights Reserved.