Docker Run Without Stop
Initial Data
Let’s imagine a situation: there is a Docker container that performs some operation and then finishes. Docker stops such a container after the operation is completed.
The Dockerfile would look something like this:
FROM alpine:latest
COPY some-cmd.sh some-cmd.sh
CMD ["/some-cmd.sh"]
Problem
Let’s consider a situation where it is necessary to enter the container after the operation is completed for debugging. How can we prevent Docker from stopping the container after the command running inside it is completed?
Solution
To achieve this, we can modify the Dockerfile slightly to allow debugging. Let’s make the following changes by adding the following at the end:
CMD ["/bin/sh", "-c", "while true; do sleep 86400; done"]
The final Dockerfile will look like this:
FROM alpine:latest
COPY some-cmd.sh some-cmd.sh
CMD ["/some-cmd.sh"]
CMD ["/bin/sh", "-c", "while true; do sleep 86400; done"]
Conclusion
Now we can enter the container, which will not be stopped after the main command is completed.