DEV Community

Cover image for Docker Cheatsheet
Vishnu Chilamakuru
Vishnu Chilamakuru

Posted on • Edited on • Originally published at vishnuch.tech

Docker Cheatsheet

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

  • Create a container (without starting it):
docker create [IMAGE]
Enter fullscreen mode Exit fullscreen mode
  • Rename an existing container
docker rename [CONTAINER_NAME] [NEW_CONTAINER_NAME]
Enter fullscreen mode Exit fullscreen mode
  • Run a command in a new container
docker run [IMAGE] [COMMAND]
Enter fullscreen mode Exit fullscreen mode
  • Remove container after it exits
docker run --rm [IMAGE]
Enter fullscreen mode Exit fullscreen mode
  • Start a container and keep it running
docker run -td [IMAGE]
Enter fullscreen mode Exit fullscreen mode
  • Start a container and creates an interactive bash shell in the container
docker run -it [IMAGE]
Enter fullscreen mode Exit fullscreen mode
  • Create, Start, and run a command inside the container and remove the container after executing command.
docker run -it-rm [IMAGE]
Enter fullscreen mode Exit fullscreen mode
  • Execute command inside already running container.
docker exec -it [container]
Enter fullscreen mode Exit fullscreen mode
  • Delete a container (if it is not running)
docker rm [CONTAINER]
Enter fullscreen mode Exit fullscreen mode
  • Update the configuration of the container
docker update [CONTAINER]
Enter fullscreen mode Exit fullscreen mode

Starting and Stopping Containers

  • Start Container
docker start [CONTAINER]
Enter fullscreen mode Exit fullscreen mode
  • Stop running Container
docker stop [CONTAINER]
Enter fullscreen mode Exit fullscreen mode
  • Stop running Container and start it again
docker restart [CONTAINER]
Enter fullscreen mode Exit fullscreen mode
  • Pause processes in a running container
docker pause [CONTAINER]
Enter fullscreen mode Exit fullscreen mode
  • Unpause processes in a running container
docker unpause [CONTAINER]
Enter fullscreen mode Exit fullscreen mode
  • Block a container until others stop
docker wait [CONTAINER]
Enter fullscreen mode Exit fullscreen mode
  • Kill a container by sending a SIGKILL to a running container
docker kill [CONTAINER]
Enter fullscreen mode Exit fullscreen mode
  • Attach local standard input, output, and error streams to a running container
docker attach [CONTAINER]
Enter fullscreen mode Exit fullscreen mode

Docker Image Commands

  • Create an image from a Dockerfile
docker build [URL/FILE]
Enter fullscreen mode Exit fullscreen mode
  • Create an image from a Dockerfile with Tags
docker build -t <tag> [URL/FILE]
Enter fullscreen mode Exit fullscreen mode
  • Pull an image from a registry
docker pull [IMAGE]
Enter fullscreen mode Exit fullscreen mode
  • Push an image to a registry
docker push [IMAGE]
Enter fullscreen mode Exit fullscreen mode
  • Create an image from a tarball
docker import [URL/FILE]
Enter fullscreen mode Exit fullscreen mode
  • Create an image from a container
docker commit [CONTAINER] [NEW_IMAGE_NAME]
Enter fullscreen mode Exit fullscreen mode
  • Remove an image
docker rmi [IMAGE]
Enter fullscreen mode Exit fullscreen mode
  • Load an image from a tar archive or stdin
docker load [TAR_FILE/STDIN_FILE]
Enter fullscreen mode Exit fullscreen mode
  • Save an image to a tar archive
docker save [IMAGE] > [TAR_FILE]
Enter fullscreen mode Exit fullscreen mode

Docker Container And Image Information

  • List running containers
docker ps
Enter fullscreen mode Exit fullscreen mode
  • Lists both running containers and ones that have stopped
docker ps -a
Enter fullscreen mode Exit fullscreen mode
  • List the logs from a running container
docker logs [CONTAINER]
Enter fullscreen mode Exit fullscreen mode
  • List low-level information on Docker objects
docker inspect [OBJECT_NAME/ID]
Enter fullscreen mode Exit fullscreen mode
  • List real-time events from a container
docker events [CONTAINER]
Enter fullscreen mode Exit fullscreen mode
  • Show port mapping for a container
docker port [CONTAINER]
Enter fullscreen mode Exit fullscreen mode
  • Show running processes in a container
docker top [CONTAINER]
Enter fullscreen mode Exit fullscreen mode
  • Show live resource usage statistics of container
docker stats [CONTAINER]
Enter fullscreen mode Exit fullscreen mode
  • Show changes to files (or directories) on a filesystem
docker diff [CONTAINER]
Enter fullscreen mode Exit fullscreen mode
  • List all images that are locally stored with the docker engine
docker [image] ls
Enter fullscreen mode Exit fullscreen mode
  • Show the history of an image
docker history [IMAGE]
Enter fullscreen mode Exit fullscreen mode

Network Commands

  • List networks
docker network ls
Enter fullscreen mode Exit fullscreen mode
  • Remove one or more networks
docker network rm [NETWORK]
Enter fullscreen mode Exit fullscreen mode
  • Show information on one or more networks
docker network inspect [NETWORK]
Enter fullscreen mode Exit fullscreen mode
  • Connects a container to a network
docker network connect [NETWORK] [CONTAINER]
Enter fullscreen mode Exit fullscreen mode
  • Disconnect a container from a network
docker network disconnect [NETWORK] [CONTAINER]
Enter fullscreen mode Exit fullscreen mode

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

Top comments (9)

Collapse
 
njokudanielo profile image
NJOKU DANIEL

Thanks

Collapse
 
claudemirfranco profile image
Claudemir F Franco

awesome docker Cheatsheet!!!! Thank you @vishnuchilamakuru

Collapse
 
vishnuchilamakuru profile image
Vishnu Chilamakuru

Thank you

Collapse
 
crazyuploader profile image
Jugal Kishore

Great cheatsheet, thanks!

Collapse
 
aealu profile image
Mazi Chukwudi

Thanks

Collapse
 
magenbrot profile image
Oliver Völker

I'm missing 'docker exec -it [container] ' to execute a command inside the already running container. For example run /bin/bash

Collapse
 
vishnuchilamakuru profile image
Vishnu Chilamakuru

Thanks..added it now :)

Collapse
 
andremoriya profile image
André Moriya

Thanks man. :D

Collapse
 
akakshuki profile image
akakshuki

thanks, that's so useful 😍