When we create a container from an image, by default, it attaches the container to the terminal. Many cases, we do not want that to happen for we have other commands to execute and do not bother to open new terminals. >_O
In that case, running a docker container in detached mode comes handy.
The default attached mode:
To create and run a container, we use the following command:
1 | $ docker run eclipse-mosquitto |
The above command creates a container with generated name and attaches it to you terminal. When we press CTRL + C, it actually stops the container. But, we do not want that!
Detached Mode:
For that we have an option to be supplied like this:
1 | $ docker run -d eclipse-mosquitto |
Now it does the same job and instead of attaching the container to the terminal it only logs the container id.
How do we know that our container was created successfully and running?
In short, we do not!
Check/tail the container logs:
To we the container’s logs:
1 | $ docker logs 09bb7465c787fc1ff2f4177a8d0dd1d5c9751b3192583df5c177968dcacb17a2 |
Tip: Use
docker psto list out the running containers, in case you want to know the name of your running containers. It is better to use-aoption to list out all the containers. On error cases, the container might not be running.
Bonus: Attaching the container back:
Just in case if you want to attach a container back again, it is good to know the attach mode.
1 | $ docker attach 09bb7465c787fc1ff2f4177a8d0dd1d5c9751b3192583df5c177968dcacb17a2 |
Remember that pressing
CTRL + Cterminates the container.