Dockerizing the python poetry application
Over the months, I have been continuosly used poetry for packaging and since then , I have found dockerizing a poetry application to be burder full. But after a peaceful research I found the things I was misleaded through internet and finally with the help of some sites problems in stackoverflow found a global dockerfile and solution for building a solution. For this video guidance check the video.
At first, lets have a poetry application by running poetry
poetry new sample-app
lets add sanic since I am using sanic server
poetry add sanic
Lets create two files __main__.py and app.py inside the sample_app directory
a) app.py
from sanic import Sanicapp = Sanic("Sample application")def run(): app.run(auto_reload=True, host="0.0.0.0", port=5002)
b) __main__.py
import sample_app.appsample_app.app.run()
Now since we have our application ready lets write that builds dockerfile that creates image for docker.
c)Dockerfile or dockerfile <you can use any name without any extension>
FROM python:3.10RUN mkdir /app2WORKDIR /app2COPY . .# ENV PYTHONPATH=${PYTHONPATH}:${PWD}RUN pip3 install poetryRUN poetry installEXPOSE 5002CMD ["poetry", "run", "app"]
The above step includes setting workdir after creation of directory and then we install python poetry with pip3 in my case. If you use pip change it to pip install poetry. Exposing the port open the container port for public. and at last the cmd for running the script app.py