Docker Now, Ep1: Python+OpenCV imshow

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 your-user

Installation Check

docker run hello-world

Get me some starter code pls

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

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 are going to create.

Build

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

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

docker login -u jerinkadocker push jerinka/opencv:1

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

README.md

# 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

sh runDocker.sh

Additional Resources:

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

About Me:

LinkedIn: www.linkedin.com/in/jerinkantony

Happy Coding

AI Enthusiast

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store