Docker Cheatsheet / Notes

Abstract
Solid development and test environments using containers.

Docker Commands

docker ps shows list of currently running containers

docker ps -a shows list of currently running and previously ran containers

docker run <image> starts container in attached mode

docker run -d <image> starts container in detached mode, system prompt returns

docker run -it <image> starts container in interactive mode, connects container shell

docker exec <con_id> <command> executes command in a running container

docker run <image> <command> runs the command in the container

docker stop <con_id> stops container with given id

docker stop <name> stops container with given name

docker rm <con_id> removes container with given id, multiple separated by space

docker rm <name> removes container with given name, multiple separated by space

docker rmi <img_id> removes image with given id, multiple separated by space

docker rmi <image> removes image with given name, multiple separated by space

docker pull <image> downloads image from Docker Hub to local storage

docker run <img>:<tag> downloads image with specified tag

Docker Run in Details

Interactive mode

docker run -it interactive mode with psuedo terminal

port mapping

docker run -p <host_port>:<con_port> maps host port to container port

volume mapping, data persistence

docker run -v <host_vol>:<con_vol> maps host storage to container storage

e.g. docker run -it /home/neeraj/Sources:/home/ alpine:3.17

Supplementary

docker logs <con_id> view the logs

docker attach <con_id> attaches to given running container

docker inspect <con_id> displays information about running container, e.g. ip address

Docker Images and Containerization

How to create the image?

start with the OS

FROM python:3.11.0-alpine

Docker Environment Variables

docker run -e APP_VARIANT=12

Docker Compose

Running multi-container app