Taking pictures in Docker container!
Have you ever tried to click photos with your camera in the laptop, of course, you did, but here is a twist, we will click the photo in a Docker container, many of you may be wondering how you can do that? Well, let me get started with this interesting thing.
Firstly, here are some of the prerequisites you should have on your laptop:
- Redhat Linux/ CentOS Linux
- You should have the basic knowledge of the Docker and Linux command line.
And, some of the python libraries, that we will discuss later.
So, are you excited!! I hope you are. Let’s get started:
Here, for this tutorial, I will be using RHEL (Redhat Enterprise Linux), which is a Linux distribution. So, to use docker in the RHEL system, we first have to install it by the following command:
✔ Step 1 :
# yum install docker # systemctl start docker
- The first line is used to install Docker CE (Community Edition).
- And after that, we have to start the service to use a docker container.
#docker run -it -p 55555:22 --privileged -v /dev/video0:/dev/video0 -v /root/Desktop/rhel7_5_extras:/extras -v /root/Desktop/rhel7_extra_new_rpm:/new -v /root/Desktop/python_lib:/python -v /run/media/root/RHEL-7.5\ Server.x86_64:/dvd -v /tmp/.X11-unix:/tmp/.X11-unix:ro -e DISPLAY=$DISPLAY --name camera1 centos:latest
Now moving on to the main part of this task, i.e. launching a docker container in RHEL.
Woah, that’s a big line, don’t be afraid, I will break down each keyword to you:
- Firstly I have used the “docker run” command that is used to launch a new container.
- Next, I have used options -i, -t, -p, -v,- -privileged, -e,- -name :
--interactive or -i
To keep STDIN open even if not attached.
-t or --tty
To allocate a pseudo-TTY.
--publish , -p
Publish a container’s port(s) to the host.
- “55555:22”: Map TCP port 22 in the container to port 55555on the Docker host.
--volume or -v
Bind mount a volume.
- “/dev/video0:/dev/video0”: This is the webcam name of your laptop so that you can access it. And, after the colon, this will be saved in the docker container.
- “/root/Desktop/rhel7_5_extras:/extras”: This image is imported because it has the required tools available, in this case, Python3, and it will be saved in the docker container as extras.
- “/root/Desktop/rhel7_extra_new_rpm:/new”: Again this image also contains some of the required tools to perform this task.
- “/root/Desktop/python_lib:/python”: It contains the Python libraries that will be used further to be installed in the docker container.
- “/run/media/root/RHEL-7.5\ Server.x86_64:/dvd”: This is the RHEL DVD which contains all the software.
- “/tmp/.X11-unix:/tmp/.X11-unix:ro”: X server is a windowing system for bitmap displays, common on Linux operating systems. To simply understand this, you can use GUI in your docker container.
Give extended privileges to this container.
--env , -e
Set environment variables.
“DISPLAY=$DISPLAY”: The magic word in the X window system is DISPLAY. A display consists (simplified) of:
- A mouse
- and a screen.
A display is managed by a server program, known as an X server. The server serves displaying capabilities to other programs that connect to it. The remote server knows where it has to redirect the X network traffic via the definition of the DISPLAY environment variable which generally points to an X Display server located on your local computer.
Assign a name to the container.
- “camera1”: It is the name given to the docker container.
At last, there is “centos:latest” which is the image I am using to deploy my docker container.
✔ Step 3:
# vim /etc/ssh/sshd_config X11UseLocalhost no X11Forwarding yes
The /etc/ssh/sshd_config file is the system-wide configuration file for OpenSSH which allows you to set options that modify the operation of the daemon. This file contains keyword-value pairs, one per line, with keywords being case insensitive.
To run graphical applications we have used, X11Forwarding yes.
✔ Step 4:
# dbus-uuidgen > /etc/machine-id
D-Bus is a software bus, inter-process communication, and remote procedure call mechanism that allows communication between multiple processes running concurrently on the same machine.
The /etc/machine-id file contains the unique machine ID of the local system that is set during installation or boot.
✔ Step 5:
# export QT_X11_NO_MITSHM=1
To set the environmental flag to force Qt to show properly.
Qt is a free and open-source widget toolkit for creating graphical user interfaces as well as cross-platform applications that run on various software and hardware platforms such as Linux, Windows.
✔ Step 6:
# cd etc/yum.repos.d/ # vi repository.repo [extras] baseurl=file:///extras gpgcheck=0 [new] baseurl=file:///new gpgcheck=0 [dvd] baseurl=file:///dvd gpgcheck=0
It is used to create the repository so that we can install the required Softwares later.
✔ Step 7:
# yum install python36-pip vim net-tools openssh-server libSM libXrender libXext python36 lspci lsusb
✔ Step 8:
# pip3.6 install opencv_python-126.96.36.199-cp36-cp36m-manylinux1_x86_64.whl numpy-1.16.2-cp36-cp36m-manylinux1_x86_64.whl --no-index -f .
✔ Step 9:
# cd /dev // To check camera is attached or not. # ls
✔ Step 10:
# xhost +local:root
To run GUI applications as the root.
✔ Step 11:
import cv2 import numpy cap=cv2.VideoCapture(0) # Used for video capturing from video files, image sequences or cameras, and 0 is the device name. ret,photo=cap.read() cv2.imshow('hi',photo) # cv2.imshow() method is used to display an image in a window. cv2.waitKey() # Waits for a pressed key. cap.release() # It will exit the camera.
Finally, write a program to click the photo in your docker container.
✔ Step 12:
# chmod +x photo.py # ./photo.py
Line 1 is used to make the python file executable.
Line 2 is used to execute the code.
So, that is the end of the task, I hope you learned a lot in this.
Happy learning! 😊✌
Interested in reading more such articles from Seema Saharan?
Support the author by donating an amount of your choice.