Skip to content

Join the Seedly owners community →

Docker

Essential Docker Commands

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

Written by 12 min read1 activity
Pixl, your presenter

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.

Pixl leans on a wooden control panel and pulls one of five brass levers
A handful of commands runs your whole Docker day

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 myapp

Common docker run Flags#

FlagPurposeExample
-dRun in background (detached)docker run -d myapp
-pMap portsdocker run -p 8080:3000 myapp
--nameGive the container a namedocker run --name web myapp
-eSet environment variablesdocker run -e NODE_ENV=production myapp
--rmAutomatically remove when stoppeddocker run --rm myapp
-itInteractive mode with terminaldocker 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 -a

Viewing 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-app

Executing 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 --version

The -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-app

Pulling Images#

Pixl walks into an open shipping container with a lantern to look around inside
docker exec lets you step inside a running container

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

Managing 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 prune

Putting 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 web

Quick Reference#

Screenshot this one. You WILL be back.

CommandWhat It Does
docker build -t name .Build an image from a Dockerfile
docker run -d -p 3000:3000 imageRun a container in the background
docker psList running containers
docker logs containerView container output
docker exec -it container shOpen a shell inside a container
docker stop containerGracefully stop a container
docker rm containerRemove a stopped container
docker pull image:tagDownload an image from a registry
docker imagesList local images

TL;DR#

  • docker build makes images, and docker run makes containers from those images
  • Use -p host:container to map ports and -d to run in the background
  • docker ps shows running containers, and docker logs shows what they printed
  • docker exec -it drops you into a shell inside a running container
  • Clean up stopped containers with docker rm so 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.