Site logo

docker Cheat sheet

Docker Basics
Show Docker version information
docker version
Display system-wide information about Docker
docker info
Show help for Docker commands
docker --help
Images
List all locally available images
docker images
List all locally available images include intermediate
docker images -a
Download an image from a registry
docker pull <image>
Remove an image
docker rmi <image>
docker rmi $(docker images -q)
docker rmi $(docker images -q)
Build an image from a Dockerfile
docker build -t <tag> <path>
Upload tagged image to registry
docker push username/repository:tag
Containers
List running containers
docker ps
List all containers (including stopped ones)
docker ps -a
Run a container from an image
docker run <image>
Run a container from an image with detached mode
docker run -d <image>
Run a command inside a running container
docker exec -it <container> <command>
Enter a running container
docker exec -it <container> bash
Stop a running container
docker stop <container>
Remove a container
docker rm <container>
Remove all containers from this machine
docker rm $(docker ps -a -q)
Live tail a container's logs
docker logs <container-id> -f
Volumes
List Docker volumes
docker volume ls
Create a Docker volume
docker volume create <name>
Display detailed information about a Docker volume
docker volume inspect <name>
Remove a Docker volume
docker volume rm <name>
Networks
List Docker networks
docker network ls
Create a Docker network
docker network create <name>
Display detailed information about a Docker network
docker network inspect <name>
Remove a Docker network
docker network rm <name>
Docker Inspect
Display detailed information about a container or image
docker inspect <container_or_image>
Format and display specific information about a container or image
docker inspect --format='{{.Key}} {{.Value}}' <container_or_image>
Clean up
Remove all unused containers, networks, images (both dangling and unreferenced), and optionally, volumes. (Docker 17.06.1-ce and superior)
docker system prune
Remove all unused containers, networks, images not just dangling ones (Docker 17.06.1-ce and superior)
docker system prune -a
Remove all unused local volumes
docker volume prune
Remove all unused networks
docker network prune
System
Show docker disk usage
docker system df
Get real time events from the server
docker system events
Display system-wide information
docker system info
Docker Compose
Show Docker Compose version information
docker compose --version
Build and start services defined in a docker-compose.yml file
docker compose up
Build and start services defined in a docker-compose.yml file in detached mode
docker compose up -d
Restart all services
docker compose restart
Stop and remove containers, networks, and volumes defined in a docker-compose.yml file
docker compose down
View output from containers
docker compose logs
Build all image service
docker compose build
Display the running processes
docker compose top
docker-compose.yml
services: web: build: context: ./Path dockerfile: Dockerfile args: APP_HOME: app ports: - "5000:5000" - "5001:5001" environment: APP_ENV: development volumes: - .:/code restart: unless-stopped depends_on: - db entrypoint: /app/start.sh entrypoint: [php, -d, vendor/bin/phpunit] redis: image: redis
Dockerfile Directives
Specify the base image for the new image
FROM <base_image>
Execute a command in the new image
RUN <command>
Execute a command in the new image
RUN <command>
Copy files or directories from the source to the destination in the new image
COPY <source> <destination>
Set the working directory for subsequent instructions
WORKDIR /path/to/directory
Inform Docker that the container will listen on the specified network ports at runtime
EXPOSE <port>
Inform Docker that the container will listen on the specified network ports at runtime
EXPOSE <port>
LABEL
LABEL version="1.0"
Provide defaults for an executing container
CMD ["executable","param1","param2"]
Set environment variables in the new image
ENV <key> <value>
Define a build-time variable with an optional default value
ARG <name>[=<default_value>]
Set the primary command to be run when the container starts
ENTRYPOINT ["executable","param1","param2"]
Copy files or directories from the source to the destination in the new image (supports URLs and unpacking)
ADD <source> <destination>
Dockerfile machine
Dockerfile stack
Dockerfile services