
Docker from scratch, part 1: images, containers, volumes, and networks
🌐 Machine-translated from Russian. Show original
In this part:
Why Docker is needed#
Docker packages the application along with everything it needs: language version, libraries, system packages. The result is an image that runs equally on the developer's laptop, test, and production server. The phrase "it works" ceases to be an argument.
Image and container#
- An image is a read-only pattern: file system and run command. For example,
postgres:17ornginx:1.27. - Container — a running instance of an image. You can run as many containers as you want from a single image.
- Images are downloaded from the registry: Docker Hub, GitHub Container Registry, GitLab Registry, or your own.
docker pull nginx:1.27 # скачать образ
docker images # список образов
docker run -d --name web -p 8080:80 nginx:1.27 # запустить в фоне, порт 8080 → 80
docker ps # запущенные контейнеры
Open http://localhost:8080 — nginx works, even though you haven't installed it.
💡 Always specify the version tag.
latestchanges without warning, and one day the server will upgrade with a different version.
Main teams#
| Command | What does |
|---|---|
docker logs -f web | view container logs |
docker exec -it web sh | to enter the container |
docker stop web / docker start web | stop / start |
docker rm web | to remove the |
docker inspect web | all container parameters in JSON |
docker stats | real-time CPU and memory consumption |
Volume: where to store data#
The container file system is temporary: if the container is deleted, the data is lost. Keep everything that needs to be saved (databases, downloads) in the volumes:
docker volume create pgdata
docker run -d --name db -e POSTGRES_PASSWORD=secret \
-v pgdata:/var/lib/postgresql/data postgres:17
- Named volume (
pgdata:/путь) — Docker decides where to store it. Convenient for databases. - Bind mount (
./config:/etc/app:ro) — a folder from the host. Convenient for configs;:ro— read-only.
Networks: how containers find each other#
Containers in one user network see each other by name:
docker network create app
docker run -d --name db --network app -e POSTGRES_PASSWORD=secret postgres:17
docker run -d --name api --network app -e DATABASE_HOST=db my-api:1.0
The application connects to the database at the address db:5432 — no need to know the IP address. The database port is not published externally (-p not specified) — this is more secure.
Cleaning#
Docker quickly backs up old images and stopped containers:
docker system df # сколько места занято
docker image prune # удалить неиспользуемые образы без тегов
docker system prune # остановленные контейнеры, неиспользуемые сети и образы
docker volume prune deletes data volumes — run intentionally.
What's next#
We learned to run other people's images. In part 2 , we will package our own application into an image and describe the entire stack with a single Docker Compose file.
Comments · 0
No comments yet.Sign in to comment, rate and see all comments.