简体   繁体   中英

Make a Dockerfile automatically from a Python project

I have a python project as below:

project
|___build.py
|___scrip1.py
|___script2.py
|___main.py

The main.py file uses functions and datasets developed in scrip1.py and script2.py. The build.py has all the needed libraries/packages

For now I am making a Dockerfile manually:

FROM python
RUN pip3 install numpy
RUN pip3 install pandas
RUN pip3 install matplotlib
RUN pip3 install scipy
ADD script1.py /
ADD script2.py /
ADD main.py /
CMD [ "python", "./main.py" ]

I am installing manually all the libraries needed in the build.py.

How can I automate the creation of a Dockerfile based on a python project? So instead of making the file manually I can run the script to make the Dockerfile.

This is a case of a package handle using pip, on your directory you can run:

pip freeze > requirements.txt

This will create a requirements.txt containing all packages to run your project.

Now in your Dockerfile

FROM python
RUN pip3 install -r requirements.txt
ADD script1.py /
ADD script2.py /
ADD main.py /
CMD [ "python", "./main.py" ]

One small optimization on your Dockerfile is replacing the ADD commands to just one COPY

FROM python
COPY . /
RUN pip3 install -r requirements.txt
CMD [ "python", "./main.py" ]

From here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM