Guest mode. You can read posts without signing up. Comments, ratings, job contacts and your own posts open after you sign up and accept the rules. What is Kommit →

Редакция КоммитРедакция портала
commit e863ae3 · 19 days ago

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:17 or nginx: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. latest changes without warning, and one day the server will upgrade with a different version.

Main teams#

CommandWhat does
docker logs -f webview container logs
docker exec -it web shto enter the container
docker stop web / docker start webstop / start
docker rm webto remove the
docker inspect weball container parameters in JSON
docker statsreal-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 00 участников · 0 гостей🔒 Rate and discuss after signing up

Comments · 0

No comments yet.Sign in to comment, rate and see all comments.