🐳 Docker Notes
Quick Reference?
Out of disk space on /var… How can I free the space that docker is using?
This command docker system prune -a
seems to free up the most space.
Tell me where docker images are stored and what version of docker I’m running?
docker info
Show Output
docker info Client: Version: 24.0.7 Context: default Debug Mode: false Server: Containers: 2 Running: 1 Paused: 0 Stopped: 1 Images: 34 Server Version: 24.0.7 Storage Driver: overlay2 Backing Filesystem: extfs Supports d_type: true Using metacopy: false Native Overlay Diff: true userxattr: false Logging Driver: json-file Cgroup Driver: cgroupfs Cgroup Version: 1 Plugins: Volume: local Network: bridge host ipvlan macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog Swarm: inactive Runtimes: io.containerd.runc.v2 runc Default Runtime: runc Init Binary: docker-init containerd version: runc version: init version: Security Options: apparmor seccomp Profile: builtin Kernel Version: 5.15.0-125-generic Operating System: Ubuntu 20.04.6 LTS OSType: linux Architecture: x86_64 CPUs: 16 Total Memory: 31.08GiB Name: MY-HOST-NAME ID: ceb5b9a8-c26c-4f97-ab17-21e5fd64c005 Docker Root Dir: /var/lib/docker Debug Mode: false Experimental: false Insecure Registries: 127.0.0.0/8 Live Restore Enabled: false
Get a list of all the images sorted by largest size with oldest date?
docker images --format "table \t\t:\t" | tail -n +2 | sort -hr -k1,1 -k2,2
Output is something like
1.16GB 2023-03-21 11:36:54 -0400 EDT my-image1:v1.0 6e7479efc0d5
863MB 2022-10-03 11:16:38 -0400 EDT my-repo/my-proj/my-image1:v1.0 6e7479efc0d5
471MB 2023-03-08 08:09:57 -0500 EST jenkins/jenkins:lts d5ed2ceef0ec
13.3kB 2023-05-04 13:37:03 -0400 EDT hello-world:latest 9c7a54a9a43c
The first part sort -k1,1
sorts human readable sizes (-h) in reverse order (-r) so the largest image
size is listed first, the 2nd sort -k2,2
refines the sort to sort by the creation date in oldest to newest
order.
(Q) How can I run a docker image and connect a terminal to it?
It’s best to run a docker image in detached mode and then connect to it via a separate docker command.
docker run -d -p 8080:80 -p 8443:443 --name my_container nginx
docker ps # to get the container ID
NGINX_ID=$(docker ps -q -f name=nginx)
docker exec -it $NGINX_ID bash
(Q) How can I find where all my docker images are stored? I’m out of disks space!
Use the docker volume ls
command to see where
docker volume ls
DRIVER VOLUME NAME
local docker_home
docker volume inspect docker_home
[
{
"CreatedAt": "2023-03-22T13:58:23-04:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/docker_home/_data",
"Name": "docker_home",
"Options": null,
"Scope": "local"
}
]
Docker Cheat Sheet
🔹 Container Management
Command | Description |
---|---|
docker ps |
List running containers |
docker ps -a |
List all containers (including stopped ones) |
docker run -d -p 8080:80 --name my_container nginx |
Run a container in detached mode and expose a port |
docker stop <container_id> |
Stop a running container |
docker start <container_id> |
Start a stopped container |
docker restart <container_id> |
Restart a container |
docker rm <container_id> |
Remove a stopped container |
docker logs -f <container_id> |
View logs of a container |
docker exec -it <container_id> bash |
Access a running container’s shell |
docker ps -a --filter ancestor=<image_id> |
Find all containers using this image |
📦 Image Management
Command | Description |
---|---|
docker images |
List all available images |
docker pull <image_name> |
Pull an image from Docker Hub |
docker rmi <image_id> |
Remove an image |
docker build -t my_image . |
Build an image from a Dockerfile |
docker tag my_image my_repo/my_image:v1 |
Tag an image for a repository |
🔄 Docker Network
Command | Description |
---|---|
docker network ls |
List all networks |
docker network create my_network |
Create a new network |
docker network inspect my_network |
Inspect a network |
docker network connect my_network <container> |
Connect a container to a network |
docker network disconnect my_network <container> |
Disconnect a container from a network |
📂 Volumes & Persistent Storage
Command | Description |
---|---|
docker volume ls |
List all volumes |
docker volume create my_volume |
Create a volume |
docker volume inspect my_volume |
Inspect a volume |
docker run -v my_volume:/data my_image |
Mount a volume to a container |
🏗️ Docker Compose
Command | Description |
---|---|
docker-compose up -d |
Start services in detached mode |
docker-compose down |
Stop and remove services |
docker-compose logs -f |
View logs for all services |
docker-compose ps |
List running services |
docker-compose build |
Build/rebuild services |
🛠️ Docker System Cleanup
Out of space on /var? Where Docker images are stored?
Try one or more of these commands.
Command | Description |
---|---|
docker system prune -a |
Remove unused containers, networks, images (including dangling) |
docker image prune -a |
Remove unused images |
docker container prune |
Remove stopped containers |
docker volume prune |
Remove unused volumes |
This command docker system prune -a
seems to free up the most space.
⚙️ Additional Useful Commands
Command | Description |
---|---|
docker inspect <container_id> |
Show detailed information about a container |
docker stats |
Show live resource usage stats |
docker top <container_id> |
Display running processes in a container |
NOT YET TRIED
Don’t run these until you’ve verified that they work, they might break your docker installation.
Moving docker images location
After docker is installed how can I reconfigure docker to use /big-drive/docker-images to hold docker images?
# Step 1: Stop docker
sudo systemctl stop docker
# Step 2: Move Existing Docker Images (optional)
sudo mv /var/lib/docker /big-drive/docker-images
# Step 3: Configure Docker Daemon to Use New Directory
sudo mkdir -p /etc/docker
cat <<EOF | sudo tee /etc/docker/daemon.json
{
"data-root": "/big-drive/docker-images"
}
EOF
# Step 4: Restart docker
sudo systemctl daemon-reload
sudo systemctl restart docker
# Step 5: Verify the new storage location
docker info | grep "Docker Root Dir"
Docker Root Dir: /big-drive/docker-images