This post will look at how to keep a Docker container running for testing, debugging, and troubleshooting purposes.
Why Does Docker Container Exit Immediately After Starting?
When you are getting started with Docker, you might face problems with Docker container exiting immediately after starting.
It does not happen if you run an official Nginx container.
However, if you run a base image like Ubuntu, busybox etc, or build a Docker image without a long running process, the container will exit right after running it.
The following screenshot shows both the running Nginx container and exited Ubuntu container.
Here is why the container exit.
- To keep the container running, you need a foreground process added to the Docker Entrypoint. It should be a long running process.
- In the official Nginx image, the Nginx foreground process is part of the Dockerfile Entrypoint . See official Nginx Dockerfile.
- Whereas, in the base Ubuntu image, there is no Entrypoint for the foreground process. That’s why it gets exited right after you execute the docker run command.
Let’s look at how to add an ENTRYPOINT
to Docker that keeps the conatiner image running.
Dockerfile Command to Keep the Container Running
Also, let’s look at another four differnt methods to keep the container running with the docker run command.
Method 1: Interactive Shell Session with pseudo-tty
You can use the -t
(pseudo-tty) docker parameter to keep the container running. Replace tty-container
with required name and ubuntu
with required image.
docker run -it --name tty-container ubuntu /bin/bash
Method 2: Using the tail command
You can run the container directly by passing the tail command via CMD arguments as shown below.
docker run -d ubuntu tail -f /dev/null
Method 3: Using sleep infinity
Another method is to execute a Linux sleep command to infinity. It essentially keeps the container running indefinitely.
docker run -d ubuntu sleep infinity
Once you have the running container, you can attach the container to the terminal session using the exec
parameter shown below.
0ab99d8ab11c
is the container ID
docker exec -it 0ab99d8ab11c /bin/bash
Method 4: Using Keep-Alive command in ENTRYPOINT
You can also use the keep alive command like tail -f /dev/null
the Dockerfile.
Here is a basic Dockerfile with an ENTRYPOINT that will keep on running without getting terminated.
FROM busybox:latest
ENTRYPOINT ["tail", "-f", "/dev/null"]
The tail command follows the /dev/null
, which discards all the data written to it. Due to which the tail command stays active.
Keep Container Running in Kubernetes
Kubernetes pods exits if you the main container running inside the pod doest have long-running process or a command that keeps the container running.
So if the default container images Entrypoint doest have command that keeps running, the container exits immediately and the pod will go the completed state.
For example, if you run a pod with ubuntu or busybox base image, the pod will exit immediately.
Now, if you want to run a pod with image that doesnt have long running process, you will have to add custom command and arguments to the container specification.
Here is a Kubernetes deployment example that uses a plain busybox image where we add a custom command sleep infinity to keep the pod running.
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-deployment
spec:
replicas: 1
selector:
matchLabels:
app: test-app
template:
metadata:
labels:
app: test-app
spec:
containers:
- name: my-container
image: busybox:latest
command: ["sleep", "infinity"]
In summary:
Following are the use cases where you will need a basic running container.
- To test/develop docker images and their configuration
- To troubleshoot systems using utilities inside a container.
- To troubleshoot the Kubernetes cluster with required utilities on a pod.
We hope this article helps! If it doesn’t solve your problem or have a different use case, please drop a comment below.
If you want to learn about optimizing Docker images, check out our guide on reducing docker image size.
1 comment
nice