I'm attempting to Dockerise a Python application, which depends on OpenCV. I've tried several different ways, but I keep getting... ImportError: No module named cv2
when I attempt to run the application.
Here's my current Dockerfile.
FROM python:2.7
MAINTAINER Ewan Valentine <ewan@theladbible.com>
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# Various Python and C/build deps
RUN apt-get update && apt-get install -y \
wget \
build-essential \
cmake \
git \
pkg-config \
python-dev \
python-opencv \
libopencv-dev \
libav-tools \
libjpeg-dev \
libpng-dev \
libtiff-dev \
libjasper-dev \
libgtk2.0-dev \
python-numpy \
python-pycurl \
libatlas-base-dev \
gfortran \
webp \
python-opencv
# Install Open CV - Warning, this takes absolutely forever
RUN cd ~ && git clone https://github.com/Itseez/opencv.git && \
cd opencv && \
git checkout 3.0.0 && \
cd ~ && git clone https://github.com/Itseez/opencv_contrib.git && \
cd opencv_contrib && \
git checkout 3.0.0 && \
cd ~/opencv && mkdir -p build && cd build && \
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D BUILD_EXAMPLES=OFF .. && \
make -j4 && \
make install && \
ldconfig
COPY requirements.txt /usr/src/app/
RUN pip install --no-cache-dir -r requirements.txt
COPY . /usr/src/app
And my requirements.txt file
Flask==0.8
gunicorn==0.14.2
requests==0.11.1
bs4==0.0.1
nltk==3.2.1
pymysql==0.7.2
xlsxwriter==0.8.5
numpy==1.11
Pillow==3.2.0
cv2==1.0
pytesseract==0.1
Fixed with a slightly different set-up
Here's an image that is built on Ubuntu 16.04 with Python2 + Python3 + OpenCV. You can pull it using
docker pull chennavarri/ubuntu_opencv_python
Here's the Dockerfile (provided in the same dockerhub repo mentioned above) that will install opencv for both python2 and python3 on Ubuntu 16.04 and also sets the appropriate raw1394 link. Copied from https://github.com/chennavarri/docker-ubuntu-python-opencv
Some additional instructions for people newly starting with Docker:
In the directory where you put this Dockerfile, build the docker image as
docker build -t ubuntu_cv .
Once the image is built, you can check by doing
docker images
You can start a docker container as
docker run -t -i ubuntu_cv:latest
Thanks for posting this. I ran into the same issue and tried your solution and although it seemed to install OpenCV it left me with an issue of conflicting versions of the Python six library so I took a different route. I think a simpler way to do this is to install Anaconda in your container and then add OpenCV. I'm using Python 2 so my entire Dockerfile to get OpenCvv installed is just:
This will install Anaconda from the continuumio/anaconda Dockerfile and then it will use Anaconda to install opencv. There is a seperate continuumio Dockerfile for Python 3 if you need that as well.