Docker CLI Commands

Docker CLI Commands

Last Updated: 23 Aug, 24


List container id of all containers

docker ps -a -q

Run a container in detached mode

docker run -d --name container-name image:tag 

The paremeter -d or --detach runs container in the background and prints the container ID

Stream logs of a running container

docker logs -f container-name

Run a container with resource limits and logging

docker run -d --name myapp \
  --cpus 0.5 \
  --memory 512m \
  --memory-reservation 256m \
  --oom-kill-disable \
  --log-driver json-file \
  --log-opt max-size=10m \
  --log-opt max-file=3 \
  image:tag

Runs a container with specific CPU and memory limits, disables OOM killer, and configures JSON file logging with rotation.

Run a container with environment variables

docker run -e VAR1=value1 -e VAR2=value2 image:tag 

Run a container with a .env file

docker run --env-file=.env image:tag 

Remove all the stopped containers

docker container prune

Disk usage of containers

docker ps --size # docker ps -s

Docker container ID by image id or name

docker ps -aqf "ancestor=<image-name-with-tag or image-id>"