Junior — Middle
How did you create Docker images from scratch in your projects?
sobes.tech AI
Answer from AI
To create a Docker image from scratch in Python projects, I usually write a Dockerfile that starts with a base image, such as python:3.9-slim. Then I copy the application's source code into the container, install dependencies using pip, and set the command to run.
A simple example of a Dockerfile:
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . ./
CMD ["python", "app.py"]
This approach allows creating a lightweight image with minimal dependencies, ready to run the application. It is important to optimize layers during the process to speed up the build and reduce the image size.