Docker for Beginners: Images, Containers and Volumes
Docker packages an application with everything it needs (runtime, libraries, config) into an image. Running an image creates a container: an isolated process that behaves the same on your laptop, a CI runner and a production server.
Images vs containers
- An image is a read-only template, built from a
Dockerfileand made of layers. - A container is a running (or stopped) instance of an image, with its own writable layer.
- One image can run as many containers.
Commands you'll use every day
docker run -d --name web -p 8080:80 nginx # run in background, host 8080 -> container 80
docker ps # running containers
docker logs -f web # follow logs
docker exec -it web sh # shell inside the container
docker stop web && docker rm web
docker images # local images
docker pull postgres:17
docker system prune # clean up unused data
Environment variables
docker run -d --name db \
-e POSTGRES_PASSWORD=secret \
-e POSTGRES_DB=app \
-p 5432:5432 \
postgres:17
Volumes: keep data when containers are removed
A container's filesystem disappears with the container. Put data that must survive in a volume:
docker volume create pgdata
docker run -d --name db -e POSTGRES_PASSWORD=secret \
-v pgdata:/var/lib/postgresql/data postgres:17
# Bind mount: share a host folder (great for development)
docker run --rm -v "$PWD":/app -w /app node:22 npm test
Your first image
FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
CMD ["node", "server.js"]
docker build -t my-api:1.0 .
docker run -d -p 3000:3000 my-api:1.0
Containers are not virtual machines: they share the host's kernel, which is why they start in milliseconds and use little memory.
Was this article helpful?
Comments
No comments yet. Have a question or a tip? Be the first to share it.