How to write a Dockerfile for a custom python project?

≡放荡痞女 提交于 2020-12-31 17:33:38

问题


I'm pretty new to Docker, and I need to create the container to run Docker container as an Apache Mesos task.

The problem is that I can't find any relevant examples. They all are centered around Web development, which is not my case.

I have a pure Python project with large number of dependencies ( like Berkeley Caffe or OpenCV ). How to write a Docker file to properly enroll all dependecies ( and how to find them out?)


回答1:


The docker hub registry contains a number of official language images, which you can use as your base image.

  • https://hub.docker.com/_/python/

The instructions tell you how you can build your python project, including the importation of dependencies.

├── Dockerfile                <-- Docker build file
├── requirements.txt          <-- List of pip dependencies
└── your-daemon-or-script.py  <-- Python script to run

Image supports both Python 2 and 3, you specify this in the Dockerfile:

FROM python:3-onbuild
CMD [ "python", "./your-daemon-or-script.py" ]

The base image uses special ONBUILD instructions to all the hard work for you.




回答2:


The official Docker site has some step-by-step and reference documentation.

However, to get you started: what might help is to think about what you would do if you were to install and start your project on a fresh machine. You'd probably do something like this...

apt-get update
apt-get install -y python python-opencv wget ...
# copy your app into /myapp/
python /myapp/myscript.py

This maps more or less one-to-one to

FROM ubuntu:14.04
MAINTAINER Vast Academician <vast@example.com>
RUN apt-get update && apt-get install -y python python-opencv wget ...
COPY /path/on/host/to/myapp /myapp
CMD ["python", "/myapp/myscript.py"]

The above is untested, of course, but you probably get the idea.



来源:https://stackoverflow.com/questions/34381370/how-to-write-a-dockerfile-for-a-custom-python-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!