Cheat Sheet - Docker

Image Management

List local images

docker image ls

Build an image

docker build . -t image:tag

Build images locally using unique tagging

# Leveraging PowerShell
docker build . -t image:$(get-date -Format "yyyyMMddHHmmss")

Remove images

# Option 1
docker image rm image:tag

# Option 2 
docker rmi image:tag

# Option 3 
docker rmi <imageid>

# Option 4
docker rmi <only-first-characters-of-image-id>

Inspect image content

# Option 1
docker run -it image:tag sh

# Option 2
docker run -it --entrypoint sh image:tag

# Option 3
docker image history --no-trunc image:tag > image_history

Container Management

List running containers

docker container ls

Run interactively and remove afterward

docker run -it --rm image:tag sh
docker run --interactive --tty --rm image:tag sh

Exposing ports

docker run -it -p <localPort>:<containerPort> image:tag

Stop and remove a running container

# Instead of 
docker container stop foobar
docker container rm foobar 

# Use ...
docker rm foobar --force 

# Or 
docker rmi -f foobar


Bind mount a single file into a container

podman run --rm -ti -v c:\\temp\\fluent-bit.conf:/etc/fluent-bit.conf -c /etc/fluent-bit.conf


Difference between Image ID and digest

Run Postgres locally

docker run -p 5432:5432 -v postgres-11-volume:/var/lib/postgresql/data --env POSTGRES_PASSWORD=postgres --name postgres-11 postgres:11 --restart=always