Table of Contents
What is meant by keeping docker container in Running State?
Docker is a containerization tool used by most organizations for building, testing, and deploying their applications in different environments. Most of you must have faced the problem of a Docker container exiting immediately after the execution is completed. While we have situation where we would like to connect to container and do some task or debugging for which the container must be in Running state.
Firstly, let us run the official Nginx and Ubuntu docker containers.
$ docker run -d nginx
Unable to find image 'nginx:latest' locally latest: Pulling from library/nginx a603fa5e3b41: Downloading [==================> ] 11.45MB/31.41MB a603fa5e3b41: Pull complete c39e1cda007e: Pull complete 90cfefba34d7: Pull complete a38226fb7aba: Pull complete 62583498bae6: Pull complete 9802a2cfdb8d: Pull complete Digest: sha256:e209ac2f37c70c1e0e9873a5f7231e91dcd83fdf1178d8ed36c2ec09974210ba Status: Downloaded newer image for nginx:latest 7f7346350316929960119bd565f35f9b7cf72859f9da300a04e8aae7b2a3b34d
$ docker run -d ubuntu
Now let us look at the status of the docker containers
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b45792966d1f ubuntu "bash" 35 seconds ago Exited (0) 31 seconds ago interesting_solomon 7f7346350316 nginx "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp gifted_lederberg
You can observe that the Nginx container is running whereas the Ubuntu container exited.
The reason for this is that in order to keep running the docker container, there must be a foreground process added to the Docker Entrypoint. This happens in the case of Nginx whereas the Ubuntu docker doesn't have an entrypoint for the foreground process.
In this article, let us see how to add an Entrypoint that keeps running the docker container.
Setup a Lab Environment
Let us take an example of Ubuntu docker and create an entrypoint to ensure that the docker container keeps running.
FROM ubuntu:latest ENTRYPOINT ["tail", "-f", "/dev/null"]
There are several ways to run the docker container. Please check out the article on running docker containers - https://www.golinuxcloud.com/docker-run-i/
Different methods to keep docker container Running
Method 1 - Build and Run the docker container in terminal mode or tty mode
$ docker build -t ubuntu:test .
Sending build context to Docker daemon 2.048kB Step 1/2 : FROM ubuntu:latest ---> a8780b506fa4 Step 2/2 : ENTRYPOINT ["tail", "-f", "/dev/null"] ---> Running in 0bd9d1f5fa00 Removing intermediate container 0bd9d1f5fa00 ---> 8d21ee677a92 Successfully built 8d21ee677a92 Successfully tagged ubuntu:test
Once the container is successfully built, run the container again using
-t for interactive mode:
$ docker run -d -t ubuntu:test
Now verify the status to make sure the docker container is still in Running state:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4f6a1037bf93 ubuntu:test "tail -f /dev/null" 24 seconds ago Up 12 seconds musing_mendeleev
Method 2 - Add the entrypoint to the docker command directly
Here we are overwriting the default entrypoint from the container to make sure the contain keeps running while trying to read
$ docker run -d ubuntu tail -f /dev/null
Verify the status of the container:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b2995a08921a ubuntu "tail -f /dev/null" 6 seconds ago Up 3 seconds vibrant_chatterjee
Method 3 - Add a infinity sleep command
This is another fix to keep docker container running by adding an infinite sleep while running the container as shown below:
$ docker run -d ubuntu sleep infinity df715c7683c61558dff9a032cfb8d959329bd2ed9e733a139dcd84a82d267151
Verify the container status:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES df715c7683c6 ubuntu "sleep infinity" 19 seconds ago Up 15 seconds trusting_jang
Why do you need a docker container in Running state?
You might need to have the docker container running because of the below reasons -
- This helps the developer to troubleshoot the issues
- This helps the developer to test the docker images
Additionally, docker also provides an option to start the containers automatically. To know more please visit our guide - https://www.golinuxcloud.com/docker-restart-container/
It is important to keep running the docker containers since the developers can troubleshoot the issues. Hope this article helped you understand how to keep running the docker container always. In case of any queries please feel free to reach out to us and we will respond at the earliest.
Docker container will automatically stop after "docker run -d"
Start containers automatically