Methods and Practices for Passing Command Line Arguments to Shell Scripts in Docker Containers

Nov 13, 2025 · Programming · 20 views · 7.8

Keywords: Docker | Shell Script | Command Line Arguments | Containerization | ENTRYPOINT

Abstract: This article provides an in-depth exploration of technical implementations for passing command line arguments to shell scripts within Docker containers. By analyzing the interaction mechanisms between CMD and ENTRYPOINT instructions in Dockerfiles, it详细介绍s two main methods for parameter passing using docker run commands: directly overriding CMD parameters and using ENTRYPOINT to receive arguments. The article compares applicable scenarios for different methods with specific code examples and discusses environment variables as an alternative approach. Content covers Docker command execution principles, parameter passing mechanisms, and best practice recommendations, offering comprehensive guidance for developers to flexibly handle command line arguments in containerized environments.

Introduction

In Docker containerized deployment processes, there is often a need to pass runtime parameters to applications within containers. This article systematically analyzes how to effectively pass command line arguments to shell scripts in Docker containers based on actual development scenarios.

Fundamentals of Docker Parameter Passing

Docker provides flexible mechanisms to handle parameter passing during container startup. Understanding the interaction between CMD and ENTRYPOINT instructions is crucial for mastering parameter passing.

Method 1: Directly Overriding CMD Parameters

This is the simplest and most direct approach for parameter passing. Consider the following shell script example:

#!/bin/bash
echo $1

The corresponding Dockerfile configuration is as follows:

FROM ubuntu:14.04
COPY ./file.sh /
CMD /bin/bash file.sh

After building the image, parameters can be passed using the following commands:

docker build -t test .
docker run -ti --rm test /file.sh abc
docker run -ti --rm test /file.sh xyz

In this method, parameters following /file.sh in the docker run command completely replace the CMD instruction defined in the Dockerfile. This means the original CMD /bin/bash file.sh is replaced with /file.sh abc, and the parameter abc is directly passed to the shell script.

Method 2: Using ENTRYPOINT to Receive Arguments

Another more elegant approach is using the ENTRYPOINT instruction. Modify the Dockerfile as follows:

FROM ubuntu:14.04
COPY ./file.sh /
ENTRYPOINT ["/file.sh"]

Simultaneously modify the shell script to handle multiple parameters:

#!/bin/bash
echo Your container args are: "$@"

When running the container, parameters are automatically passed to the script specified by ENTRYPOINT:

docker run test hello world
# Output: Your container args are: hello world

In-depth Technical Principle Analysis

Docker's CMD and ENTRYPOINT instructions have different semantics and behavior patterns:

CMD Instruction Characteristics:

ENTRYPOINT Instruction Characteristics:

When both ENTRYPOINT and CMD are defined, Docker concatenates and executes them together. This mechanism provides greater flexibility for parameter passing.

Choice of Execution Format

Docker supports two command execution formats: Shell format and Exec format.

Shell Format Example:

CMD /bin/bash file.sh

Exec Format Example:

ENTRYPOINT ["/file.sh"]

The Exec format directly executes commands without going through a shell interpreter, offering better signal handling performance and clearer environment variable inheritance. In parameter passing scenarios, using the Exec format is recommended for more predictable behavior.

Environment Variables as an Alternative Approach

In addition to directly passing command line arguments, environment variables can be used as an alternative for parameter passing. Modify the shell script:

#!/bin/bash
echo $FOO

Pass parameters through environment variables:

docker run -e FOO="hello world!" test

This approach has advantages in configuration management and sensitive information handling, particularly when parameters need to remain consistent across different environments.

Extended Practical Application Scenarios

The C language program scenario mentioned in the reference article demonstrates the application of parameter passing in compiled languages. By modifying program code to accept command line arguments and combining it with Docker's ENTRYPOINT mechanism, flexible runtime configuration can be achieved.

Example C program modification:

UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://%s:4840", argv[1]);

Corresponding Dockerfile configuration:

ENTRYPOINT ["/app/opc_client"]

This pattern enables the same Docker image to adapt to different configuration requirements through parameters in various environments.

Best Practice Recommendations

Based on the above analysis, the following best practices are proposed:

  1. For applications requiring fixed entry points, use ENTRYPOINT with parameter passing
  2. For scenarios requiring completely custom commands, use CMD and allow runtime override
  3. Prioritize using Exec format for better performance and signal handling
  4. Properly handle parameter boundary cases and error inputs in scripts
  5. Consider using environment variables for handling sensitive configuration information

Conclusion

Docker provides multiple flexible parameter passing mechanisms, allowing developers to choose appropriate methods based on specific requirements. By deeply understanding the interaction principles of CMD and ENTRYPOINT, combined with proper script design, flexible and reliable containerized applications can be constructed. In actual projects, it is recommended to select the most suitable parameter passing strategy based on application characteristics, deployment environment, and operational requirements.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.