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.
- i – To run the container in interactive mode.
- d – Detached mode. The running terminal won’t show because of this.
- 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
If you want to run the container terminal in your parent terminal, then simply skip d –
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.
Kubernetes Series
- Introduction to Kubernetes
- Introduction to Docker, Containers, Images & Repository
- Install and Run Docker Images
- Docker Image – FROM, RUN, COPY, ENTRYPOINT, CMD, EXPOSE explained
- Why docker run or start command not running my container?
- How to list all docker images in system?
- How to list all docker containers?
- How to start/stop a docker container?
- Difference between docker run and docker start
- How to bind docker container port with host?
- How to get logs of docker container?
- How to live stream logs of docker container?
- Set custom name to a docker container
- Access docker container filesystem & terminal
- Getting docker details using docker inspect
- Kyverno – Installation, Policies, Testing, Reporting, Monitoring, Security
- Complete Kubernetes Project Step By Step
- Introduction to Kubernetes Objects