Skip to main content
Welcome to this comprehensive guide on essential Docker commands. In this article, you’ll learn how to run containers, list and remove them, manage images, and pull images from Docker Hub. If you are new to Docker, check out the official Docker documentation for further reading.

Running Containers with Docker

The Docker run command is used to start a container from a specified image. Below is an example of starting a container:
To explore a variety of available images, visit the Docker Hub website and click on “Explore”. Here, you’ll find many official images such as “hello-world”, CentOS, Ubuntu, and more. For instance, to run a CentOS container, use:
The image shows a Docker Hub webpage listing official repositories like Postgres, Node, and CentOS, with star ratings and pull counts for each.
If you have custom images, use the format “username/repository”. For example, if your Docker Hub ID is “Mumshad” and you have an image named “ansible-playbook”, reference it as “Mumshad/ansible-playbook”. Below is another simulated output showing Docker downloading the image:
You can explicitly download an image using:
The image shows a webpage from Docker Hub, listing official Docker images like nginx, redis, and ubuntu, with their stars and pull counts.
When using a base image like CentOS without specifying a command, the container will exit immediately since there is no process running. To keep the container active, launch it with an interactive shell:
Once inside the container, you can verify the OS by checking the release information:
After verifying, simply exit the container.

Listing Running and Exited Containers

To list currently running containers, execute:
If your interactive CentOS container exited after the session ended, it will not appear in the above list. To view all containers, including those that have exited, use the -a flag:
For example, after starting a CentOS container with a sleep command:
You can run this command in the background using the -d flag:
Now, view the list of running containers:
Since the container will exit after the sleep duration, re-checking with docker ps later will show that it has stopped. To confirm the status of all containers, including exited ones, run:
Containers will display exit codes indicating their termination status (e.g., 0 for normal exit, 137 for forceful termination).

Removing Containers

Cleanup unused containers using the docker rm command. You can remove a container by its specific ID or name. To remove a container with a specific ID:
You can also remove multiple containers by providing parts of their IDs or names:
Below is a sample session demonstrating container removal:

Managing Docker Images

Throughout your Docker sessions, you might have pulled several images such as hello-world, ubuntu, and centos. To list all locally available images, run:
The image shows a terminal window open in mRemoteNG, connected to a Docker host, displaying a command prompt as root in the /root directory.
To remove an image, use the docker rmi command. For example, remove the busybox image with:
Similarly, remove the Ubuntu image with:
If an image is being used by an existing container (even if stopped), Docker will not remove it. In such cases, remove the dependent containers using docker rm -f before attempting to delete the image.
To remove a CentOS image used by a container, first delete all associated containers and then run:
After cleanup, you might only be left with the “centos” and “hello-world” images, which you can remove in a similar manner.

Pulling Docker Images

While the Docker run command pulls missing images automatically, you can download an image without starting a container using the docker pull command. For example, to pull the Ubuntu image, run:
After pulling the image, verify its presence:

Running Containers in Detached Mode and Executing Commands

To run containers in the background, use the -d (detached) flag. For instance, to run an Ubuntu container that sleeps for 100 seconds:
Then, check the container’s status with:
To execute commands on a running container, use docker exec. For example, viewing the OS release information inside the Ubuntu container:

Conclusion

In this guide, we explored basic Docker commands that help you run containers both interactively and in detached mode, list running and stopped containers, remove containers and images, and pull images from Docker Hub. Experiment with these commands to enhance your Docker skills and master container management. For more detailed technical resources, consider these references: Happy Dockering and see you in the next article!

Watch Video

Practice Lab