Docker Commands Cheat Sheet

Images, containers, Compose, volumes & cleanup — with examples.

A free, searchable Docker command guide on one long page. Starts with Image vs. Container — the core idea everything else builds on — then covers writing a Dockerfile (FROM, WORKDIR, COPY, RUN, EXPOSE, CMD) with a quick-reference instruction table, images, running & managing containers, logs & inspecting, Docker Compose, volumes & networking, registry & tagging and cleanup/maintenance. Every command has a copy-ready line and a real worked example with terminal output. Search “run container”, “dockerfile” or “prune” to jump straight to it. Runs fully in your browser, no sign-up, no API.

About Docker Commands Cheat Sheet

Docker Commands Cheat Sheet features

Best practices

Habits that keep Docker fast and tidy

Docker quietly accumulates disk usage and confusion if you skip these.

docker run --rm automatically removes the container the moment it stops — perfect for one-off test runs you don't want cluttering docker ps -a afterward.

Frequently asked questions

What is the difference between a Docker image and a Docker container?

An image is a read-only, packaged template — the blueprint. A container is a running (or stopped) INSTANCE of that image, like a live process. You can start many containers from the same image, each isolated from the others.

How do I write a Dockerfile?

Start with FROM to pick a base image (like FROM node:20), WORKDIR to set the folder to work in, COPY to bring your files in, RUN to install dependencies at build time, EXPOSE to document the port your app uses, and CMD to set the command that runs when the container starts. Then build it with docker build -t my-app .

What is the difference between CMD and RUN in a Dockerfile?

RUN executes a command while the image is being BUILT, and its result (like installed packages) is saved permanently into the image. CMD sets the default command that runs when a container STARTS from that image, and can be overridden at docker run time.

How do I run a Docker container?

Use docker run — for example docker run -d -p 8080:80 --name web nginx runs an nginx container in the background (-d), maps port 8080 on your machine to port 80 inside the container (-p), and names it "web".

How do I see which containers are running?

Run docker ps to see currently running containers, or docker ps -a to also include stopped ones.

How do I stop and remove a Docker container?

Run docker stop <container> to stop it gracefully, then docker rm <container> to delete it. Use docker rm -f <container> to force-remove one that's still running.

How do I view a container's logs?

Run docker logs <container> to see everything it has printed, or docker logs -f <container> to keep watching new log lines live, similar to tail -f.

How do I get a shell inside a running container?

Run docker exec -it <container> bash to open an interactive terminal inside the container — useful for debugging or inspecting files directly.

What does docker compose up do?

It reads docker-compose.yml in the current folder and builds/starts every service it defines — a web app, a database, a cache, all in one command. Add -d to run everything in the background, and --build to force a fresh image rebuild first.

How do I make data survive a container being removed?

Mount a volume with -v — either a folder on your machine (docker run -v ./data:/app/data image) or a Docker-managed named volume (docker volume create db-data, then -v db-data:/var/lib/mysql). Without a volume, any data written inside a container is lost when it's removed.

How do I free up disk space Docker is using?

Run docker system df to see what's taking the space, then docker container prune to remove old stopped containers, docker image prune -a to remove unused images, or docker system prune -a --volumes to clean up everything at once.

How do I push my image to Docker Hub?

First log in with docker login, tag your image with your username (docker tag my-app username/my-app:v1.0), then push it with docker push username/my-app:v1.0.

What does the -p flag mean in docker run?

-p host-port:container-port publishes a port, connecting a port on your machine to a port inside the container. The number on the left is what YOU connect to; the number on the right is what the app inside the container is actually listening on.

How do containers talk to each other?

Put them on the same custom Docker network with docker network create <name>, then run each container with --network <name>. Containers on the same network can reach each other by their container name, like a hostname.

Is this Docker commands cheat sheet free to use?

Yes — it's 100% free with no sign-up, no login and no limits. Every command, example and the search box work fully in your browser.

Related tools — Guides & Tutorials