Docker Cheat sheet

It seems like everyone has their own Docker Cheatsheet nowadays, and I thought I'd add one more into the mix. I've noted down some handy commands I use most often when I work on Docker-related tasks.

Cheat sheet

Status of docker

A quick and easy way to see an overview of docker on your machine. E.g. how many containers and images have been downloaded and their status.

docker info

Build Docker image

This is only required if we want to create a Docker image from a Dockerfile.

docker build .
docker build -t newImage . // tag the image

Run Docker container

Once we create our own Docker image or pull one from a registry, we'd want to run it on our machine.

docker run alpine:3.9
docker run -d alpine:3.9 // detached mode
docker run -it alpine:3.9 /bin/bash // interactive mode
docker run -v "$(pwd)/src:/app" alpine:3.9 // map volume
docker run -p 8080:8000 alpine:3.9 // map port

Stop and remove all Docker containers

Wipe all containers. This is especially handy after playing around with a bunch of ideas on the development machine.

docker stop $(docker ps -aq) // OR docker stop $(docker ps -a -q)
docker rm $(docker ps -aq)  // OR docker rm $(docker ps -a -q)

docker rm -f $(docker ps -aq) // doing it all in one line with force remove

Remove all Docker images

I personally prefer to have no Docker images on my machine unless they are in use. This is a brutal but easy way to get rid of all the random images that are taking up precious storage space.

docker rmi $(docker images -q)