Junior — Middle
Can you explain the process of building an image in Docker and how it works?
sobes.tech AI
Answer from AI
Building an image in Docker is the process of creating a ready-to-run container based on the description in a Dockerfile. The Dockerfile contains instructions that are executed sequentially to form the image.
How it works:
- Docker reads the Dockerfile line by line.
- Each instruction (for example, FROM, RUN, COPY) is executed, creating a new layer of the image.
- The result is an image containing all necessary dependencies, files, and configurations.
Example of a simple Dockerfile for a Python application:
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]
Build command:
docker build -t my-python-app .
As a result, you will get an image named my-python-app, which can be run in a container.