Docker Essential Commands Cheat Sheet | Docker Cheat Sheet

Anand Mohan
3 min readAug 30, 2024

--

Docker is a powerful tool for containerizing applications, making them portable, scalable, and easy to manage. Whether you’re a beginner or a seasoned pro, having a quick reference to essential Docker commands can significantly boost your productivity. This article provides a comprehensive cheat sheet of Docker commands, covering everything from basic operations to advanced container management.

Basic Docker Commands

1. Check Docker Version

$ docker — version

Verify your Docker installation and check the version.

2. Run a Container

$ docker run <image>

Launch a container from a specified image.

3. List Running Containers

$ docker ps

View all currently running containers.

4. List All Containers (Including Stopped)

$ docker ps -a

Display all containers, regardless of their state.

5. Start a Stopped Container

$ docker start <container_id>

Restart a previously stopped container.

6. Stop a Running Container

$ docker stop <container_id>

Halt a running container.

7. Remove a Stopped Container

$ docker rm <container_id>

Delete a container that is no longer in use.

8. Remove an Image

$ docker rmi <image_id>

Remove an image from your local Docker registry.

9. Pull an Image from Docker Hub

$ docker pull <image>

Download an image from Docker Hub.

10. Push an Image to Docker Hub

$ docker push <repository>/<image>

Upload an image to your Docker Hub repository.

Working with Images

1. Build an Image from a Dockerfile

$ docker build -t <image_name>:<tag> .

Create an image from a Dockerfile in the current directory.

2. Tag an Image

$ docker tag <image_id> <repository>/<image_name>:<tag>

Add a tag to an image for easier reference.

3. Inspect an Image

$ docker inspect <image_id>

Get detailed information about an image.

4. List All Images

$ docker images

See all images stored in your local Docker registry.

Container Management

1. Rename a Container

$ docker rename <old_name> <new_name>

Change the name of an existing container.

2. View Container Logs

$ docker logs <container_id>

Check the logs generated by a container.

3. Execute a Command in a Running Container

$ docker exec -it <container_id> <command>

Run a command inside an active container.

4. Stop All Running Containers

$ docker stop $(docker ps -q)

Stop every container currently running.

5. Remove All Stopped Containers

$ docker rm $(docker ps -a -q)

Delete all containers that are no longer running.

6. Remove All Unused Images

$ docker image prune

Clean up dangling images not associated with any container.

Volumes and Networks

1. Create a Volume

$ docker volume create <volume_name>

Set up a new Docker volume for data persistence.

2. List Volumes

$ docker volume ls

Display all Docker volumes.

3. Remove a Volume

$ docker volume rm <volume_name>

Delete a specific Docker volume.

4. Create a Network

$ docker network create <network_name>

Establish a new Docker network.

5. List Networks

$ docker network ls

See all Docker networks.

6. Inspect a Network

$ docker network inspect <network_name>

Get details about a Docker network.

7. Remove a Network

$ docker network rm <network_name>

Delete a Docker network.

Docker Compose

1. Start Services Defined in a Docker Compose File

$ docker-compose up

Launch all services specified in a `docker-compose.yml` file.

2. Start Services in Detached Mode

$ docker-compose up -d

Run services in the background.

3. Stop Services

$ docker-compose down

Shut down all services.

4. Build Images Defined in Docker Compose

$ docker-compose build

Build images as defined in a `docker-compose.yml` file.

5. List Running Containers in Docker Compose

$ docker-compose ps

View containers managed by Docker Compose.

Advanced Commands

1. Export a Container to a Tar File

$ docker export <container_id> > <filename>.tar

Save a container’s filesystem as a tar archive.

2. Import a Tar File as an Image

$ docker import <filename>.tar <image_name>:<tag>

Create an image from a tar archive.

3. Copy Files from a Container

$ docker cp <container_id>:/path/to/file /local/path

Transfer files from a container to your local machine.

4. Copy Files to a Container

$ docker cp /local/path <container_id>:/path/to/file

Move files from your local machine to a container.

5. Monitor Real-Time Container Resource Usage

$ docker stats

Track the resource usage (CPU, memory, etc.) of running containers.

Having a Docker command cheat sheet handy can streamline your container management and improve your workflow. Whether you’re building images, managing containers, or configuring networks and volumes, these commands cover a wide range of Docker functionalities. Keep this guide nearby to reference quickly as you work with Docker, and don’t hesitate to explore each command further to fully understand its capabilities.

--

--

Anand Mohan
Anand Mohan

Written by Anand Mohan

Hi ! I am Anand. I am a DevOps Engineer and an Open Source Contributor love to learn and share new things everyday.

No responses yet