Tooling · Docker

Docker commands for everyday work

Docker has hundreds of commands and flags, but day to day you use about twenty. This is that twenty: building images, running containers, getting a shell inside one, reading logs, publishing ports, keeping data in volumes, and cleaning up afterwards.

7 min read

  • docker
  • containers
  • cli
  • cheat sheet
gist.github.com/sawissac/1db5c918f54e65f46eb6a107cae71778

Images and containers

Two words come up in every command, so get them straight first:

  • An image is a read-only template: an OS base, your code, its dependencies. You build it once.
  • A container is a running (or stopped) instance of an image. You can start many containers from one image.

Most commands take either a name or an ID. IDs can be shortened to the first few characters, as long as they're unique: docker logs 3f2a works.

Look around

bash
docker ps              # running containers
docker ps -a # running and stopped containers
docker images # images on this machine
docker version # client and engine versions
docker info # engine details: storage driver, CPUs, memory
docker system df # disk used by images, containers and volumes

docker ps is short for docker container ls, and docker images for docker image ls. The long forms follow a docker <object> <verb> pattern that's easier to guess when you forget a command.

Build an image

bash
docker build -t my-app:1.0 .
  • -t name:tag names the image. Leave the tag off and you get latest.
  • The . at the end is the build context: the folder whose files the Dockerfile can COPY. Everything in it is sent to the engine, so keep a .dockerignore with node_modules, .git and build output in it.

Pass build-time values to an ARG in the Dockerfile with --build-arg:

bash
docker build -t my-app:1.0 --build-arg NODE_VERSION=22 .

Run a container

bash
docker run --rm --name web -d -p 8080:80 nginx:alpine
FlagWhat it does
--rmDelete the container when it stops, so stopped ones don't pile up
--name webGive it a name to use instead of the ID
-dDetached: run in the background and give the terminal back
-itInteractive with a terminal, for shells and REPLs
-p 8080:80Publish container port 80 on host port 8080
-e KEY=valueSet an environment variable
--env-file .envLoad environment variables from a file

The port mapping is always host:container. The example above serves nginx at http://localhost:8080.

-p 8080:80 listens on every network interface, so anyone on your Wi-Fi can reach it. For local-only, bind it to loopback: -p 127.0.0.1:8080:80.

For a throwaway shell in a fresh container:

bash
docker run --rm -it ubuntu:24.04 bash

Get inside a running container

bash
docker exec -it web sh

exec starts a new process in the container. Use sh on Alpine-based images, which don't ship bash. Exit the shell and the container keeps running.

attach is different: it connects your terminal to the container's main process.

bash
docker attach web

Pressing Ctrl+C while attached sends the signal to the main process, which usually stops the container. Detach with Ctrl+P then Ctrl+Q instead. Most of the time you want exec, not attach.

Read the logs

bash
docker logs web                # everything so far
docker logs -f web # follow, like tail -f
docker logs --tail 100 -f web # last 100 lines, then follow

logs shows what the main process writes to stdout and stderr. If your app logs to a file inside the container, it won't appear here.

Keep data with volumes

A container's filesystem disappears with the container. To keep data, mount storage from outside it. There are two kinds.

Named volumes are managed by Docker. Use them for databases and anything else the container owns:

bash
docker volume create app-data

docker run -d --name db \
-e POSTGRES_PASSWORD=secret \
--mount type=volume,src=app-data,target=/var/lib/postgresql/data \
postgres:16-alpine

Bind mounts map a folder on your machine into the container. Use them in development so the container sees your edits live:

bash
docker run --rm -it \
-p 3000:3000 \
-w /app \
--mount type=bind,src="$(pwd)",target=/app \
node:22-alpine \
sh -c "npm install && npm run dev"

-w sets the working directory inside the container, and sh -c runs the command string in a shell.

You'll also see the short -v form. It does the same thing; a name on the left means a volume, a path means a bind mount:

bash
-v app-data:/var/lib/postgresql/data   # volume
-v "$(pwd)":/app # bind mount

--mount is longer but fails loudly on a typo in the source path, where -v quietly creates an empty folder.

Let containers talk

Containers on the same user-defined network can reach each other by container name:

bash
docker network create app-net

docker run -d --name db --network app-net -e POSTGRES_PASSWORD=secret postgres:16-alpine
docker run -d --name api --network app-net -p 3000:3000 my-app:1.0

Inside api, the database is at db:5432. No ports need publishing for that; -p is only for reaching a container from the host.

DriverUse it for
bridgeThe default. An isolated network for containers on one host
hostThe container shares the host's network stack directly (Linux)
noneNo networking at all

Once you have more than two containers wired together like this, a Compose file is easier to live with. Local Postgres in one command walks through one.

Clean up

bash
docker stop web           # stop gracefully
docker rm web # remove a stopped container
docker rm -f web # stop and remove in one go
docker rmi my-app:1.0 # remove an image

docker container prune # remove all stopped containers
docker image prune # remove dangling (untagged) images
docker system prune # stopped containers, unused networks, dangling images, build cache

Each prune asks for confirmation; add -f to skip the prompt in scripts.

docker system prune -a --volumes also removes every image not used by a container and every unused volume. Unused volumes include the database volume of a container you just removed. Check docker volume ls before you run it.

Dockerfile instructions

InstructionWhat it does
FROMThe base image to start from, such as node:22-alpine or debian:bookworm-slim
ARGA build-time variable, set with --build-arg. Not available when running
ENVAn environment variable, available at build time and in the running container
WORKDIRSet the working directory for the instructions after it. Created if missing
COPYCopy files from the build context into the image
ADDLike COPY, but also fetches URLs and unpacks local tar archives
RUNRun a command at build time, such as installing packages
EXPOSEDocument which port the app listens on. It doesn't publish it; -p does
ENTRYPOINTThe executable the container always runs
CMDThe default command, or default arguments to ENTRYPOINT. Overridden by docker run arguments

Prefer COPY unless you need what ADD adds. Use WORKDIR rather than RUN cd, because each RUN starts a fresh shell and the cd doesn't carry over.

A small Node app's Dockerfile puts most of these together:

dockerfile
FROM node:22-alpine
WORKDIR /app

COPY package*.json ./
RUN npm ci

COPY . .
ENV NODE_ENV=production
EXPOSE 3000
CMD ["node", "server.js"]

The order matters. Copying package.json and installing dependencies before copying the rest of the code means Docker reuses the cached npm ci layer on every build where only your code changed.


Build with docker build, run with docker run --rm, look inside with exec and logs, and keep anything worth keeping in a volume. That covers most days.