Docker Cheatsheet
Tags: docker, devops, productivity, discuss
Published: 2021-06-26 08:49:17

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production. In this post, I will mention docker commands which we need or most of the use-cases.


* [Lifecycle Commands](#lifecycle-commands)
* [Starting and Stopping Containers](#starting-and-stopping-containers)
* [Docker Image Commands](#docker-image-commands)
* [Docker Container And Image Information](#docker-container-and-image-information)
* [Network Commands](#network-commands)

## Lifecycle Commands

- Create a container (without starting it):
```docker
docker create [IMAGE]
```

- Rename an existing container
```docker
docker rename [CONTAINER_NAME] [NEW_CONTAINER_NAME]
```

- Run a command in a new container
```docker
docker run [IMAGE] [COMMAND]
```

- Remove container after it exits
```docker
docker run --rm [IMAGE]
```

- Start a container and keep it running
```docker
docker run -td [IMAGE]
```

- Start a container and creates an interactive bash shell in the container
```docker
docker run -it [IMAGE]
```

- Create, Start, and run a command inside the container and remove the container after executing command.
```docker
docker run -it-rm [IMAGE]
```
- Execute command inside already running container.
```docker
docker exec -it [container]
```

- Delete a container (if it is not running)
```docker
docker rm [CONTAINER]
```

- Update the configuration of the container
```docker
docker update [CONTAINER]
```

## Starting and Stopping Containers

- Start Container
```docker
docker start [CONTAINER]
```

- Stop running Container
```docker
docker stop [CONTAINER]
```

- Stop running Container and start it again
```docker
docker restart [CONTAINER]
```

- Pause processes in a running container
```docker
docker pause [CONTAINER]
```

- Unpause processes in a running container
```docker
docker unpause [CONTAINER]
```

- Block a container until others stop 
```docker
docker wait [CONTAINER]
```

- Kill a container by sending a SIGKILL to a running container
```docker
docker kill [CONTAINER]
```

- Attach local standard input, output, and error streams to a running container
```docker
docker attach [CONTAINER]
```

## Docker Image Commands

- Create an image from a Dockerfile
```docker
docker build [URL/FILE]
```

- Create an image from a Dockerfile with Tags
```docker
docker build -t <tag> [URL/FILE]
```

- Pull an image from a registry
```docker
docker pull [IMAGE]
```

- Push an image to a registry
```docker
docker push [IMAGE]
```

- Create an image from a tarball
```docker
docker import [URL/FILE]
```

- Create an image from a container
```docker
docker commit [CONTAINER] [NEW_IMAGE_NAME]
```

- Remove an image
```docker
docker rmi [IMAGE]
```

- Load an image from a tar archive or stdin
```docker
docker load [TAR_FILE/STDIN_FILE]
```

- Save an image to a tar archive
```docker
docker save [IMAGE] > [TAR_FILE]
```

## Docker Container And Image Information

- List running containers
```docker
docker ps
```

- Lists both running containers and ones that have stopped
```docker
docker ps -a
```

- List the logs from a running container
```docker
docker logs [CONTAINER]
```

- List low-level information on Docker objects
```docker
docker inspect [OBJECT_NAME/ID]
```

- List real-time events from a container
```docker
docker events [CONTAINER]
```

- Show port mapping for a container
```docker
docker port [CONTAINER]
```

- Show running processes in a container
```docker
docker top [CONTAINER]
```

- Show live resource usage statistics of container
```docker
docker stats [CONTAINER]
```

- Show changes to files (or directories) on a filesystem
```docker
docker diff [CONTAINER]
```

- List all images that are locally stored with the docker engine
```docker
docker [image] ls
```

- Show the history of an image
```docker
docker history [IMAGE]
```

## Network Commands

- List networks
```docker
docker network ls
```

- Remove one or more networks
```docker
docker network rm [NETWORK]
```

- Show information on one or more networks
```docker
docker network inspect [NETWORK]
```

- Connects a container to a network
```docker
docker network connect [NETWORK] [CONTAINER]
```

- Disconnect a container from a network
```docker
docker network disconnect [NETWORK] [CONTAINER]
```

<hr>

## Thank you for reading
Hope you find these resources useful. If you like what you read and want to see more about system design, microservices, and other technology-related stuff... You can follow me on 

- Twitter - <a href="https://twitter.com/vishnuchi?ref_src=twsrc%5Etfw" class="twitter-follow-button" data-size="large" data-show-count="false">Follow @vishnuchi</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
- Subscribe to my weekly newsletter [here](https://www.getrevue.co/profile/vishnuch).