How to keep Docker Container Running? [SOLVED]

Written By - Ashwini Kanitkar

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
$ 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 -


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 /de/null:

$ 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

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 -

  1. This helps the developer to troubleshoot the issues
  2. 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 -




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


Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment