Docker Cheatsheet

Posted : admin On 1/29/2022
Author
  1. Docker Cheatshet
  2. Docker Cheat Sheet
  3. Docker Build Cheatsheet
  4. Docker Cheat Sheet Pdf 2020
  5. Docker Cheat Sheet Pdf 2018
  • Name
    Austin Miller
    Twitter
    @armiiller
Commands

In this article I will highlight the 6 key docker commands I use on a daily basis while using Docker in the real world.

$ docker image build -file add/Dockerfile -t centos:addtest. $ docker container run centos:addtest ls -lR /app /app: total 4 drwxr-xr-x 2 501 games 4096 Jan 12 13:25 copydir /app/copydir: total 4-rw-r-r- 1 501 games 8 Jan 12 13:19 subfile.txt-rw-r-r- 1 501 games 0 Jan 12 13:25 subfile2.txt # There is no tar file itself and decompression. Docker is an increasingly popular tool designed to make it easier to create, deploy and run applications within a container. We recently published an article – Data Scientist guide for getting started with Docker – which hopefully laid out some of the basics.

By no means is this an extensive list of commands, I kept it short on purpose so you could use it as a quick reference guide. I’ve also omitted the topic of building images and the commands that are associated with that.

At the bottom of the page, I’ll also put some good links to other Docker resources I like or frequently use.

CommandShort Description
docker psList running containers
docker exec -it <container name> /bin/sh SSH into container
docker restart <container name>Restart a container
docker statsShow running container stats
docker system dfCheck docker daemon disk space usage
docker system prune -afRemove images, networks, containers, and volumes

Check Running Containers

Docker
  • Command: docker ps
  • Description: Show running containers.
  • Official Docs: https://docs.docker.com/engine/reference/commandline/ps/

Normally I will use this command just as often as I use ls command on a *NIX terminal. It’s especially useful when you first SSH to a machine to check what’s running. It’s also useful during the development and configuration process since you’ll likely have containers stopping/starting/crashing.

SSH Into Container

  • Command: docker exec -it <container name> /bin/sh
  • Description: SSH into a container.
  • Official Docs: https://docs.docker.com/engine/reference/commandline/exec/

This is probably my 2nd most popular command. Normally I am using this while trying to debug a container and need to shell into the container. Just note the -i flag means interactive and -t means TTY (aka a teletype terminal).

Also, you can use any command instead of /bin/sh; I only put that here because I frequently am SSHing into an alpine image which doesn’t support bash.

Restart A Container

  • Command: docker restart <container name>
  • Description: Restart a container.
  • Official Docs: https://docs.docker.com/engine/reference/commandline/restart/

I debated putting this command in here, since I don’t use it all that often, but it’s a nice to have. Great example of when to use this - you change your prometheus configuration and need it to pick up the changes in your config file. You might also use this when resizing a volume.

Other commands you might use often, but I didn’t think were so worthy of their own section are docker start (see docs) and docker stop (see docs). You’ll use these commands normally when setting up or testing images and you’ll likely use a lot of flags. I didn’t think they were so applicable because you should honestly be using docker compose or some other orchestration system (like Amazon ECS or Kubernetes) to launch your containers.

Check CPU and Memory Usage

  • Command: docker stats
  • Description: Display a live stream of running containers usage statistics.
  • Official Docs: https://docs.docker.com/engine/reference/commandline/stats/

I’m normally using this command when I am trying to figure out optimal soft/hard limits for containers. You might also use this if you are debugging which container is using most of your host’s resources.

Check Disk Space Usage

  • Command: docker system df
  • Description: Display information about disk space being used by your containers.
  • Official Docs: https://docs.docker.com/engine/reference/commandline/system_df/

This one doesn’t come up to often, but it has, especially when you are building lots of images on a box or you are storing lots of data (like prometheus). If you are, you might consider setting up a cron job to prune your images and volumes on a recurring basis.

Prune Images and Volumes

  • Command: docker system prune -af
  • Description: Remove all unused images (dangling and unreferenced), containers, networks, and volumes.
  • Official Docs: https://docs.docker.com/engine/reference/commandline/system_prune/

You’ll probably only use this command on a Docker build machine or on your dev box, nevertheless take note, cause you are likely to use it.

Docker Cheatsheet

Also, just like mentioned above, if this is a build box consider setting up a cron job to prune your images. If you’re a cron syntax noob like me, you might find crontab.guru of use in understanding the syntax and shortcuts for popular time intervals.

Docker Cheatshet

Docker Links You Might Find Useful

Images

Docker Cheat Sheet

  • VPN - Run an IPsec VPN server. Super useful if you work from cafes like Starbucks.
  • Prometheus + Grafana Setup - Out of the box Prometheus and Grafana setup. We actually use a fork of this for monitoring the PagerTree platform.
  • Alpine Image - Slim OS image for Node.js apps in production.
  • GitLab Runner - A GitLab Runner inside a docker container. We use this at PagerTree to build our images.
Docker

Blog Articles

Docker Build Cheatsheet

  • Docker Crash Course - If you’re new to docker this is a great crash course. Starts from installing docker all the way to docker compose. It can be a lot to take in so you might have to read it a couple times.
  • Deployment Automation with GitLab Runner - Super helpful writeup on setting up the GitLab Runner for your own CI/CD pipeline. Used this tutorial extensively when setting up PagerTree’s CI/CD.

Docker Cheat Sheet Pdf 2020

Cheat Sheets

Docker Cheat Sheet Pdf 2018

  • Official Docker Cheat Sheet - Don’t use this one a ton, but you might. Has a lot of the basics and is the official docker.com cheat sheet.
  • Red Hat Developers Compresensive Cheat Sheet - A small 6 page pdf that covers container, image, network, registery, and volume related commands. Also has an overview of the Dockerfile syntax.