Docker Now, Ep1: Python+OpenCV imshow

Jerin K Antony
4 min readFeb 25, 2021
Image: https://www.techrepublic.com/article/how-to-create-a-docker-image-and-push-it-to-docker-hub/

Gone are the days of bare metal installs, gone are the days of cliché excuse “Well, it worked on my machine!”, its the time of containers. Docker is the most loved platform for building containers.

So here we are about to start our journey into understanding Docker and using it in our daily life :) If you find any of the explanation here non sensible, just dial up here, https://docs.docker.com/get-started/ and you will get all the answers you are looking for.

Install

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
sudo usermod -aG docker $USER
# logout and login for sudo change to effect

Installation Check

docker run hello-world

Get me some starter code

Well, no more dialogues, right to the point, lets create a docker image for python OpenCV application to show an image. Clone the github repo using command,

git clone https://github.com/jerinka/imshow_docker

It contains the following items:

| — 2.png
| — Dockerfile
| — README.md
| — requirements.txt
| — runDocker.sh
` — show.py

Basically, a script named show.py, it just reads and displays and image,

import cv2img = cv2.imread('2.png',1)
cv2.imshow('img',img)
cv2.waitKey(1000)

A requirements.txt contains dependencies to be installed,

opencv-python==4.0.0.21

Dockerfile

Now the hero comes, Dockerfile! Will explain it in detail,

FROM python:3.6.9-slim# Install system packages
RUN apt-get update && apt-get install -y --no-install-recommends \
bzip2 \
g++ \
git \
graphviz \
libgl1-mesa-glx \
libhdf5-dev \
openmpi-bin \
wget \
python3-tk && \
rm -rf /var/lib/apt/lists/*

# Setting up working directory
RUN mkdir /src
WORKDIR /src
COPY requirements.txt requirements.txtRUN pip install --upgrade pipRUN pip install --no-cache-dir -r requirements.txt# Minimize image size
RUN (apt-get autoremove -y; \
apt-get autoclean -y)
ENV QT_X11_NO_MITSHM=1CMD ["bash"]
#CMD ["python", "show.py"]

The very first command FROM is for getting a base image. Base image is like a fresh PC with one particular software, or a pack of software preinstalled, available in DockerHub. Then you install stuffs in this brand-new PC using RUN command. Make a directory named src in this image and copy requirements from PWD. Install stuffs, set some environment variables and finally, CMD for executing command in terminal of the brand-new PC you just created. It can be bash, it can be python show.py, anything you want to run. With bash, you can play around with terminal of the image you created.

Build

Build the Docker Image,

docker build -t jerinka/opencv:1 .

Run the Image,

docker run -it --rm -v ${PWD}:/src jerinka/opencv:1

Above command mounts present working directory to src folder of image created and all the contents of PWD will be accessible in scr folder of Docker Image! Also we got command prompt to access the container! Now try your favourite bash command, is that ls?

RUN

Now, if you try python show.py, it will show error ‘: cannot connect to X server’, solution is to allow the services, better in a shell script,

runDocker.sh

xhost +local:docker
XSOCK=/tmp/.X11-unix
XAUTH=/tmp/.docker.xauth
xauth nlist $DISPLAY | sed -e 's/^..../ffff/' | xauth -f $XAUTH nmerge -
docker run -m 8GB -it --rm -e DISPLAY=$DISPLAY -v $XSOCK:$XSOCK -v $XAUTH:$XAUTH -e XAUTHORITY=$XAUTH -v ${PWD}:/src -it jerinka/opencv:1
xhost -local:docker

Now you can run docker via command,

sh runDocker.sh

And now try,

root@9c9323322:/src# python show.py

App runs and here comes the long waited ‘output’

Imshow Figure

PUSH

Ok, its time to push the image to Dockerhub! Create one account first, remember your username and password. Go to terminal and type,

docker login -u jerinkadocker push jerinka/opencv:1

Change jerinka to your username and opencv:1 to your image name and tag.

README.md

It's time for you to play around, learn more from Docker get-started tutorial. BTW Docker official tutorials are really good, very sensible examples and explanation. Make sure you add a Dockerfile + steps to build/run in a README.md to your existing projects and help others in understanding same.

# Docker mnist show## Install docker and nvidia-docker2[Follow the steps here](https://cnvrg.io/how-to-setup-docker-and-nvidia-docker-2-0-on-ubuntu-18-04/)## Docker Tutorial[Orientation and setup](https://docs.docker.com/get-started/)## Running cpu docker image```
bash runDocker.sh
```


## Running imshow example
```
python3 show.py
```
## Building Docker Image and pushing to Dockerhub```
docker build -t jerinka/opencv:1 .
```
```
docker push jerinka/opencv:1
```

RUN on any other system

Just install Docker in any system, clone the github repository and stuff just works.. that's it.

sh runDocker.sh

Additional Resources:

Video Tutorials: https://youtu.be/wi-MGFhrad0

As usual, all codes are available in github repo — imshow_Docker

Other Tutorials:

Course on GitLab CICD: https://www.udemy.com/course/gitlab-cicd-essentials-for-industry-comprehensive-tutorial/?referralCode=78BD52230019795171CF

About Me:

ML Tech lead at Ignitarium

LinkedIn: www.linkedin.com/in/jerinkantony

Happy Coding

--

--