Essential Docker Commands
Learn the core Docker commands for running, managing, and debugging containers

Pixl presents
Build, run, logs, exec, stop. Memorize these and stop googling the same Docker command every single morning.Build, run, logs, exec, stop. Memorize these and stop googling the same Docker command every single morning.

Okay, enough theory. Time to get your hands dirty. This lesson walks through the commands you'll reach for over and over when you're working with Docker, and by the end you'll be building, running, inspecting and cleaning up containers without breaking a sweat.
Building an Image#
You can't run a container without an image first. The docker build command makes an image from a Dockerfile (you'll write your own next lesson).
# Build an image from the current directory and tag it as "myapp"
docker build -t myapp .
# Build with a specific tag/version
docker build -t myapp:v1.0 .The -t flag gives your image a name (and a tag if you want one). That lil . at the end tells Docker to use the folder you're sitting in right now as the build context, which is also where it looks for the Dockerfile unless you say otherwise.
Running a Container#
The docker run command creates a brand new container from an image and starts it up.
# Basic run
docker run myapp
# Run with port mapping (host:container)
docker run -p 3000:3000 myapp
# Run in the background (detached mode)
docker run -d -p 3000:3000 myapp
# Run with a custom name
docker run -d --name my-web-app -p 3000:3000 myappCommon docker run Flags#
| Flag | Purpose | Example |
|---|---|---|
-d | Run in background (detached) | docker run -d myapp |
-p | Map ports | docker run -p 8080:3000 myapp |
--name | Give the container a name | docker run --name web myapp |
-e | Set environment variables | docker run -e NODE_ENV=production myapp |
--rm | Automatically remove when stopped | docker run --rm myapp |
-it | Interactive mode with terminal | docker run -it node:24-alpine sh |
Listing Containers#
Want to know what's running? That's docker ps.
# Show running containers
docker ps
# Example output:
# CONTAINER ID IMAGE STATUS PORTS NAMES
# a1b2c3d4e5f6 myapp Up 2 minutes 0.0.0.0:3000->3000/tcp my-web-app
# Show ALL containers (including stopped ones)
docker ps -aViewing Logs#
When something breaks (or you're just nosy about what your app is up to), check the logs.
# View logs for a container
docker logs my-web-app
# Follow logs in real-time (like tail -f)
docker logs -f my-web-app
# Show only the last 50 lines
docker logs --tail 50 my-web-app
# Show logs with timestamps
docker logs -t my-web-appExecuting Commands Inside a Container#
Every now and then you need to climb inside a running container to poke at files, check a config or chase down a bug.
# Open a shell inside a running container
docker exec -it my-web-app sh
# Run a single command inside the container
docker exec my-web-app ls /app
# Check Node.js version inside the container
docker exec my-web-app node --versionThe -it flags give you an interactive terminal, so you can type commands like you're standing right inside the container.
Stopping and Removing Containers#
Done with a container? Here's how you shut it down and clear it out.
# Stop a running container (graceful shutdown)
docker stop my-web-app
# Force stop (immediate kill)
docker kill my-web-app
# Remove a stopped container
docker rm my-web-app
# Stop and remove in one step
docker rm -f my-web-appPulling Images#

You can download images from Docker Hub without running them.
# Pull the latest nginx image
docker pull nginx:latest
# Pull a specific version
docker pull node:24-alpine
# Pull from a different registry
docker pull ghcr.io/owner/image:tagManaging Images#
# List all images on your machine
docker images
# Remove an image
docker rmi node:24-alpine
# Remove all unused images (free up disk space)
docker image prunePutting It All Together#
Here's what a normal day of developing with Docker usually looks like, start to finish.
# 1. Build your image
docker build -t myapp .
# 2. Run it with port mapping
docker run -d --name web -p 3000:3000 myapp
# 3. Check it's running
docker ps
# 4. View the logs
docker logs web
# 5. Debug if needed
docker exec -it web sh
# 6. Stop when done
docker stop web
# 7. Clean up
docker rm webQuick Reference#
Screenshot this one. You WILL be back.
| Command | What It Does |
|---|---|
docker build -t name . | Build an image from a Dockerfile |
docker run -d -p 3000:3000 image | Run a container in the background |
docker ps | List running containers |
docker logs container | View container output |
docker exec -it container sh | Open a shell inside a container |
docker stop container | Gracefully stop a container |
docker rm container | Remove a stopped container |
docker pull image:tag | Download an image from a registry |
docker images | List local images |
TL;DR#
docker buildmakes images, anddocker runmakes containers from those images- Use
-p host:containerto map ports and-dto run in the background docker psshows running containers, anddocker logsshows what they printeddocker exec -itdrops you into a shell inside a running container- Clean up stopped containers with
docker rmso they quit eating disk space - The usual flow goes build, run, inspect, stop, remove
What's Next?#
You've been building images with docker build this whole time, but where do the build instructions actually come from? Next lesson you'll write your own Dockerfile, the recipe that tells Docker exactly how to build your app's image...
This lesson ends with a short activity.
