Monday, October 2, 2017

Install Opencv in docker (ubuntu) build app in docker

Opencv in Docker, Build app in docker tutorial

How to run and build application based on Opencv in Docker container is purpose of this tutorial. The process described bellow is how to install Docker engine in windows machine, by command line download base Ubuntu image, install Opencv inside the container and commit changes to create own image to save your installation to add what ever opencv app you want to scale in Docker.. Cool ? No magic. Really
docker opencv app

Install Docker in windows

The important think is that windows running docker machine in Hyper-V virtual machine available under Windows 10. If you select this installation you can have troubles with Virtual Box no longer work after Hyper-V is set to enable.. There is option use also installation based on Oracle Virtual Box but i select easy install using the first options which is nicely integrated with Windows 10 and brings me advantage of docker development under ubuntu on windows machines.. 

For the installation just follow the step by step tutorial on first link for Hyper-V based installation

Download and install Links !Read text before!
To download Docker for Windows Hyper-V use this link
To download Docker for Windows using Oracle VirtualBox link

Play with docker

After the easy installation you should have available icon in Windows like in following image. 

Docker windows opencv
This allows you to control your docker engine like start stop and update, all the work is done by easy commands in CMD command line.. Do not worry of command line.. It is easy like to write email.. 
Docker opencv

The installed docker is now available through cmd line.. 
Docker help tutorial
You can simply verify by issue command as me docker help and if you have succesfully installed docker you should see the same output of possible command options. 

Docker basics 

Just briefly about docker.. I try to use simplest word to describe this. By issue command docker ps you can see that i do not have anything set up. The main components are docker container and image. 


Docker image

It is easy, Image is something like CD, all you need to install is there ready to be used by specific application. The most often the application you want to develop is included in this image. For example, basic ubuntu installation. This image could be extend to satisfy specific needs of developer. 

Docker contained

The container is running image instance. I have one image with ubuntu and http server inside. I could run from one image several containers with exactly same settings. This allows me easy scalable  deployment of my application from 10 users  on one container to 10 milion in hundred container of same app and setting from this base image. CD that could be installed several times is just container..

Install Opencv in ubuntu docker image

You can use different kind of images to start. I would like to recommend ubuntu, where we install our opencv library. The process is that you take some base image from already exist repository. This is just set up that you run container and image is basically downloaded in background. After you install opencv and prerequisites and commit changes inside container. This commit create your unique image with your saved changes (installed) opencv and save locally.. If you skipp this step your changes inside container are lost..  

C:\docker run -it ubuntu bash

By issue this command you run ubuntu basic container and start ubuntu bash command line.. You can exit bash command line inside ubuntu containe by type EXIT.
I just issue different command docker run -it ID_of_image bash because i have already image installed. But after you issue C:\docker run -it ubuntu bash you will be inside linux like on picture bellow. 
opencv docker

Now install opencv follow the documentation.

All is done in bash ubuntu command line. Again no mystery and who is afraid of command line is just because he used to work in different environment. But it is necessary and behind is nothing really hard.  Now we follow exactly same installation for Linux distribution, I got somethink for Debian here just a short version but you will be find with follow approach.. 

1. install Required dependencies 
We are root that mean sudo is not necessary, just copy this 3 lines one by one and follow the several instructions. Basically sometimes is write Y to command line. Nothink else. 

apt-get install build-essential

apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

Now you have everything you need to build opencv. Now issue this command to download source code from git. This will create you the opencv folder, and after by cd jump into opencv folder. By issue mkdir build you create new directory. Name as build! Jump into by cd build.. You can start install right now.

git clone

cd opencv 

mkdir build

cd build

Now you are in build folder. This one is empty but is under the opecv where the all source code are located. Here just run following command for basic configuration.. Now take some time and after you will see some overview what is available to be installed. For example avlibs to use video captures and other opencv vide options.. Do not forgot the dots on next line :)

cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..

Now is time to issue another command which build your opencv from source based on cmake configuration. By type simple make in linux bash.

make                      OR    make -j4 to run in parallel 4 build jobs.

But just make is quite fine. Now this steps take several minutes. 10 to 40 depends how much power and -j jobs you run to make opencv. The last is to install opencv into the system by make install. 

make install

Done, your library are ready and you can develop your app under Linux (But this will be in next tutorial) in docker (/usr/local/lib) check the screen shot below for location cd /usr/local/lib , and list them by ls to display this.
docker ubuntu opencv

 NOW we need to save what is in our container. When we are saving new container we create new image from we are starting containers.  

!!Do not issue exit yet.. save first!! 

Save changes mean - commit and create new image

This create new image with all the changes and allow you start container from this image with all the configuration from previous step. Maybe run 1000 containers to serve the video application for millions of customers.  Open new commad line windows like on image. Run command that display running container docker -ps.

Docker image

Now from new cmd line save the container by this command in c:\> docker commit your_container_id name/description:version ,BE sure that c3f279d17e0a is the same container id  obtain by docker -ps command.. Like in example. 

docker commit c3f279d17e0a ubuntu/image:version3

This take some time and create your local image. Than could be again started by same command.
C:\docker run -it ubuntu/image:version3 bash 

Next steps for docker/opencv tutorials 

Build the app inside the environment which is just prepare and use visual studio with remote build in ubuntu container.. The next tutorial already in progress.

Reference for docker

The best easy manual for docker is here


  1. Really, you blog has interesting and very valuable information about the translation.

    ทางเข้า sbo
    ทางเข้า sbobet

  2. Wow, this is quite amazing. I think it would be that many people still find these stories.




  3. GREAT ARTICLE! It is really interesting to read from the beginning & I would like to share your blog to my circles, keep sharing… DevOps Online Training
    DevOps Online Training