Docker Container Persistence: Best Practices for CMD Instruction and Shell Scripts

Nov 30, 2025 · Programming · 9 views · 7.8

Keywords: Docker | Container Lifecycle | Shell Script | CMD Instruction | Persistence

Abstract: This paper provides an in-depth analysis of the interaction mechanism between CMD instructions and shell scripts in Docker containers, examining the root causes of premature container termination. By reconstructing Dockerfile configurations and shell script designs, three effective solutions for maintaining container persistence are proposed: using /bin/bash to maintain interactive sessions, adding infinite loops to keep processes active, and combining sleep commands to sustain container state. With detailed code examples, the article thoroughly explains the implementation principles and applicable scenarios of each solution, helping developers master the core technologies of container lifecycle management.

Analysis of Docker Container Lifecycle Mechanism

The lifecycle of a Docker container is closely tied to its main process (PID 1). When using the CMD instruction to execute a shell script, the container exits immediately after the script completes, which is a fundamental design mechanism of Docker. Understanding this mechanism is crucial for building stable containerized applications.

Root Cause: Container Exit Due to Shell Script Completion

In the original configuration, the Dockerfile uses the CMD /usr/local/bin/shell.sh instruction, and the shell script contains only the echo "Hello-docker" command. The issue with this configuration is that once the echo command finishes executing, the shell process terminates immediately, causing the container's main process to end and triggering container exit.

Solution One: Maintaining Interactive Shell Sessions

By adding the /bin/bash command at the end of the shell script, a persistent interactive session can be created:

#!/bin/bash
echo "Hello-docker" > /usr/hello.txt
/bin/bash

The advantages of this method include:

Solution Two: Using Infinite Loops to Sustain Processes

Another effective approach is to add an infinite loop within the shell script:

#!/bin/bash
echo "Hello-docker"
while true; do
    sleep 1
done

Benefits of this solution:

Solution Three: Optimized Approach Combining Sleep Command

Modify the CMD instruction in the Dockerfile to directly incorporate the sleep command:

CMD /usr/local/bin/shell.sh ; sleep infinity

Characteristics of this method:

Docker Best Practices and Architectural Considerations

According to recommendations from reference articles, Docker container design should adhere to the single responsibility principle. Each container should focus on running a single main process, which helps:

In actual production environments, it is recommended to use Docker Compose for managing multi-container applications to ensure proper collaboration between services.

Code Implementation and Verification

Complete optimized implementation example:

FROM ubuntu:14.04

ADD shell.sh /usr/local/bin/shell.sh
RUN chmod +x /usr/local/bin/shell.sh

CMD ["/usr/local/bin/shell.sh"]

Corresponding shell script:

#!/bin/bash
echo "Container initialization completed"
# Execute necessary configuration tasks
echo "Starting main process..."
/bin/bash

Verify container status using docker run -itd image_name command, and confirm continuous container operation with docker ps.

Conclusion and Recommendations

The key to maintaining Docker container persistence lies in ensuring the main process does not terminate. The three solutions provided in this article each have their advantages, and developers can choose based on specific requirements:

Proper container lifecycle management forms the foundation of reliable Dockerized applications, and understanding these core concepts will significantly improve the success rate of containerized deployments.

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.