DevOps
Docker
Commands

Docker Commands

Introduction

Docker provides a powerful CLI (Command Line Interface) for managing containers, images, networks, and volumes. Below is a comprehensive list of the most commonly used Docker commands, categorized by functionality, with a brief explanation for each.

Installation Commands

docker install

Installs Docker on your system. Refer to official Docker installation guides for platform-specific instructions.

Docker Login Commands

docker login

Logs you into Docker Hub or a custom Docker registry to authenticate your session.

Running Containers

docker run <image_name>

Runs a container from a specified image. If the image doesn't exist locally, it pulls it from a registry.

docker run -p <host_port>:<container_port> <image_name>

-p <host_port>:<container_port>: Exposes a port from the container to the host machine.

docker run -e <env_var> <image_name>

-e <env_var>: Sets an environment variable inside the container.

docker run -d <image_name>

-d: Runs the container in detached mode (in the background).

docker run --name <container_name> <image_name>

-name <container_name>: Assigns a name to the container.

docker run -v <host_path>:<container_path> <image_name>

-v <host_path>:<container_path>: Mounts a volume from the host to the container.

docker ps

Lists all running containers.

docker ps -a

Lists all containers, including stopped ones.

-a: Shows all containers (running and stopped).

docker kill <container_id>

Stops a running container immediately.

Container Management Commands

docker create <image_name>

Creates a container from a specified image but doesn't start it.

docker start <container_id>

Starts a stopped container.

docker stop <container_id>

Stops a running container gracefully.

docker kill <container_id>

Immediately stops a running container.

docker restart <container_id>

Restarts a running or stopped container.

docker pause <container_id>

Pauses a running container, suspending its processes.

docker unpause <container_id>

Resumes a paused container.

docker rm <container_id>

Removes a stopped container from the system. -f: Forces the removal of a running container.

Inspecting The Container

docker ps

Lists running containers.

docker ps -a

Lists all containers (running or stopped).

docker top <container_id>

Displays information about the processes running in a container.

docker diff <container_id>

Shows the changes made to a container's filesystem.

docker inspect <container_id>

Provides detailed information about a container, including its configuration and state. --format: Formats the output using a Go template.

Image Management Commands

docker images

Lists all available Docker images on the local machine.

docker rmi <image_id>

Removes an image from the local machine.

docker pull <image_name>

Pulls an image from Docker Hub or a custom registry.

docker tag <image_id> <new_tag>

Tags an image with a new tag.

docker push <image_name>

Pushes an image to Docker Hub or a custom registry.

docker build -t <image_name> <path>

Builds a Docker image from a Dockerfile located at the specified path.

Docker Network Commands

docker network ls

Lists all Docker networks.

docker network create <network_name>

Creates a new Docker network.

docker network connect <network_name> <container_id>

Connects a running container to a network.

docker network disconnect <network_name> <container_id>

Disconnects a container from a network.

Docker Exposing Ports Commands

docker run -p <host_port>:<container_port> <image_name>

-p: Maps a port from the container to the host machine for external access.

  • Example: docker run -p 8080:80 nginx exposes port 80 in the container to port 8080 on the host machine.

Docker Commands for Removing Containers, Images, Volumes, and Networks

docker rm <container_id>

Removes a stopped container from the system. -f: Forces the removal of a running container.

docker rmi <image_id>

Removes an image from the local machine.

docker volume rm <volume_name>

Removes a Docker volume.

docker network rm <network_name>

Removes a Docker network.

Docker File Commands

docker build -t <image_name> <path>

Builds a Docker image from a Dockerfile located at the specified path.

docker exec <container_id> <command>

Runs a command inside a running container.

Docker Volume Commands

docker volume ls

Lists all Docker volumes.

docker volume create <volume_name>

Creates a new Docker volume.

docker volume inspect <volume_name>

Displays information about a specific volume.

docker volume rm <volume_name>

Removes a Docker volume.

Docker Hub Commands

docker login

Logs into Docker Hub or a custom Docker registry.

docker logout

Logs out of Docker Hub or a custom registry.

docker pull <image_name>

Downloads an image from Docker Hub or a custom registry.

docker push <image_name>

Uploads an image to Docker Hub or a custom registry.

General Commands

docker version

Displays Docker version information.

docker info

Displays detailed system-wide information about Docker, including the number of containers, images, and Docker engine version.

Conclusion

The Docker CLI provides a powerful set of commands that allow users to efficiently manage containers, images, networks, and volumes. By mastering these commands and options, you can automate workflows, deploy applications, and optimize containerized environments with ease.