Mastering Docker Commands: A Complete Hands-On Guide

Tue Oct 31, 2023

Introduction

Docker has revolutionized the way we develop, deploy, and manage applications. It provides a powerful platform for containerization, making it easier to build, package, and distribute applications with all their dependencies.

To harness the full potential of Docker, it's crucial to understand its commands and how they work. In this blog, we'll explore various Docker commands with proper Hands-on to help you understand Docker even better.

Prerequisites

Before diving into this comprehensive guide on Docker commands, it's essential to ensure you have the following prerequisites:

  1. Basic Understanding of Docker: This blog assumes that you have a foundational knowledge of what Docker is and its core concepts.
  2. Docker Server: To practice the commands and examples provided in this blog.

If you're new to Docker, it's recommended to begin with my earlier blog posts:

  1. Docker and Its Architecture: A Deep Dive
  2. How to Install Docker on Ubuntu 22.04 LTS in AWS Cloud

Docker Commands

Docker provides a wide range of commands to manage containers, images, networks, volumes, and more. After Docker is installed, you can access these commands via the Docker CLI (Command Line Interface). 

Here is a list of some of the Docker commands available, we can list them by executing the docker --help command

Common Commands

01. docker info 
The docker info command will provide essential information about your Docker setup, like the number of containers, images, and configuration details.

02. docker version
The docker version command displays the Docker Engine's version information, including client and server versions.

03. docker search
The docker search command allows you to search for Docker images on Docker Hub or other container registries.

04. docker pull
The docker pull command downloads a Docker image from a registry to your local system.

05. docker images
The docker images command lists the Docker images currently stored on your local system.

06. docker run
The docker run command creates and runs a Docker container from a specified image.

07. docker ps
The docker ps command shows a list of running Docker containers.

08. docker exec 
The docker exec command is used to run a command inside a running Docker container.
Syntax: docker exec <name of container/ container-ID> <command>


09. docker login

The docker login command is used to authenticate and log in to a Docker container registry, like Docker Hub, with your credentials, allowing you to push or pull images.
Syntax:  docker login -u <username> 


10. docker push 
The docker push command is used to upload (push) a Docker image to a container registry, making it available for others to download and use.
Syntax: docker push <dockerhub username/image_name:<tag>

11. docker logout
The docker logout command allows you to log out of a Docker container registry, such as Docker Hub, by revoking your authentication credentials.

12. docker build
The docker build command is used to create a Docker image from a Dockerfile, which contains instructions for building an image.

what is a Dockerfile? how to write a Dockerfile we are going to see this in the upcoming blog.

Management Commands

Docker volume
In Docker, a "volume" is a mechanism for persistently storing data that is used by containers. Volumes allow you to share and manage data between containers and the host system. They are an essential component for managing and maintaining data across container lifecycles

01. docker volume create 
This command is used to create a new volume in the Docker

02. docker volume ls
This command is used to list the available volumes in the Docker

03. docker volume inspect
This command is used to show detailed information about a specific volume, including its configuration and mount points.

04. docker volume rm
This command is used to delete the volume

05. docker volume prune
This command is used to remove all unused Docker volumes. It helps to free up disk space by deleting volumes that are not associated with any running or stopped containers.


Docker Network
A Docker network is a communication bridge that allows containers to communicate with each other, with the host system, or with other networks, all while being isolated from one another.

Docker provides a networking model that enables containers to connect to various types of networks and services. Docker networks are an essential component of containerization because they allow containers to function as if they were on their own virtual network, providing isolation and security.

01. docker network create
This command creates a new Docker network with a given name, which containers can use to communicate with each other and the outside world while maintaining network isolation.

02. docker network ls
This command is used to list all the Docker networks that are currently available on your system.

03. docker network inspect
This command is used to obtain detailed information about a Docker network.

04. docker network connect
This command is used to connect an existing container to a specified Docker network.

The "jolly_payne" container initially uses the default "bridge" network. After running the docker network connect command with the "my-bridge-network," this additional network is connected to the container.

05. docker network disconnect
This command is used to disconnect a running container from a specified Docker network.

We disconnected the "jolly_payne" container from the "bridge" network using the docker network disconnect command. Now, the container is only connected to the "my-bridge-network" network.

06. docker network prune
This command removes unused Docker networks, helping you free up resources by deleting networks that are no longer associated with any running containers or services.

07. docker network rm
This command is used to permanently delete a specific Docker network, which disconnects all containers attached to that network and removes the network from your Docker system.

Swarm Commands

Docker Swarm is a container orchestration tool that helps manage and scale Docker containers in a cluster. It provides load balancing, service discovery, and high availability for containerized applications. It uses manager and worker nodes to distribute and run containers, making it easier to deploy and maintain applications in a clustered environment.

I am planning to create a dedicated blog on the topic of Docker Swarm.

Commands

The below commands are not standalone commands they have to be used along with other docker sub-commands like docker image, docker volume, and docker Network .... etc.

Example:

docker volume rm <volume-name>
docker container start <container-id>
docker container stop <container-id>
docker network ls
docker top <contaioner-id

Conclusion

In this blog, we've covered many Docker commands through hands-on examples. We'll explore the remaining commands in upcoming blog posts.

I hope you enjoyed reading this blog and found it informative. If you have any questions or topics you'd like us to cover in future blogs, please don't hesitate to connect with me on LinkedIn. 

Thank you for joining us on this Docker journey.

Sampath Siva Kumar Boddeti
AWS & Terraform Certified

Launch your GraphyLaunch your Graphy
100K+ creators trust Graphy to teach online
𝕏
DevOpsHub 2024 Privacy policy Terms of use Contact us Refund policy