Dockerfile Overview
A concise reference for common Docker commands with short descriptions.
In general the Dockerfile
or docker-compose.yml
file are stored in the root directory of the project.
List all docker containers on your machine
docker ps -a
List all running docker containers on your machine
docker ps
List all docker volumes
docker volume ls
Remove specific volume
docker volume rm <volume-name>
Remove all unused volumes
docker volume prune
Clear cached build layers and force docker to rebuild every layer (if you get an error while building a docker image)
docker builder prune -f
- This also frees up disk space
Example Dockerfile for the offical Hello World Image
# Use the official lightweight Hello World image
FROM hello-world
Build Images
Build a Docker image from a Dockerfile
docker build -f Dockerfile.app -t my-app .
Run Containers
Run a container in detached mode, restart automatically, and map ports
docker run -d --restart=always --name my-app -p 3000:3000 my-app
Run multiple containers on different ports
docker run -d --restart=always --name app1 -p 3001:3000 app1
docker run -d --restart=always --name app2 -p 3002:3000 app2
Networks
Create a custom network
docker network create my-network
Connect a container to a network
docker network connect my-network my-app
Inspect a container’s IP on the network
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my-app
Container Info
List running containers
docker ps
List all containers including stopped
docker ps -a
Stop Containers
Stop by container name
docker stop my-app
Stop by container ID
docker stop <container-id>
Stop all running containers
docker stop $(docker ps -q)
Remove Containers
Remove a specific container
docker rm my-app
Redeploy Container
Stop container
docker stop my-app
Remove container
docker rm my-app
Build container
docker build -f Dockerfile.app -t my-app .
Run container
docker run -d --name my-app --network my-network -p 3000:3000 my-app
Troubleshoot Docker
If you have errors while building docker images or containers try theses commands
Yes. The commands you already used remove almost everything unused, but here’s a complete breakdown:
- Remove all stopped containers, unused networks, dangling images, and build cache:
docker system prune --volumes --all --force
--all
removes all unused images, not just dangling ones.--volumes
removes unused volumes.--force
skips confirmation.
- Remove all builder cache (used in multi-stage builds):
docker builder prune --all --force
- Optional: Remove unused images individually:
docker images -a # lists all images
docker rmi <image_id>
- Optional: Remove all stopped containers individually:
docker ps -a # lists all containers
docker rm <container_id>
- Optional: Remove all unused volumes:
docker volume ls -qf dangling=true
docker volume rm $(docker volume ls -qf dangling=true)
The two commands you already ran are sufficient to fully clean the environment, including caches that could cause cross-platform module issues.