Docker Commands Cheat Sheet
The complete Docker cheat sheet. Build containers, manage images, and orchestrate environments with Docker Compose.
docker build
Builds a Docker image from a Dockerfile in the current directory.
Syntax
docker build -t <image-name> <path>Example
docker build -t my-node-app .Pro Tip
Use `-t` to tag the image with a readable name, otherwise you'll have to manage images by their randomly generated ID.
Common Mistake
Forgetting the `.` at the end of the command. The `.` tells Docker to look in the current directory for the Dockerfile.
docker run
Creates and starts a new container from a specific image.
Syntax
docker run [options] <image-name>Example
docker run -p 3000:3000 -d my-node-appPro Tip
Use the `-d` flag to run the container in 'detached' mode (in the background) so it doesn't freeze your terminal.
Common Mistake
Forgetting to map ports using `-p`. Your app might be running inside the container, but you won't be able to access it from your browser.
docker ps
Lists all currently running containers.
Syntax
docker psExample
docker psPro Tip
Use `docker ps -a` to see ALL containers, even the ones that have stopped running.
Common Mistake
Thinking a container was deleted when it was only stopped.
docker stop
Gracefully stops a running container.
Syntax
docker stop <container-id>Example
docker stop a1b2c3d4e5f6Pro Tip
You only need to type the first 3 or 4 characters of the container ID.
Common Mistake
Using `docker kill` instead of `stop`. `stop` gives the application time to save data and shut down properly.
docker rm
Removes a stopped container.
Syntax
docker rm <container-id>Example
docker rm a1b2c3d4e5f6Pro Tip
Use `docker system prune` to easily clean up all stopped containers, unused networks, and dangling images.
Common Mistake
Trying to remove a container that is still running. You must stop it first, or use the `-f` (force) flag.
docker compose up
Builds, (re)creates, starts, and attaches to containers for a service defined in a docker-compose.yml file.
Syntax
docker-compose upExample
docker-compose up -dPro Tip
Use `-d` to run everything in the background. Use `docker-compose down` to cleanly stop and remove the entire stack.
Common Mistake
Running this command in a directory that doesn't have a `docker-compose.yml` file.
docker logs
Fetches the logs of a specific container. Useful for debugging.
Syntax
docker logs <container-id>Example
docker logs -f a1b2c3d4e5f6Pro Tip
Use the `-f` (follow) flag to stream the logs live to your terminal as they happen.
Common Mistake
Piping logs into `grep` instead of using built-in log filters.