Docker container commands – start, stop, detach, and interactive

Start command

>> docker container start CONTAINER-ID

This command is used to start one or more stopped containers

Say we are running a ubuntu container.

>> docker container run ubuntu sleep 30

Note* The above ubuntu container starts and stops after 30 seconds.

If you want to start the above-stopped container then we can use the start command

>> docker container start a4a7536aee20

Once we start the above container it will be Up for 30 seconds and then stop.

Detach mode

Say we are running a ubuntu container.

>> docker container run ubuntu sleep 30

Note* When we run the above command our command window gets freeze for 30 seconds. We cannot perform any other action on that window for 30 seconds.

Now say we don’t have to get blocked instead we have to run the container in the background. We can achieve this using detach command.

>> docker container run -d ubuntu sleep 30

-d means detach ( We can use detach when we want some container to run in background )

Interactive mode

In the above example, the ubuntu container starts, then sleep for 30 seconds, and then stops.

Now instead of moving the container in sleep mode, we have to go or traverse inside the container. We can achieve this using interactive mode

>> docker container run -it ubuntu /bin/bash

i means interactive
t means tty

If we are executing exit command in the container then it stops the container. Now what if we want to come out of the running container but it should not stop . It should be running in the background .

>> Ctrl + p + q

Ctrl + p + q don’t stop the container. Container runs in the background.

Stop Command

>> docker container stop CONTAINER-ID

This command stops one or more running containers

In this article, we have covered docker commands with examples to start and stop the container along with detach and interactive modes. I hope you found this article interesting and valuable. Please share this article with your friends and help me grow. If you are having any concerns or questions about this article please comment below. If you want to get in touch with me please visit the Contact Me page and send me an email.

2 thoughts on “Docker container commands – start, stop, detach, and interactive”

Leave a Comment