T

TechIdea

Ecosystem

Back to Commands
docker COMMANDS

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

bash
docker build -t <image-name> <path>

Example

bash
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

bash
docker run [options] <image-name>

Example

bash
docker run -p 3000:3000 -d my-node-app

Pro 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

bash
docker ps

Example

bash
docker ps

Pro 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

bash
docker stop <container-id>

Example

bash
docker stop a1b2c3d4e5f6

Pro 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

bash
docker rm <container-id>

Example

bash
docker rm a1b2c3d4e5f6

Pro 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

bash
docker-compose up

Example

bash
docker-compose up -d

Pro 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

bash
docker logs <container-id>

Example

bash
docker logs -f a1b2c3d4e5f6

Pro 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.

Growth Newsletter

Get practical AI tools, SEO tips, and growth guides weekly.

Join creators, students, and businesses scaling with TechIdea.