Docker Now, Ep1: Python+OpenCV imshow


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, and you will get all the answers you are looking for.


curl -fsSL -o
sudo sh
sudo usermod -aG docker your-user

Installation Check

docker run hello-world

Get me some starter code pls

git clone

It contains the following items:

| — 2.png
| — Dockerfile
| —
| — requirements.txt
| —
` —

Basically a script named, it just reads and displays and image,

import cv2img = cv2.imread('2.png',1)

A requirements.txt contains dependencies to be installed,



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
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", ""]

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, anything you want to run. With bash, you can play around with terminal of the image you are going to create.


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?


xhost +local:docker
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,


And now try,

root@9c9323322:/src# python

App runs and here comes the long waited ‘output’

Imshow Figure


docker login -u jerinkadocker push jerinka/opencv:1

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

# Docker mnist show## Install docker and nvidia-docker2[Follow the steps here]( Docker Tutorial[Orientation and setup]( Running cpu docker image```

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

RUN on any other system


Additional Resources:

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

About Me:


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