Docker Commands Cheat Sheet

Introduction

Docker is a popular platform for developing, shipping, and running applications in containers. It enables developers to package an application with all its dependencies into a standardized unit for software development. Understanding Docker commands is essential for managing containers, images, networks, and more. This cheat sheet provides a quick reference to the most commonly used Docker commands.

Docker Commands Cheat Sheet

Here’s a handy cheat sheet of the most commonly used Docker commands, ordered by their usage and popularity:

Docker Command Description
docker run <image> Runs a command in a new container.
docker ps Lists running containers.
docker ps -a Lists all containers, including stopped ones.
docker images Lists all Docker images on the local system.
docker pull <image> Pulls an image from a Docker registry.
docker build -t <tag> . Builds an image from a Dockerfile in the current directory.
docker exec -it <container> <command> Runs a command in a running container interactively.
docker stop <container_id> Stops a running container.
docker start <container_id> Starts a stopped container.
docker restart <container_id> Restarts a running or stopped container.
docker rm <container_id> Removes one or more containers.
docker rmi <image_id> Removes one or more images.
docker push <image> Uploads an image to a registry.
docker login Logs in to a Docker registry.
docker logout Logs out from a Docker registry.
docker tag <image> <repository:tag> Tags an image for easier reference.
docker commit <container_id> <new_image> Creates a new image from a container’s changes.
docker inspect <container_id> Displays detailed information on Docker objects.
docker logs <container_id> Fetches the logs of a container.
docker network ls Lists all networks.
docker network create <network_name> Creates a new network.
docker network rm <network_name> Removes one or more networks.
docker volume ls Lists all volumes.
docker volume create <volume_name> Creates a new volume.
docker volume rm <volume_name> Removes one or more volumes.
docker cp <container>:<path> <local_path> Copies files or directories between a container and the local filesystem.
docker top <container_id> Displays the running processes of a container.
docker stats Displays a live stream of container(s) resource usage statistics.
docker save -o <output_file> <image> Saves an image to a tar archive.
docker load -i <input_file> Loads an image from a tar archive.
docker history <image> Shows the history of an image.
docker info Displays system-wide information.
docker system prune Removes all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes.
docker version Shows the Docker version information.
docker help Provides help on Docker commands.

Docker Commands with Examples

Docker Run Command

Runs a command in a new container. This command initializes a container from a specified image, starting any default processes defined within the image.

Syntax:

docker run <image>

Example:

docker run hello-world

This command pulls the hello-world image from the Docker registry (if not already present) and runs it in a new container, displaying a welcome message to verify Docker’s installation.

Docker PS Command

Lists running containers. This command shows the active containers with their status, names, and other relevant details.

Syntax:

docker ps

Example:

docker ps

This command displays a list of all currently running containers, providing information such as container ID, image, command, creation time, status, ports, and names.

Docker PS -A Command

Lists all containers, including stopped ones. This command provides a comprehensive overview of all containers created on the system.

Syntax:

docker ps -a

Example:

docker ps -a

This command shows a list of all containers, both running and stopped, allowing you to see containers that have exited or been stopped.

Docker Images Command

Lists all Docker images on the local system. This command shows the images that are available locally for creating containers.

Syntax:

docker images

Example:

docker images

This command provides a list of all Docker images stored locally, displaying information such as repository name, tag, image ID, creation date, and size.

Docker Pull Command

Pulls an image from a Docker registry. This command downloads the specified image from Docker Hub or another Docker registry.

Syntax:

docker pull <image>

Example:

docker pull ubuntu

This command pulls the ubuntu image from the Docker registry, downloading it to your local system for use in creating containers.

Docker Build Command

Builds an image from a Dockerfile in the current directory. This command constructs an image based on the instructions defined in a Dockerfile.

Syntax:

docker build -t <tag> .

Example:

docker build -t myapp .

This command builds a Docker image named myapp from a Dockerfile located in the current directory.

Docker Exec Command

Runs a command in a running container interactively. This command allows you to execute commands inside an existing container.

Syntax:

docker exec -it <container> <command>

Example:

docker exec -it mycontainer /bin/bash

This command opens an interactive terminal (/bin/bash) inside the running container mycontainer.

Docker Stop Command

Stops a running container. This command sends a SIGTERM signal to gracefully stop the container.

Syntax:

docker stop <container_id>

Example:

docker stop 1a2b3c4d5e6f

This command stops the running container with the ID 1a2b3c4d5e6f.

Docker Start Command

Starts a stopped container. This command reactivates a container that has been previously stopped.

Syntax:

docker start <container_id>

Example:

docker start 1a2b3c4d5e6f

This command starts the stopped container with the ID 1a2b3c4d5e6f.

Docker Restart Command

Restarts a running or stopped container. This command stops and then starts the specified container.

Syntax:

docker restart <container_id>

Example:

docker restart 1a2b3c4d5e6f

This command restarts the container with the ID 1a2b3c4d5e6f.

Docker RM Command

Removes one or more containers. This command deletes the specified container(s) from the system.

Syntax:

docker rm <container_id>

Example:

docker rm 1a2b3c4d5e6f

This command removes the container with the ID 1a2b3c4d5e6f.

Docker RMI Command

Removes one or more images. This command deletes the specified image(s) from the local system.

Syntax:

docker rmi <image_id>

Example:

docker rmi 7d9495d03763

This command removes the image with the ID 7d9495d03763.

Docker Push Command

Uploads an image to a registry. This command pushes a local image to a Docker registry.

Syntax:

docker push <image>

Example:

docker push myrepo/myimage

This command uploads the myrepo/myimage image to the Docker registry.

Docker Login Command

Logs in to a Docker registry. This command authenticates your Docker client with a Docker registry.

Syntax:

docker login

Example:

docker login

This command prompts for your Docker registry credentials, allowing you to log in.

Docker Logout Command

Logs out from a Docker registry. This command removes the authentication token for the Docker registry.

Syntax:

docker logout

Example:

docker logout

This command logs you out from the Docker registry.

Docker Tag Command

Tags an image for easier reference. This command creates a new tag for an existing image.

Syntax:

docker tag <image> <repository:tag>

Example:

docker tag 7d9495d03763 myrepo/myimage:latest

This command tags the image with ID 7d9495d03763 as myrepo/myimage:latest.

Docker Commit Command

Creates a new image from a container’s changes. This command saves the current state of a container as a new image.

Syntax:

docker commit <container_id> <new_image>

Example:

docker commit 1a2b3c4d5e6f mynewimage

This command creates a new image mynewimage from the container with ID 1a2b3c4d5e6f.

Docker Inspect Command

Displays detailed information on Docker objects. This command provides low-level information about a container or image.

Syntax:

docker inspect <container_id>

Example:

docker inspect 1a2b3c4d5e6f

This command shows detailed information about the container with ID 1a2b3c4d5e6f.

Docker Logs Command

Fetches the logs of a container. This command retrieves the standard output and error logs from a container.

Syntax:

docker logs <container_id>

Example:

docker logs 1a2b3c4d5e6f

This command fetches the logs of the container with ID 1a2b3c4d5e6f.

Docker Network LS Command

Lists all networks. This command displays all the networks created on the Docker host.

Syntax:

docker network ls

Example:

docker network ls

This command lists all the Docker networks on your system.

Docker Network Create Command

Creates a new network. This command allows you to create a custom network for your containers.

Syntax:

docker network create <network_name>

Example:

docker network create mynetwork

This command creates a new network named mynetwork.

Docker Network RM Command

Removes one or more networks. This command deletes the specified Docker network(s).

Syntax:

docker network rm <network_name>

Example:

docker network rm mynetwork

This command removes the network named mynetwork.

Docker Volume LS Command

Lists all volumes. This command displays all the volumes created on the Docker host.

Syntax:

docker volume ls

Example:

docker volume ls

This command lists all the Docker volumes on your system.

Docker Volume Create Command

Creates a new volume. This command allows you to create a named volume for your containers.

Syntax:

docker volume create <volume_name>

Example:

docker volume create myvolume

This command creates a new volume named myvolume.

Docker Volume RM Command

Removes one or more volumes. This command deletes the specified Docker volume(s).

Syntax:

docker volume rm <volume_name>

Example:



docker volume rm myvolume

This command removes the volume named myvolume.

Docker CP Command

Copies files or directories between a container and the local filesystem. This command allows you to transfer files to and from a container.

Syntax:

docker cp <container>:<path> <local_path>

Example:

docker cp mycontainer:/path/to/file /local/path

This command copies the file from the container mycontainer to the local directory /local/path.

Docker Top Command

Displays the running processes of a container. This command shows the processes running inside a specified container.

Syntax:

docker top <container_id>

Example:

docker top 1a2b3c4d5e6f

This command displays the running processes inside the container with ID 1a2b3c4d5e6f.

Docker Stats Command

Displays a live stream of container(s) resource usage statistics. This command shows real-time information on CPU, memory, and network usage for your containers.

Syntax:

docker stats

Example:

docker stats

This command provides live resource usage statistics for all running containers.

Docker Save Command

Saves an image to a tar archive. This command exports an image to a tar file, which can be transferred or backed up.

Syntax:

docker save -o <output_file> <image>

Example:

docker save -o myimage.tar myimage

This command saves the myimage Docker image to a tar file named myimage.tar.

Docker Load Command

Loads an image from a tar archive. This command imports an image from a tar file.

Syntax:

docker load -i <input_file>

Example:

docker load -i myimage.tar

This command loads the Docker image from the tar file myimage.tar.

Docker History Command

Shows the history of an image. This command displays the history of the layers in a Docker image.

Syntax:

docker history <image>

Example:

docker history myimage

This command shows the history of the myimage Docker image.

Docker Info Command

Displays system-wide information. This command provides details about the Docker installation, including the number of containers, images, and the system’s configuration.

Syntax:

docker info

Example:

docker info

This command displays detailed information about the Docker environment on your system.

Docker System Prune Command

Removes all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes. This command cleans up your Docker environment by removing unnecessary files.

Syntax:

docker system prune

Example:

docker system prune

This command removes all unused containers, networks, images, and optionally, volumes to free up disk space.

Docker Version Command

Shows the Docker version information. This command provides the version details of the Docker client and server.

Syntax:

docker version

Example:

docker version

This command displays the Docker version information, including the client and server versions.

Docker Help Command

Provides help on Docker commands. This command displays a list of commands or detailed information about a specific command.

Syntax:

docker help

Example:

docker help

This command provides a list of all available Docker commands and their descriptions.

Conclusion

Mastering Docker commands is essential for effectively managing your containers and images. This cheat sheet provides a quick reference to the most commonly used Docker commands, helping you streamline your container workflows and improve your development process. By understanding and using these commands, you can simplify your project builds, enhance your productivity, and ensure your applications are well-structured and maintainable. Keep this guide handy to make the most of Docker. Happy containerizing!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top