Vast Academician
Vast Academician

Reputation: 357

How to write a Dockerfile for a custom python project?

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?)

Upvotes: 3

Views: 3342

Answers (2)

Mark O'Connor
Mark O'Connor

Reputation: 77971

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

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.

Upvotes: 5

Wander Nauta
Wander Nauta

Reputation: 19625

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 <[email protected]>
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.

Upvotes: 0

Related Questions