Docker Scratchpad


This is a collection of useful Docker commands.

Containers and Images

  • Show all running containers:
    docker ps
  • Shows all running and stopped containers:
    docker ps -a
  • Shows only CONTAINER IDs of all running and stopped containers:
    docker ps -aq 
  • Stop all running containers:
    docker container stop $(docker container ls -aq)
  • Remove all containers:
    docker container rm $(docker container ls -aq) 
  • Show all top level images:
    docker images
  • Remove all images:
    docker rmi $(docker images -aq) 

Dockerfile

  • Build image from Dockerfile in the current directory with tag "sometag":
    docker build -t sometag .
  • Run the container interactively:
    docker run -it sometag

Volumes

  • List volumes:
    docker volume ls
  • Run the latest nginx image and mount the host directory D:\Volume as a volume \Volume:
    docker run -d --name test -v D:/Volume:/Volume nginx:latest
    Run a bash session interactively in the container named "test" created above:
    docker exec -it test /bin/bash
    Get a list of files in the volume:
    ls Volume
    Inspect your container to varify the volume:
    docker inspect test