In this tutorial, we show how to stop, start or run a docker container. Also we show how to remove an existing container. We also talk about what is the difference between docker run and docker start.
If you don’t know what is docker container, we recommend you have a look at the essential docker tutorial to understand the docker concepts and the differences between docker containers and docker images.
So for start, you can run a docker container by simply running:
docker run docker-image-id
docker-image-id is the name of the docker image which gets pulled from the docker hub.
As an example, if you run: “docker run hello-world“, it downloads the hello-world docker image and creates a docker container of that image and runs the entry point program inside the docker container.
Every time you run the “docker run hello-world”, it creates a new docker container and runs the initial program.
In below we ran the command “docker run hello-world” two times and you can see the outputs of the “docker ps –all” command:
docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1e514798dff2 hello-world "/hello" 4 minutes ago Exited (0) 4 minutes ago compassionate_dewdney
46bba1f9d4b8 hello-world "/hello" 34 minutes ago Exited (0) 34 minutes ago priceless_faraday
As you see, two docker containers has created , run and exited.
However, if you want to run an existing docker container without creating a new one from the image, you can run command: “docker start conrainer-ID -a“. You can find container-id from “docker ps –all” command and “-a” means wait for output or stdout.
docker start 1e514798dff2 -a
You can stop a running docker container by simply running “docker stop container-id“, also you can remove or delete a docker container by running “docker rm container-id” and in case if removing failed because of some dependencies you can remove by force by “docker rm –force container-id”
docker start container-id -a
docker stop container-id
docker rm container-id
# remove by force
docker rm --force container-id
Also please note that you can assign a name for a docker container so it is easier to start or stop it without looking for container-id every time by using “–name“. (two dashes)
For example, you can have “docker run –name mycontainer hello-world“, which creates a docker container with the “mycontainer” name. Then you can start it by simply running: “docker start mycontainer -a“