Docker explained: images, containers and volumes without the jargon
5 min readDocker · Infrastructure · Guides
Docker is mentioned in half the self-hosting posts on this site, and I have never stopped to explain it. People hear the words image, container, layer and volume and assume they need a computer science degree. They do not. Underneath the vocabulary there are three simple ideas, and once they click, most Docker errors become easy to read.
Here is the short version. An image is a read-only package containing an app and everything it needs. A container is a running instance of an image. A volume is storage that lives outside the container, so your data survives when the container is deleted. The rest of this post is those three ideas with real commands.
The problem Docker solves
You build an app on your laptop. It needs a particular Node version, a few system libraries and some environment settings. You copy it to a server and it breaks, because the server has a different version of something. Everyone has lived this, and the classic answer is that it works on my machine.
Docker's answer is to ship the machine's relevant parts along with the app. Instead of handing over code and a list of instructions, you hand over an image that already contains the runtime, the libraries and the code. It runs the same way on your laptop, on a colleague's laptop and on a VPS, as long as Docker is there.
Images: the recipe, frozen
An image is built from a text file called a Dockerfile. Each instruction adds a layer, and layers are cached, which is why a second build is often much faster than the first. You usually start from a base image that someone else maintains, such as an official Node or Python image, and add your own files on top.
FROM node:22-slim
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]Read it top to bottom. Start from a slim Node image, set a working folder, copy the dependency files and install them, then copy the rest of the code, note the port and define the start command. Copying package*.json first is not an accident: Docker reuses the cached install layer when only your source code changed, so rebuilds stay fast.
Images are identified by a name and a tag, such as node:22-slim. A tag like latest is just a label that can point at different content over time, so for anything you care about, pin a specific version.
Containers: the image, running
When you run an image, Docker creates a container. It is an isolated process with its own filesystem view, its own network and its own process list. It is not a full virtual machine with its own kernel. Containers share the host's kernel, which is why they start in a second and use far less memory than a VM.
One image can start many containers. Think of the image as a class and each container as an object created from it. The important consequence is that a container's own writable layer is temporary. Delete the container and anything written inside it is gone.
docker build -t myapp:1.0 .
docker run -d --name myapp -p 8080:3000 myapp:1.0
docker ps
docker logs -f myapp
docker stop myapp && docker rm myappThe -p 8080:3000 flag maps port 8080 on your machine to port 3000 inside the container. Without it the app runs but nothing outside can reach it. This one line explains a huge share of the my container is running but I cannot open it questions.
Volumes: where data survives
Because container storage is disposable, anything you want to keep needs to live somewhere else. That is what a volume is: a folder managed by Docker, outside the container's own layer, that you attach to a path inside the container. Databases are the classic case. Run Postgres without a volume and your data disappears with the container.
docker volume create pgdata
docker run -d --name db \
-e POSTGRES_PASSWORD=change-me \
-v pgdata:/var/lib/postgresql/data \
postgres:16Now you can remove and recreate the db container, upgrade the image, even change settings, and the data in pgdata stays put. There is a second kind of mount called a bind mount, where you map a specific host folder, such as your project directory, into the container. It is great for development because edits show up instantly, and less great in production because it ties the container to one machine's folder layout and permissions.
Docker Compose: many containers, one file
Real applications are rarely one container. You have an app, a database and perhaps a cache. Typing a long docker run for each is miserable. Docker Compose lets you describe the whole stack in a single YAML file and start it with one command.
services:
app:
build: .
ports:
- "8080:3000"
environment:
DATABASE_URL: postgres://postgres:change-me@db:5432/app
depends_on:
- db
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: change-me
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:Notice that the app reaches the database at the hostname db. Compose creates a private network where each service can be found by its name. That is also why localhost inside a container means the container itself, not your computer, which trips up almost everyone once.
The commands I actually use
docker compose up -danddocker compose downto start and stop a stack.docker logs -f nameto watch what a container is printing.docker exec -it name shto open a shell inside a running container.docker ps -ato see every container, including stopped ones.docker system dfto see how much disk Docker is using.
Where it bites
- Disk fills up - old images, stopped containers and build cache pile up. Run
docker system prunecarefully, and read what it will delete first. - Data loss - no volume means no persistence. Check the path a database actually stores data in before you trust it.
- Secrets in images - anything you
COPYor put in anENVline is baked into a layer that can be extracted. Pass secrets at runtime, and keep.envfiles out of the build with a.dockerignore. - Running as root - by default many containers run as root inside. Use a non-root user where the image supports it.
- Exposed ports - publishing a database port to the whole internet is a common and expensive mistake. Bind it to
127.0.0.1unless something outside needs it. - Latest tag drift - an unpinned image can change under you the next time you pull.
The mental model to keep
Image equals recipe, container equals the meal being cooked, volume equals the fridge where leftovers stay. You can throw the pan away and cook again from the same recipe, and the leftovers are still there. If you remember that, you already understand why Docker upgrades are mostly pull a new image, recreate the container, keep the volume.
Treat containers as disposable and data as precious, and Docker stops being mysterious.
Docs: docs.docker.com

Written by
Shihab Hossain — Full-Stack Developer
I build thoughtful digital products from Bangladesh — fast web experiences with Next.js, Android apps with Flutter, and the infrastructure behind them.
Work with me