Mastering Docker Commands: A Comprehensive Guide | Chandrashekhar Kachawa | Tech Blog

Mastering Docker Commands: A Comprehensive Guide

Docker

Docker has become an indispensable tool in modern software development, enabling developers to build, ship, and run applications in isolated environments called containers. To effectively leverage Docker, understanding its command-line interface (CLI) is crucial. This guide provides a comprehensive overview of the most essential Docker commands, complete with their usage and practical examples.

I. Image Management Commands

Docker images are read-only templates used to create containers. These commands help you manage them.

docker pull

Downloads an image from a registry (like Docker Hub).

Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]

Example: Download the latest Ubuntu image.

docker pull ubuntu:latest

docker images (or docker image ls)

Lists all local Docker images.

Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]

Example: List all images.

docker images

docker build

Builds an image from a Dockerfile.

Usage: docker build [OPTIONS] PATH | URL | -

Example: Build an image from the current directory’s Dockerfile and tag it.

docker build -t my-app:1.0 .

docker rmi

Removes one or more images.

Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]

Example: Remove an image by its ID or name.

docker rmi my-app:1.0

docker push

Pushes an image to a registry.

Usage: docker push [OPTIONS] NAME[:TAG]

Example: Push an image to Docker Hub.

docker push myusername/my-app:1.0

II. Container Management Commands

Containers are runnable instances of Docker images. These commands help you manage their lifecycle.

docker run

Creates and starts a new container from an image.

Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Example: Run an Nginx container, mapping port 8080 on the host to port 80 in the container.

docker run -d -p 8080:80 --name my-nginx nginx

docker ps

Lists running containers.

Usage: docker ps [OPTIONS]

Example: List all running containers.

docker ps

Example: List all containers (including stopped ones).

docker ps -a

docker start

Starts one or more stopped containers.

Usage: docker start [OPTIONS] CONTAINER [CONTAINER...]

Example: Start a container named my-nginx.

docker start my-nginx

docker stop

Stops one or more running containers.

Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...]

Example: Stop a container named my-nginx.

docker stop my-nginx

docker restart

Restarts one or more containers.

Usage: docker restart [OPTIONS] CONTAINER [CONTAINER...]

Example: Restart a container named my-nginx.

docker restart my-nginx

docker rm

Removes one or more containers.

Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]

Example: Remove a stopped container.

docker rm my-nginx

Example: Force remove a running container.

docker rm -f my-nginx

docker exec

Runs a command in a running container.

Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

Example: Execute a bash shell inside my-nginx container.

docker exec -it my-nginx bash

docker logs

Fetches the logs of a container.

Usage: docker logs [OPTIONS] CONTAINER

Example: View logs of my-nginx.

docker logs my-nginx

Example: Follow logs in real-time.

docker logs -f my-nginx

III. Network Management Commands

Docker networks allow containers to communicate with each other and with the outside world.

docker network ls

Lists Docker networks.

Usage: docker network ls [OPTIONS]

Example: List all networks.

docker network ls

docker network create

Creates a new network.

Usage: docker network create [OPTIONS] NAME

Example: Create a custom bridge network.

docker network create my-custom-network

docker network connect

Connects a container to a network.

Usage: docker network connect [OPTIONS] NETWORK CONTAINER

Example: Connect my-nginx to my-custom-network.

docker network connect my-custom-network my-nginx

docker network disconnect

Disconnects a container from a network.

Usage: docker network disconnect [OPTIONS] NETWORK CONTAINER

Example: Disconnect my-nginx from my-custom-network.

docker network disconnect my-custom-network my-nginx

docker network rm

Removes one or more networks.

Usage: docker network rm NETWORK [NETWORK...]

Example: Remove my-custom-network.

docker network rm my-custom-network

IV. Volume Management Commands

Docker volumes are the preferred mechanism for persisting data generated by and used by Docker containers.

docker volume ls

Lists Docker volumes.

Usage: docker volume ls [OPTIONS]

Example: List all volumes.

docker volume ls

docker volume create

Creates a new volume.

Usage: docker volume create [OPTIONS] [NAME]

Example: Create a named volume.

docker volume create my-data

docker volume inspect

Display detailed information on one or more volumes.

Usage: docker volume inspect [OPTIONS] VOLUME [VOLUME...]

Example: Inspect my-data volume.

docker volume inspect my-data

docker volume rm

Removes one or more volumes.

Usage: docker volume rm [OPTIONS] VOLUME [VOLUME...]

Example: Remove my-data volume.

docker volume rm my-data

V. System-Wide Commands

These commands provide information and management capabilities for your Docker daemon.

docker info

Display system-wide information.

Usage: docker info [OPTIONS]

Example: Get Docker system information.

docker info

docker version

Display Docker version information.

Usage: docker version [OPTIONS]

Example: Get Docker version.

docker version

docker system prune

Removes unused Docker data (containers, images, networks, volumes).

Usage: docker system prune [OPTIONS]

Example: Clean up unused Docker objects.

docker system prune -a

Conclusion

Mastering these Docker commands will significantly enhance your productivity and control over your containerized applications. From managing images and containers to configuring networks and persisting data, the Docker CLI provides a powerful interface for all your containerization needs. Keep experimenting and exploring the vast capabilities of Docker!

Latest Posts

Enjoyed this article? Follow me on X for more content and updates!

Follow @Ctrixdev