Top 10 Docker Commands Every Developer Should Know [Chapter 1]

Manikanta - Nov 5 - - Dev Community

Docker is a powerful tool that allows developers to automate the deployment of applications inside lightweight, portable containers. These containers package the application and its dependencies into a single unit that can run consistently across different environments. To make the most of Docker, you need to be familiar with a set of core commands that help you manage containers, images, and other resources. In this blog, we’ll explore the Top 10 Docker commands you should know to become more proficient with Docker.


1. docker --version
The first step when you're getting started with Docker is verifying its installation. You can use this command to check the installed version of Docker on your machine.

docker --version
Enter fullscreen mode Exit fullscreen mode

2. docker pull
The docker pull command allows you to download Docker images from Docker Hub or other image repositories. This command pulls the specified image (in this case, ubuntu) from the default Docker registry (Docker Hub). You can pull any image, such as a specific version (ubuntu:20.04), or any public image available on Docker Hub.

docker pull ubuntu
Enter fullscreen mode Exit fullscreen mode

3. docker build
The docker build command is used to create a Docker image from a Dockerfile. Here, myimage is the tag you're giving to the image, and . represents the current directory (where your Dockerfile is located). Docker reads the Dockerfile in the directory and builds the image based on the instructions inside it.

docker build -t myimage .
Enter fullscreen mode Exit fullscreen mode

4. docker run
The docker run command is used to create and start a container from a specified image.

  1. -d runs the container in detached mode (in the background).
  2. -p 8080:80 maps port 8080 on your local machine to port 80 inside the container (useful for web apps).
  3. - nginx is the image you're running (in this case, the Nginx web server).
  4. - --name provide name for container
docker run -d -p 8080:80 --name <name-of-container> nginx 
Enter fullscreen mode Exit fullscreen mode

5. docker ps
The docker ps command shows the running containers on your system. This command lists all the containers that are currently running, showing information such as container ID, image name, status, and the ports that are exposed.

docker ps
Enter fullscreen mode Exit fullscreen mode

If you want to see all containers (including stopped ones), you can use

docker ps -a
Enter fullscreen mode Exit fullscreen mode

6. docker stop
The docker stop command stops a running container.You provide the container ID or name (e.g., docker stop mycontainer). This stops the specified container gracefully.

docker stop <container_id>
Enter fullscreen mode Exit fullscreen mode

If you want to forcefully stop it, you can use

docker kill <container_id>
Enter fullscreen mode Exit fullscreen mode

7. docker rm
After stopping a container, you may want to remove it. The docker rm command allows you to delete stopped containers. This removes the container from your system. You can remove multiple containers at once by listing their IDs separated by spaces. If you want to remove a running container, you'll need to stop it first using docker stop.

docker rm <container_id>
Enter fullscreen mode Exit fullscreen mode
docker rm <container_1> <container_2> <container_3>
Enter fullscreen mode Exit fullscreen mode

8. docker images
The docker images command lists all the images stored locally on your system. This shows information about the available images, such as repository, tag, image ID, creation date, and size. This command helps you keep track of the images you have locally.

docker images
Enter fullscreen mode Exit fullscreen mode

9. docker rmi
The docker rmi command is used to remove Docker images. This command removes the specified image from your local system. If an image is being used by a container, you'll need to stop and remove the container before you can delete the image. You can also remove multiple images by providing their IDs in the same command.

docker rmi <image_id>
Enter fullscreen mode Exit fullscreen mode

10. docker exec
The docker exec command is used to run a command inside a running container.This allows you to start an interactive Bash shell inside a running container. The -it flags stand for interactive (-i) and pseudo-TTY (-t), which together allow you to interact with the shell. This is especially useful for debugging or managing containers in real-time.

docker exec -it <container_id> bin/bash
Enter fullscreen mode Exit fullscreen mode

Conclusion
Mastering these 10 essential Docker commands will help you become more comfortable working with Docker containers and images. As you continue to explore Docker, you'll likely come across more advanced commands and features, but understanding the basics is crucial for building a solid foundation.

By using these commands, you can manage images, containers, and other Docker resources, helping you streamline development workflows, automate testing, and deploy applications more efficiently.

See you in Chapter 2

. .
Terabox Video Player