Docker 101: Basic Commands You Need to Know
Are you new to Docker and wondering where to start? Docker 101 is here to help you learn the essential commands you'll need to manage containers effectively. In this blog, we'll break down some of the most important Docker commands and explain how to use them. They are as follows:
docker run
: This command is used to start a new container from an image. It’s one of the most commonly used commands to get your application running.For example in the terminal of Docker Desktop:
docker pull
: Use this command to download Docker images from a registry like Docker Hub. It’s how you get the images you need to create containers.docker run -it <dockerimages_name> bash
By using thebash
command and the-it
options helps us to access directly into the container.For example in the terminal of Docker Desktop:
I downloaded the Docker CentOS image and tried running it, but it exited immediately because no commands were specified. Docker containers need a command to stay active.
I'll run it again using the
bash
command and the-it
options, which will log me directly into the container. Now, I’m inside the Docker container, with the prompt showingroot@
followed by a unique container ID.docker build
: This command lets you create a new Docker image from a Dockerfile. It’s useful for customizing images to fit your application’s needs.docker ps
: This command shows you a list of all currently running containers. It’s helpful for checking the status of your containers.For example in the terminal of Docker Desktop:
The
docker ps
command lists only the currently running containers. At the moment, I don't have any running containers.To address this, I'll start a CentOS container again by using the command
docker run centos
. Previously, I used thebash
command, but this time I'll use thesleep
command. Specifically, I'll instruct the container to sleep for 20 seconds. After the 20 seconds elapse, the container will stop automatically.docker ps -a
: This command lists all containers, including those that are stopped.For example in the terminal of Docker Desktop:
Unlike
docker ps
, which only shows running containers,docker ps -a
provides a complete view of both active and inactive containers, including their status, container IDs, and other relevant details.docker stop
: If you need to stop a running container, this is the command to use. It gracefully shuts down the container.docker rm
: Once a container is stopped, you can remove it from your system with this command. It helps keep your environment clean.For example in the terminal of Docker Desktop:
docker images
:Use this command to list all the Docker images available on your local machine. This includes details such as the repository name, tag, image ID, creation date, and size of each image.For example in the terminal of Docker Desktop:
It's useful for viewing the images you have pulled or built locally, and for managing those images.
docker rmi
: Use this command to remove Docker images from your local machine. By specifying the image ID or name, you can delete one or more images.For example in the terminal of Docker Desktop:
Smol Reminder: You have to delete docker image first then a docker container.!
Stay Tuned!
With these basic commands, you'll be well on your way to managing your Docker containers with ease. Mastering these essential Docker commands will empower you to efficiently handle containerized applications, streamline your development workflow, and maintain a clean and organized environment. Stay tuned for more tips and tricks on getting the most out of Docker!
Subscribe to my newsletter
Read articles from Mehreen Mallick Fiona directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Mehreen Mallick Fiona
Mehreen Mallick Fiona
Hi, I'm Mehreen Mallick Fiona! 👋 I'm a passionate computer science and engineering undergraduate at BRAC University, diving deep into the world of technology and innovation. I'm particularly interested in cloud computing, front-end development, and AI-powered solutions. 🌩️💻 When I'm not coding or studying, I'm here to share my journey, learn from this amazing community, and collaborate on exciting projects. Let's connect and build something great together! 🚀 Feel free to check out my latest projects and articles, and don't hesitate to reach out. I'm always open to new ideas and collaborations!