Why docker run command not running container?

docker run command not running container error

Sometimes, docker run <image> or docker start <container_id> doesn’t start a container. This is because either there is no starting process or the process completes instantly.

Introduction

In Dockerfile of an image, we indicates the ENTRYPOINT and CMD. These instructions tells the container what to run. This could be a shell file, a script or an executable. A container keeps running till this file is running. Or, if we open a terminal in container and keep it open.

Problem

Some docker images are not designed to run anything on their own. For example, operating system images. We are suppose to run our software and programs on an operating system. That’s why docker container exits immediately.

Solution

Solution is simple – Just run a non-ending process into those containers. Command is –

docker run -idt <image>

In case of starting a stopped container, use this –

docker start -idt <container_id>

What this command will do? It will start your container along with a default terminal process. But you can’t see it because we have added more flags here.

  1. i – To run the container in interactive mode.
  2. d – Detached mode. The running terminal won’t show because of this.
  3. t – It will run the default terminal in container.

So, in order to run a node image, you can use this command –

docker run -idt node
docker run -idt node

If you want to run the container terminal in your parent terminal, then simply skip d

docker run -it node
docker run -it node

You can see that node terminal is running in my command prompt. This terminal is actually running in container but attached to my cmd.

Why images are created to exit immediately?

Because they are not designed to run on their own. You do not run a container of node image. Else, you use node to run your javascript application. So, in your Dockerfile, you will mention FROM node. The image will be downloaded but container will run your software. The same thing happens with operating system images.

Conclusion

We can conclude it by saying that a never ending process is needed to keep the container running. In case of our web application, we keep the server process running and that’s why our container doesn’t exit.