Home > Net >  How to dockerize multi-file python project
How to dockerize multi-file python project

Time:04-12

My project structure:

├── async_fastapi
    ├── main.py
    ├── auth.py
    ├── db.py
    ├── schemas.py
    ├── Token.py
    ├── users.py
    ├── items.py

This question might seem dumb for you but I can not figure it out. It is my believe that Dockerfile should be build on specific python file. But what if my project is spread across multiple files/directories ? Do I have create specific Dockerfile for each .py and then put them together with docker-compose or is there other way ?

CodePudding user response:

Let's modify your project structure a little, so that it becomes build-easy.

├── requirements.txt
├── Dockerfile
├── async_fastapi
    ├── main.py
    ├── auth.py
    ├── db.py
    ├── schemas.py
    ├── Token.py
    ├── users.py
    ├── items.py

Put all your dependencies in the requirements.txt so that it can be installed in the container. In the Dockerfile, you can have something like below.

FROM python:3.8.10
ENV WORKSPACE /opt/installs
RUN mkdir -p $WORKSPACE
WORKDIR $WORKSPACE
COPY requirements.txt requirements.txt
RUN python3.8 -m pip install -r requirements.txt
COPY async_fastapi .
CMD ["python3.8", "main.py"]

This must be enough to build it. Ofc, the docker definition is only on assumptions, which you could adjust.

  • Related