LoginSignup
7
6

More than 1 year has passed since last update.

【Python】DockerでOpenCVをインストールする時の設定

Posted at

はじまり

DockerでPython用のコンテナをビルドする時に
OpenCVも一緒にインストールしたら躓いたので、備忘録。

最初に躓いたイメージ

まず、このイメージでビルドししてopencvを利用しているプログラムを実行しました。

FROM ubuntu:latest

ENV PYTHON_VERSION 3.7.1
ENV HOME /root
ENV PYTHON_ROOT $HOME/local/python-$PYTHON_VERSION
ENV PATH $PYTHON_ROOT/bin:$PATH
ENV PYENV_ROOT $HOME/.pyenv

RUN apt update

RUN apt install -y python3 python3-pip
RUN pip install --upgrade pip

WORKDIR /usr/src/app
COPY ./ /usr/src/app

RUN pip install opencv-python

そうしたら、こんなエラーが出現。

# python3 app.py

------------------------------------------------------------------------------------
Traceback (most recent call last):
  File "app.py", line 4, in <module>
    import cv2
  File "/usr/local/lib/python3.8/dist-packages/cv2/__init__.py", line 8, in <module>
    from .cv2 import *
ImportError: libGL.so.1: cannot open shared object file: No such file or directory
------------------------------------------------------------------------------------

設定その1:モジュール追加その1、そして再度躓く

どうやら、OpenCVの実行に必要なモジュールをaptで取ってくる必要があるらしい。

FROM ubuntu:latest

ENV PYTHON_VERSION 3.7.1
ENV HOME /root
ENV PYTHON_ROOT $HOME/local/python-$PYTHON_VERSION
ENV PATH $PYTHON_ROOT/bin:$PATH
ENV PYENV_ROOT $HOME/.pyenv

RUN apt update
# ↓追加↓
RUN apt-get install -y libgl1-mesa-dev

RUN apt install -y python3 python3-pip
RUN pip install --upgrade pip

WORKDIR /usr/src/app
COPY ./ /usr/src/app

RUN pip install opencv-python

しかぁし、またもや躓く。

------------------------------------------------------------------------------------
Traceback (most recent call last):
  File "app.py", line 4, in <module>
    import cv2
  File "/usr/local/lib/python3.8/dist-packages/cv2/__init__.py", line 8, in <module>
    from .cv2 import *
ImportError: libgthread-2.0.so.0: cannot open shared object file: No such file or directory
------------------------------------------------------------------------------------

設定その2:モジュール追加その2、そして再度躓く

どうやら、OpenCVの実行に必要な別のモジュールをaptで取ってくる必要があるらしい。

FROM ubuntu:latest

ENV PYTHON_VERSION 3.7.1
ENV HOME /root
ENV PYTHON_ROOT $HOME/local/python-$PYTHON_VERSION
ENV PATH $PYTHON_ROOT/bin:$PATH
ENV PYENV_ROOT $HOME/.pyenv

RUN apt update

# - ↓削除↓
RUN apt-get install -y libgl1-mesa-dev
# + ↓追加↓
RUN apt install -y libopencv-dev

RUN apt install -y python3 python3-pip
RUN pip install --upgrade pip

WORKDIR /usr/src/app
COPY ./ /usr/src/app

RUN pip install opencv-python

しかし、ビルドを始めて近くのコンビニに行って帰ってきたら、、、

-----------------------------------------------------------------------------------------------------------
=> [3/8] RUN apt install -y libopencv-dev                                                          833.9s
 => => # questions will narrow this down by presenting a list of cities, representing                                                                                          
 => => # the time zones in which they are located.                                                                                                                             
 => => #   1. Africa      4. Australia  7. Atlantic  10. Pacific  13. Etc                                                                                                      
 => => #   2. America     5. Arctic     8. Europe    11. SystemV                                                                                                               
 => => #   3. Antarctica  6. Asia       9. Indian    12. US                                                                                                                    
 => => # Geographic area:                                                                                                                                                      
-----------------------------------------------------------------------------------------------------------

タイムゾーンの選択を愚直に待ち続けていたのでした・・・

設定その3:タイムゾーンを設定する

サーバのタイムゾーンを設定していないことで発生する模様。以下で追加して対処。

RUN ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime

そしたら、無事Pythonプログラムが走りました。

最終的にビルドできたイメージ

FROM ubuntu:latest

ENV PYTHON_VERSION 3.7.1
ENV HOME /root
ENV PYTHON_ROOT $HOME/local/python-$PYTHON_VERSION
ENV PATH $PYTHON_ROOT/bin:$PATH
ENV PYENV_ROOT $HOME/.pyenv

# タイムゾーン
RUN ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime

# apt
RUN apt update
RUN apt install -y libopencv-dev

# install python and pip
RUN apt install -y python3 python3-pip
RUN pip install --upgrade pip

# set working directory and copy files
WORKDIR /usr/src/app
COPY ./ /usr/src/app

# install opencv
RUN pip install opencv-python

おしまい

タイムゾーン設定は忘れないようにしなければですね。いい勉強になりました。

7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6