Commonly Used Docker Commands: A Quick Reference Guide

Docker has become an essential tool for developers, enabling them to create, deploy, and run applications in containers. To get the most out of Docker, it's crucial to understand the basic commands that are commonly used in daily operations. This guide provides a quick reference to some of the most frequently used Docker commands, making it easier for you to manage your Docker environments effectively.
This guide requires basic knowledge of Docker as a prerequisite. If you are not familiar with Docker, read here.
1. Docker Installation and Version Check
Before diving into Docker commands, ensure Docker is installed on your machine. You can check the installation and version with:
docker --version
2. Docker Images
List Docker Images
To see all the Docker images on your system:
docker images
Pull a Docker Image
To download an image from Docker Hub:
docker pull <image_name>
For example:
docker pull nginx
Build a Docker Image
To build an image from a Dockerfile in the current directory:
docker build -t <image_name> .
For example:
docker build -t my-node-app .
Remove a Docker Image
To delete an image from your system:
docker rmi <image_id>
3. Docker Containers
List Docker Containers
To list all running containers:
docker ps
To list all containers, including stopped ones:
docker ps -a
Run a Docker Container
To create and start a container from an image:
docker run -d -p <host_port>:<container_port> <image_name>
For example:
docker run -d -p 80:80 nginx
Stop a Docker Container
To stop a running container:
docker stop <container_id>
Remove a Docker Container
To remove a stopped container:
docker rm <container_id>
4. Docker Container Management
Start a Stopped Container
To start a container that has been stopped:
docker start <container_id>
Restart a Docker Container
To restart a running or stopped container:
docker restart <container_id>
View Container Logs
To see the logs of a container:
docker logs <container_id>
Execute a Command in a Running Container
To run a command inside a running container:
docker exec -it <container_id> <command>
For example, to open a bash shell inside the container:
docker exec -it <container_id> /bin/bash
5. Docker Volumes
Create a Docker Volume
To create a new volume:
docker volume create <volume_name>
List Docker Volumes
To see all volumes on your system:
docker volume ls
Remove a Docker Volume
To delete a volume:
docker volume rm <volume_name>
6. Docker Networks
Create a Docker Network
To create a custom network:
docker network create <network_name>
List Docker Networks
To see all networks:
docker network ls
Connect a Container to a Network
To connect a running container to a network:
docker network connect <network_name> <container_id>
Disconnect a Container from a Network
To disconnect a container from a network:
docker network disconnect <network_name> <container_id>
Conclusion
Familiarity with these commonly used Docker commands will greatly enhance your ability to manage Docker containers, images, volumes, and networks. Whether you're developing applications, setting up CI/CD pipelines, or managing production environments, these commands provide a solid foundation for effective Docker usage.
For a deeper dive into Docker commands and capabilities, refer to the official Docker documentation. Happy Dockering!
References:
- Docker Cheatsheet