Skip to Content
DockerDockerfile Overview

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

Terminal
docker ps -a

List all running docker containers on your machine

Terminal
docker ps

List all docker volumes

Terminal
docker volume ls

Remove specific volume

Terminal
docker volume rm <volume-name>

Remove all unused volumes

Terminal
docker volume prune

Clear cached build layers and force docker to rebuild every layer (if you get an error while building a docker image)

Terminal
docker builder prune -f
  • This also frees up disk space

Example Dockerfile for the offical Hello World Image

Dockerfile
# Use the official lightweight Hello World image FROM hello-world

Build Images

Build a Docker image from a Dockerfile

Terminal
docker build -f Dockerfile.app -t my-app .

Run Containers

Run a container in detached mode, restart automatically, and map ports

Terminal
docker run -d --restart=always --name my-app -p 3000:3000 my-app

Run multiple containers on different ports

Terminal
docker run -d --restart=always --name app1 -p 3001:3000 app1
Terminal
docker run -d --restart=always --name app2 -p 3002:3000 app2

Networks

Create a custom network

Terminal
docker network create my-network

Connect a container to a network

Terminal
docker network connect my-network my-app

Inspect a container’s IP on the network

Terminal
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my-app

Container Info

List running containers

Terminal
docker ps

List all containers including stopped

Terminal
docker ps -a

Stop Containers

Stop by container name

Terminal
docker stop my-app

Stop by container ID

Terminal
docker stop <container-id>

Stop all running containers

Terminal
docker stop $(docker ps -q)

Remove Containers

Remove a specific container

Terminal
docker rm my-app

Redeploy Container

Stop container

Terminal
docker stop my-app

Remove container

Terminal
docker rm my-app

Build container

Terminal
docker build -f Dockerfile.app -t my-app .

Run container

Terminal
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:

  1. 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.
  1. Remove all builder cache (used in multi-stage builds):
docker builder prune --all --force
  1. Optional: Remove unused images individually:
docker images -a # lists all images docker rmi <image_id>
  1. Optional: Remove all stopped containers individually:
docker ps -a # lists all containers docker rm <container_id>
  1. 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.