The problem it solves
The classic situation: the project runs for you and not for your colleague. The cause is different versions of the language, libraries or operating system. Diagnosing it takes a day.
A container solves this by packaging the environment along with the code — the right language version, the libraries, the settings. It then runs identically for anyone and on any server.
"It works on my machine" stops being an argument: if it works in the container, it works everywhere.
What a container is, plainly
Not a virtual machine. A virtual machine is a whole computer inside a computer: heavy and slow.
A container is an isolated box that shares your system's kernel but has its own file system and processes. It starts in seconds and weighs little.
Two terms to keep apart:
- An image — the recipe and the built artefact. An immutable snapshot with your application and its environment.
- A container — a running instance of an image. One image can produce many.
First commands
Pull a ready image and run it:
docker run --rm hello-world
Run a database without installing it on your system:
docker run --name pg -e POSTGRES_PASSWORD=secret -p 5432:5432 -d postgres
Reading that: a container name, an environment variable with the password, a port exposed outward, and detached execution.
Incidentally that's the easiest way to get a database for study: nothing to install and nothing to clean up afterwards.
See what's running:
docker ps
Stop and remove:
docker stop pg
docker rm pg
A minimal build file
A file of instructions for building your project's image. For a Python application, roughly:
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "main.py"]
Line by line: start from a ready image with the language, set a working directory, copy the dependency list, install them, copy the code, declare the start command.
The order matters: dependencies are copied and installed before the code, so that changing the code doesn't reinstall them. That speeds up rebuilds noticeably.
Build and run:
docker build -t myapp .
docker run --rm -p 8000:8000 myapp
What not to put in an image
- Passwords and keys. Pass those as environment variables.
- Your machine's virtual environment.
- The version history folder and other junk — there's an exclusion file for that.
Why a beginner should care
First, it appears in postings increasingly often, and being able to package your own application sets you apart.
Second, it's convenient for yourself: databases and helper services for study come up with one command and go away just as easily, without cluttering your system.
Third, a containerised project is easy to deploy anywhere — you aren't tied to one platform.
Хватит читать — пора делать
На CohortX можно найти команду под пет-проект и получить тот самый опыт, о котором спрашивают на собеседовании.
Похожие статьи
- Running an app and a database together: meeting ComposeHow to describe several services in one file, start everything with one command, and keep passwords out of the code.
- Поднять базу и приложение вместе: знакомство с ComposeКак описать несколько служб одним файлом, запустить всё одной командой и не хранить пароли в коде.
- Docker для начинающих: зачем он нуженЧто такое контейнер простыми словами, какую проблему решает, первые команды и минимальный файл описания.