Docker #

Basic Commands #

# List Docker CLI commands
docker docker container --help

# Display Docker version and info
docker --version
docker version
docker info

docker run hello-world # Execute Docker image

docker image ls # List Docker images

# List Docker containers (running, all, all in quiet mode)
docker container ls
docker container ls --all
docker container ls -aq

docker build -t friendlyhello .  # Create image using this directory's Dockerfile
docker run -p 4000:80 friendlyhello  # Run "friendlyhello" mapping port 4000 to 80
docker run -d -p 4000:80 friendlyhello         # Same thing, but in detached mode
docker exec -it <container_name_or_id> /bin/bash
docker container ls                                # List all running containers
docker container ls -a             # List all containers, even those not running
docker container stop <hash>           # Gracefully stop the specified container
docker container kill <hash>         # Force shutdown of the specified container
docker container rm <hash>        # Remove specified container from this machine
docker container rm $(docker container ls -a -q)         # Remove all containers

docker image ls -a                             # List all images on this machine
docker image rm <image id>            # Remove specified image from this machine
docker image rm $(docker image ls -a -q)   # Remove all images from this machine
docker login             # Log in this CLI session using your Docker credentials
docker tag <image> username/repository:tag  # Tag <image> for upload to registry
docker push username/repository:tag            # Upload tagged image to registry
docker run username/repository:tag                   # Run image from a registry

# GHCR
docker login ghcr.io
docker build . -t ghcr.io/NAMESPACE/IMAGE_NAME
docker push ghcr.io/NAMESPACE/IMAGE_NAME

Docker cleanup commands #

Docker is an incredibly powerful tool for containerizing applications, but it can quickly consume a significant amount of disk space on your machine.

# Cleanup Docker images
docker image prune -a  # remove all unused Docker images
docker image prune     # remove only dangling images (i.e., images that 
                       # aren't associated with any containers)
docker image prune -a --filter "until=720h" # filter images older than 30 days

# Cleanup Docker containers
docker container prune      # remove all stopped containers
docker rm -f $(docker ps -a -q)     # remove all containers, including the running ones

# Cleanup Docker volumes
docker volume prune     # remove all unused volumes,
docker volume prune --filter "dangling=true" # remove dangling volumes

# Docker cleanup everything
docker system prune -a --volumes