Docker Tutorial and/or Cheatsheet

Introduction

Following are the Docker commands that I commonly use. Note that I also prefer using Portainer as a web-based GUI for easy management of containers and images on my host. The downside of Portainer is that it can abstract away the Docker commands which is not good if you’re learning. Therefore, I maintain a balance between use of Portainer and the direct Docker commands shown later in this post. Portainer can be run as a Docker container in the following manner:

docker pull portainer/portainer-ce
docker run --rm -d -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock portainer/portainer-ce

Portainer can now be accessed on the localhost at port 9000: localhost:9000

Docker image commands

Description Command
List available images on host docker images
Search available images on dockerhub docker search <name>
Delete image docker rmi <name>
Build image (folder contains Dockerfile) docker build - < Dockerfile
OR docker build .
Build image (with tag) docker build -t <name>:<tag> <path>

Docker container commands

Description Command
Stop container docker container stop <name>
Delete container docker rm <name>
Delete container docker rm $(docker ps -a -q)
Run container and delete after exit docker run --rm <name>
Run container in interactive mode docker run -i <name>
Run container in background (detach) docker run -d
Execute command in container docker exec -it <name> <command>
Shell in container docker exec -it <name> sh

Docker management commands

Description Command
List running containers docker ps -a
Attach to running container docker attach <name>
Logs for running container docker logs -f <name>
Clean environment docker system prune

Docker networking commands

Description Command
Publish container port on host docker run <name> -p <port>:<port>

Docker mount commands

Description Command
Publish container port on host docker run -it --rm --mount type=bind,source=<hostpath>,target=<containerpath> <name>

Docker compose commands

Before running these commands, ensure that you have the docker-compose.yml file that specifies the different services, containers, and networks.

Description Command
Create and run containers and networks docker compose up
Stop and remove containers, networks, volumes docker compose down -v
Pranshu Bajpai
Pranshu Bajpai
Principal Security Architect

Pranshu Bajpai, PhD, is a principle security architect..