# Day 20 - ULTIMATE DOCKER CHEAT SHEET

## Introduction :

Docker is a powerful platform that enables developers to automate the deployment, scaling, and management of applications within isolated environments called containers.

These containers bundle an application and all its dependencies, including libraries, configurations, and runtime environments, into a single package.

This approach ensures consistency across different development and production environments, making it easier to develop, test, and deploy software across various systems.

At its core, Docker utilizes containerization technology, which allows you to run applications in isolated, lightweight, and portable containers.

These containers share the host system's operating system kernel, but each container is isolated from others, providing a level of security and resource segregation.

**This blog provides a list of the most common Docker commands cheat sheet.**

## **List of Docker Commands**

### **Docker Container Management Commands**

This section features the essential commands related to the lifecycle of Docker containers. We will learn how to create, manage, and remove containers from your Docker system using the below commands.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691517841453/015fc1ce-bb87-477e-8a10-af1c2d92d6c6.png align="center")

See the containers currently running on the system:

```plaintext
docker ps
```

See all the containers, both running and non-running:

```plaintext
docker ps -a
```

Create a container (without starting it):

```plaintext
docker create [image]
```

Create an interactive container :

```plaintext
docker create -it [image]
```

Rename an existing container:

```plaintext
docker rename [container] [new-name]
```

Delete a container (if it is not running):

```plaintext
docker rm [container]
```

Forcefully remove a container, even if it is running:

```plaintext
docker rm -f [container]
```

View logs for a running container:

```plaintext
docker logs [container]
```

View real-time events for a container:

```plaintext
docker events [container]
```

Update the configuration of one or more containers:

```plaintext
docker update [container]
```

View port mapping for a container:

```plaintext
docker port [container]
```

Show running processes in a container:

```plaintext
docker top [container]
```

View live resource usage statistics for a container:

```plaintext
docker stats [container]
```

Show changes to files or directories on the filesystem:

```plaintext
docker diff [container]
```

Copy a local file to a directory in a container:

```plaintext
docker cp [file-path] CONTAINER:[path]
```

### **Running a Container**

The following commands show you how to start & stop processes in a container and how to manage container execution.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691518054974/e075b49d-c9b9-4175-bdef-e2544f346e1b.png align="center")

Run a command in a container based on an image:

```plaintext
docker run [image] [command]
```

Create, start, and provide a custom name for the container:

```plaintext
docker run --name [container-name] [image]
```

Establish a connection with a container by mapping a host port to a container port:

```plaintext
docker run -p [host-port]:[container-port] [image]
```

Run a container and remove it after it stops:

```plaintext
docker run --rm [image]
```

Run a detached (background) container:

```plaintext
docker run -d [image]
```

Start an interactive process, such as a shell, in a container:

```plaintext
docker run -it [image]
```

Start a container:

```plaintext
docker start [container]
```

Stop a running container:

```plaintext
docker stop [container]
```

Stop a running container and start it up again:

```plaintext
docker restart [container]
```

Pause processes in a running container:

```plaintext
docker pause [container]
```

Resume processes in a running container:

```plaintext
docker unpause [container]
```

Block a container until others stop (after which it prints their exit codes):

```plaintext
docker wait [container]
```

Kill a container by sending a SIGKILL to a running container:

```plaintext
docker kill [container]
```

Attach local standard input, output, and error streams to a running container:

```plaintext
docker attach [container]
```

Run a shell inside a running container:

```plaintext
docker exec -it [container] [shell]
```

### **Docker Image Commands**

Below you will find all the necessary commands for working with Docker images.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691518276971/782ce156-0f15-4a25-bfac-eeb7a6a60c5a.png align="center")

Create an image from a Dockerfile:

```plaintext
docker build [dockerfile-path]
```

Build an image from a Dockerfile located in the current directory:

```plaintext
docker build .
```

Create an image from a Dockerfile and tag it.

```plaintext
docker build -t [name]:[tag] [dockerfile-path]
```

Specify a file to build from:

```plaintext
docker build -f [file-path]
```

Pull an image from a registry:

```plaintext
docker pull [image]
```

Push an image to a registry:

```plaintext
docker push [image]
```

Create an image from a tarball:

```plaintext
docker import [url/file]
```

Create an image from a container:

```plaintext
docker commit [container] [new-image]
```

Tag an image:

```plaintext
docker tag [image] [image]:[tag]
```

Show all locally stored top-level images:

```plaintext
docker images
```

Show history for an image:

```plaintext
docker history [image]
```

Remove an image:

```plaintext
docker rmi [image]
```

Load an image from a tar archive or stdin:

```plaintext
docker load --image [tar-file]
```

Save an image to a tar archive file:

```plaintext
docker save [image] > [tar-file]
```

Remove unused images:

```plaintext
docker image prune
```

### **Networking**

One of the most valuable features of Docker software is the ability to connect containers to each other and to other non-Docker workloads. This section covers network-related commands.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691518440965/c259912a-3886-43c0-8c9d-4152383845de.png align="center")

List available networks:

```plaintext
docker network ls
```

Remove one or more networks:

```plaintext
docker network rm [network]
```

Show information on one or more networks:

```plaintext
docker network inspect [network]
```

Connect a container to a network:

```plaintext
docker network connect [network] [container]
```

Disconnect a container from a network:

```plaintext
docker network disconnect [network] [container]
```

### **Docker General Management Commands**

Once you set up your containers, you will need to know how to get all the important information for managing them.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691518541702/b9d5d639-64ed-4f11-bf64-e7f44c539c6e.png align="center")

Log in to a Docker registry:

```plaintext
docker login
```

Log out of a Docker registry:

```plaintext
docker logout
```

List low-level information on Docker objects:

```plaintext
docker inspect [object]
```

Show the version of the local Docker installation:

```plaintext
docker version
```

Display information about the system:

```plaintext
docker info
```

Remove unused images, containers, and networks:

```plaintext
docker system prune
```

## Conclusion :

This list of commonly used Docker commands should help you become more familiar with them. The cheat sheet will be helpful when you want to create and manage containers, images, and networks.

**I hope you find the blog post helpful! If you do, don't forget to like 👍, comment 💬, and share 📢 it with your friends and colleagues.**
