Docker環境にJupyter labとOpenCVをinstallしたい

この記事は最終更新日から1年以上が経過しています。

M1 Macを購入したのですが、重宝していたpipenv環境でライブラリがインストールできない事象が結構発生し、OpenCVもM1 Mac環境でインストールに失敗してしまいました。。

ならば、Dockerコンテナの中ならいけるのではと思い立ちチャレンジしてみることにしました。

ただ、Google Colaboratorが便利なので別に環境構築などしなくてもと思ったのですが、ローカル環境で色々試行錯誤したい時にColaboratorだと私は少し気が引けたりします(変な画像とかUPするの恥ずかしかったりします笑)

なのでついでにJupyter labも入れてしまうことにしました!

まずはDockrfileからいきます。openCV関連のインストールからやっていきます。

FROM centos:latest

RUN yum update -y
RUN yum install -y python3 python3-pip
RUN pip3 install --upgrade pip setuptools
RUN pip3 install opencv-python opencv-contrib-python numpy pandas

そしてdocker-compose.ymlです。

version: '3'

services:
  opencv_test:
    container_name: opencv_test
    build:
      context: .
      dockerfile: Dockerfile
    command: python3
    tty: true
    working_dir: '/root/'
    volumes:
      - ./opt:/root

ではbuildします。

$ docker-compose build
$ docker-compose up

build後docker-compose up

docker container exec -it opencv_test bash

でコンテナの中に入りました。

import cv2
import numpy as np

filename = "test.jpg"
img = cv2.imread(filename, 1)
cv2.imwrite("write_test.jpg", img)
print(img)

index.pyというファイルにテストコードを記載しているので実行してみます。

python3 index.py

するとなんとエラーが出てしまいました。

ImportError: libGL.so.1: cannot open shared object file: No such file or directory

が原因のようなのですが、詳しい原因はわかりませんでした。

色々調べたところosにmesa-libGLをインストールする必要があるようでした。

Dockerfileを編集します。

FROM centos:latest

RUN yum update -y
RUN yum install -y python3 python3-pip mesa-libGL # 追記しました
RUN pip3 install --upgrade pip setuptools
RUN pip3 install opencv-python opencv-contrib-python numpy pandas

もう一度、docker-compose build&docker-compose upしてからコンテナに入って

python3 index.pyをしました。

imgデータがprintされ、無事に動いておりました!

では次はJupyter labです。

Dockerfileに追記しました。

FROM centos:latest

RUN yum update -y
RUN yum install -y python3 python3-pip mesa-libGL
RUN pip3 install --upgrade pip setuptools
RUN pip3 install opencv-python opencv-contrib-python numpy pandas jupyterlab #追記しました

ほいで、docker-compose.ymlのコマンド部分もjupyter-labように修正します。

version: '3'

services:
  opencv_test:
    container_name: opencv_test
    build:
      context: .
      dockerfile: Dockerfile
    command: jupyter-lab # 編集しました
    tty: true
    working_dir: '/root/'
    volumes:
      - ./opt:/root

一旦docker-compose downをしてコンテナを落とします。

ではbuildしますdocker-compose build

残念エラーでした。

error: command 'gcc' failed with exit status 1

↑のエラーが出ていたのでこちらも根本的な原因はわかりませんでしたが、

OSにgccを入れてみます。

FROM centos:latest

RUN yum update -y
RUN yum install -y python3 python3-pip mesa-libGL gcc # 追記しました。
RUN pip3 install --upgrade pip setuptools
RUN pip3 install opencv-python opencv-contrib-python numpy pandas jupyterlab

ではもう一度、docker-compose build

残念またダメでした。。腰痛は悪化しました。

include <pyconfig.h>のエラーが出ていたのでpythonが古いかもと思い

python3-develでpython3をインストールしてみることにしました。

FROM centos:latest

RUN yum update -y
RUN yum install -y python3-devel python3-pip mesa-libGL gcc # python3をpyton-develに変えました
RUN pip3 install --upgrade pip setuptools
RUN pip3 install opencv-python opencv-contrib-python numpy pandas jupyterlab

ではbuildします。

buildできました!

ではコンテナ起動です。

$ docker-compose up残念コンテナが落ちました。。

opencv_test | [C 2021-11-25 11:58:15.390 ServerApp] Running as root is not recommended. Use --allow-root to bypass.

ルートでコマンドを叩くなということのようです。

docker-compose.ymlを修正しました。

version: '3'

services:
  opencv_test:
    container_name: opencv_test
    build:
      context: .
      dockerfile: Dockerfile
    command: jupyter-lab --allow-root # --allow-rootを追記しました。
    tty: true
    working_dir: '/root/'
    volumes:
      - ./opt:/root

では立ち上げます。$ docker-compose up

おお!立ち上がりましたがノートブックにアクセスできません!

涙が出そうですが、portの設定をしていませんでした。

docker-compose.ymlを修正します。

version: '3'

services:
  opencv_test:
    container_name: opencv_test
    build:
      context: .
      dockerfile: Dockerfile
    command: jupyter-lab --allow-root
    tty: true
    working_dir: '/root/'
    volumes:
      - ./opt:/root
    ports:
      - '8888:8888' # 追記しました。

ではもう一度 docker-compose up

!!状況変わらずでした。。

わからなくなってしまいましたので「jupyter note 開かん docker」で検索しました。

どうやらdockerコンテナの外からアクセスしているので弾かれてしまうようでした。なのでコマンドに --ipオプションを付けることで解消できるようです。

docker-compose.ymlを修正します。

version: '3'

services:
  opencv_test:
    container_name: opencv_test
    build:
      context: .
      dockerfile: Dockerfile
    command: jupyter-lab --ip=* --allow-root # 追記しました。
    tty: true
    working_dir: '/root/'
    volumes:
      - ./opt:/root
    ports:
      - '8888:8888'



ではもう一度 docker-compose upしてターミナルに出力されたURLでアクセスします。

おおお!立ち上がりました。

お試しで先ほど作ったプログラムもnotebook上に入力します。

おおやっと動きました。

一安心です。

今回やってみて思ったのは、centos系のエラーの原因の根本がわからなかったのが悔しかったです。

ただnotebookが動いたので、色々と実験をしていきたいと思いました。

ここまでお読みいただきありがとうございました。