Docker
Docker notes
List
Check images
List all images:
docker imagesCheck containers
List all containers:
docker psClear
Clear unused images
Clear only unused images:
docker image prune -fClear unused containers
Clear only unused containers:
docker container prune -fClear all unused
Clear all unused images, containers, and networks:
docker system prune -fRemove
Remove all
WARNING
This will remove all images, containers, and networks.
docker system prune --all -fRemove all containers
Remove all containers:
docker rm $(docker ps -a -q)Remove all images
Remove all images:
docker rmi $(docker images -q)Remove all volumes
Remove all volumes:
docker volume rm $(docker volume ls -q)Remove all networks
Remove all networks:
docker network rm $(docker network ls -q)Build
Build image
Build an image from a Dockerfile:
docker build -t my-image .Build image with tag
Build an image with a tag:
docker build -t my-image:latest .Run
Run container
Run a container from an image:
docker run my-imageRun container with port
Run a container with a port:
docker run -p 8080:80 my-imageRun container with volume
Run a container with a volume:
docker run -v /path/to/host:/path/to/container my-imageRun container with interactive
Run a container with interactive mode:
docker run -it my-imageRun container with detach
Run a container with detach mode:
docker run -d my-imageRun container with name
Run a container with a name:
docker run --name my-container my-imageRun container with environment
Run a container with an environment variable:
docker run -e MY_ENV=my-value my-imageRun with docker compose
Up
Run with docker compose:
docker compose upUp and build
Run with docker compose and build:
docker compose up --buildUp and detach
Run with docker compose and detach:
docker compose up -dDown
Stop with docker compose:
docker compose downDown and remove
Stop with docker compose and remove:
docker compose down -vLogs
Check logs
Check logs of a container:
docker logs my-containerCheck logs with follow
Check logs of a container with follow:
docker logs -f my-containerExec
Exec command
Execute a command in a running container:
docker exec my-container lsExec interactive
Execute an interactive command in a running container:
docker exec -it my-container bashCopy
Copy from container
Copy a file from a container:
docker cp my-container:/path/to/file /path/to/hostCopy to container
Copy a file to a container:
docker cp /path/to/host my-container:/path/to/container