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.
gist.github.com/sawissac/1db5c918f54e65f46eb6a107cae71778Images 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
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 volumesdocker 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
docker build -t my-app:1.0 .-t name:tagnames the image. Leave the tag off and you getlatest.- The
.at the end is the build context: the folder whose files the Dockerfile canCOPY. Everything in it is sent to the engine, so keep a.dockerignorewithnode_modules,.gitand build output in it.
Pass build-time values to an ARG in the Dockerfile with --build-arg:
docker build -t my-app:1.0 --build-arg NODE_VERSION=22 .Run a container
docker run --rm --name web -d -p 8080:80 nginx:alpine| Flag | What it does |
|---|---|
--rm | Delete the container when it stops, so stopped ones don't pile up |
--name web | Give it a name to use instead of the ID |
-d | Detached: run in the background and give the terminal back |
-it | Interactive with a terminal, for shells and REPLs |
-p 8080:80 | Publish container port 80 on host port 8080 |
-e KEY=value | Set an environment variable |
--env-file .env | Load 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:
docker run --rm -it ubuntu:24.04 bashGet inside a running container
docker exec -it web shexec 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.
docker attach webPressing
Ctrl+Cwhile attached sends the signal to the main process, which usually stops the container. Detach withCtrl+PthenCtrl+Qinstead. Most of the time you wantexec, notattach.
Read the logs
docker logs web # everything so far
docker logs -f web # follow, like tail -f
docker logs --tail 100 -f web # last 100 lines, then followlogs 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:
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-alpineBind mounts map a folder on your machine into the container. Use them in development so the container sees your edits live:
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:
-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:
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.0Inside api, the database is at db:5432. No ports need publishing for that; -p is only for reaching a container from the host.
| Driver | Use it for |
|---|---|
bridge | The default. An isolated network for containers on one host |
host | The container shares the host's network stack directly (Linux) |
none | No 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
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 cacheEach prune asks for confirmation; add -f to skip the prompt in scripts.
docker system prune -a --volumesalso removes every image not used by a container and every unused volume. Unused volumes include the database volume of a container you just removed. Checkdocker volume lsbefore you run it.
Dockerfile instructions
| Instruction | What it does |
|---|---|
FROM | The base image to start from, such as node:22-alpine or debian:bookworm-slim |
ARG | A build-time variable, set with --build-arg. Not available when running |
ENV | An environment variable, available at build time and in the running container |
WORKDIR | Set the working directory for the instructions after it. Created if missing |
COPY | Copy files from the build context into the image |
ADD | Like COPY, but also fetches URLs and unpacks local tar archives |
RUN | Run a command at build time, such as installing packages |
EXPOSE | Document which port the app listens on. It doesn't publish it; -p does |
ENTRYPOINT | The executable the container always runs |
CMD | The 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:
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.