Docker: “How to setup Python Interpreter and run Python Code on Docker Container?”
Docker is a set of “platform as a service” products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries, and configuration files; they can communicate with each other through well-defined channels. ~Wikipedia
Docker Installation
To install Docker in your Operating System (Linux, Windows, or Mac), Please check out the official Docker documentation here.
Installing Docker on RHEL 8 in AWS
I’ve used RHEL 8 in AWS for Docker Installation. To install Docker we need to run the following command
yum install docker
The output will be
Red Hat Update Infrastructure 3 Client Configur 20 kB/s | 2.1 kB 00:00
Red Hat Enterprise Linux 8 for x86_64 - AppStre 17 kB/s | 2.8 kB 00:00
Red Hat Enterprise Linux 8 for x86_64 - BaseOS 15 kB/s | 2.4 kB 00:00
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
podman-docker noarch 2.0.5-5.module+el8.3.0+8221+97165c3f
rhel-8-appstream-rhui-rpms 44 k
Installing dependencies:
conmon x86_64 2:2.0.20-2.module+el8.3.0+8221+97165c3f
rhel-8-appstream-rhui-rpms 49 k
containernetworking-plugins
x86_64 0.8.6-2.module+el8.3.0+8221+97165c3f
rhel-8-appstream-rhui-rpms 20 M
containers-common x86_64 1:1.1.1-3.module+el8.3.0+8221+97165c3f
rhel-8-appstream-rhui-rpms 66 k
criu x86_64 3.14-2.module+el8.3.0+8221+97165c3f
rhel-8-appstream-rhui-rpms 500 k
fuse-overlayfs x86_64 1.1.2-3.module+el8.3.0+8221+97165c3f
rhel-8-appstream-rhui-rpms 67 k
fuse3-libs x86_64 3.2.1-12.el8 rhel-8-baseos-rhui-rpms 94 k
iptables x86_64 1.8.4-10.el8 rhel-8-baseos-rhui-rpms 581 k
libnet x86_64 1.1.6-15.el8 rhel-8-appstream-rhui-rpms 67 k
libnetfilter_conntrack x86_64 1.0.6-5.el8 rhel-8-baseos-rhui-rpms 65 k
libnfnetlink x86_64 1.0.1-13.el8 rhel-8-baseos-rhui-rpms 33 k
libnftnl x86_64 1.1.5-4.el8 rhel-8-baseos-rhui-rpms 83 k
libslirp x86_64 4.3.1-1.module+el8.3.0+8221+97165c3f
rhel-8-appstream-rhui-rpms 69 k
libvarlink x86_64 18-3.el8 rhel-8-baseos-rhui-rpms 44 k
nftables x86_64 1:0.9.3-16.el8 rhel-8-baseos-rhui-rpms 312 k
podman x86_64 2.0.5-5.module+el8.3.0+8221+97165c3f
rhel-8-appstream-rhui-rpms 13 M
podman-catatonit x86_64 2.0.5-5.module+el8.3.0+8221+97165c3f
rhel-8-appstream-rhui-rpms 308 k
policycoreutils-python-utils
noarch 2.9-9.el8 rhel-8-baseos-rhui-rpms 251 k
protobuf-c x86_64 1.3.0-4.el8 rhel-8-appstream-rhui-rpms 37 k
runc x86_64 1.0.0-68.rc92.module+el8.3.0+8221+97165c3f
rhel-8-appstream-rhui-rpms 3.7 M
slirp4netns x86_64 1.1.4-2.module+el8.3.0+8221+97165c3f
rhel-8-appstream-rhui-rpms 50 k
Installing weak dependencies:
container-selinux noarch 2:2.144.0-1.module+el8.3.0+8221+97165c3f
rhel-8-appstream-rhui-rpms 49 k
Enabling module streams:
container-tools rhel8Transaction Summary
================================================================================
Install 22 PackagesTotal download size: 40 M
Installed size: 136 M
Is this ok [y/N]: y
Downloading Packages:
(1/22): libnet-1.1.6-15.el8.x86_64.rpm 526 kB/s | 67 kB 00:00
(2/22): protobuf-c-1.3.0-4.el8.x86_64.rpm 281 kB/s | 37 kB 00:00
(3/22): fuse-overlayfs-1.1.2-3.module+el8.3.0+8 500 kB/s | 67 kB 00:00
(4/22): container-selinux-2.144.0-1.module+el8. 546 kB/s | 49 kB 00:00
(5/22): podman-docker-2.0.5-5.module+el8.3.0+82 485 kB/s | 44 kB 00:00
(6/22): slirp4netns-1.1.4-2.module+el8.3.0+8221 561 kB/s | 50 kB 00:00
(7/22): containers-common-1.1.1-3.module+el8.3. 671 kB/s | 66 kB 00:00
(8/22): libslirp-4.3.1-1.module+el8.3.0+8221+97 690 kB/s | 69 kB 00:00
(9/22): podman-catatonit-2.0.5-5.module+el8.3.0 2.8 MB/s | 308 kB 00:00
(10/22): conmon-2.0.20-2.module+el8.3.0+8221+97 455 kB/s | 49 kB 00:00
(11/22): containernetworking-plugins-0.8.6-2.mo 37 MB/s | 20 MB 00:00
(12/22): runc-1.0.0-68.rc92.module+el8.3.0+8221 12 MB/s | 3.7 MB 00:00
(13/22): criu-3.14-2.module+el8.3.0+8221+97165c 3.8 MB/s | 500 kB 00:00
(14/22): podman-2.0.5-5.module+el8.3.0+8221+971 20 MB/s | 13 MB 00:00
(15/22): fuse3-libs-3.2.1-12.el8.x86_64.rpm 741 kB/s | 94 kB 00:00
(16/22): libnfnetlink-1.0.1-13.el8.x86_64.rpm 280 kB/s | 33 kB 00:00
(17/22): libnetfilter_conntrack-1.0.6-5.el8.x86 556 kB/s | 65 kB 00:00
(18/22): policycoreutils-python-utils-2.9-9.el8 2.3 MB/s | 251 kB 00:00
(19/22): libvarlink-18-3.el8.x86_64.rpm 233 kB/s | 44 kB 00:00
(20/22): iptables-1.8.4-10.el8.x86_64.rpm 5.3 MB/s | 581 kB 00:00
(21/22): libnftnl-1.1.5-4.el8.x86_64.rpm 900 kB/s | 83 kB 00:00
(22/22): nftables-0.9.3-16.el8.x86_64.rpm 3.1 MB/s | 312 kB 00:00
--------------------------------------------------------------------------------
Total 29 MB/s | 40 MB 00:01
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : libnftnl-1.1.5-4.el8.x86_64 1/22
Running scriptlet: libnftnl-1.1.5-4.el8.x86_64 1/22
/sbin/ldconfig: /lib64/libhdfs.so.0 is not a symbolic link/sbin/ldconfig: /lib64/libhadoop.so.1 is not a symbolic link
Installing : libnfnetlink-1.0.1-13.el8.x86_64 2/22
Running scriptlet: libnfnetlink-1.0.1-13.el8.x86_64 2/22
/sbin/ldconfig: /lib64/libhdfs.so.0 is not a symbolic link/sbin/ldconfig: /lib64/libhadoop.so.1 is not a symbolic link
Installing : libnetfilter_conntrack-1.0.6-5.el8.x86_64 3/22
Running scriptlet: libnetfilter_conntrack-1.0.6-5.el8.x86_64 3/22
/sbin/ldconfig: /lib64/libhdfs.so.0 is not a symbolic link/sbin/ldconfig: /lib64/libhadoop.so.1 is not a symbolic link
Running scriptlet: iptables-1.8.4-10.el8.x86_64 4/22
Installing : iptables-1.8.4-10.el8.x86_64 4/22
Running scriptlet: iptables-1.8.4-10.el8.x86_64 4/22
/sbin/ldconfig: /lib64/libhdfs.so.0 is not a symbolic link/sbin/ldconfig: /lib64/libhadoop.so.1 is not a symbolic link
Installing : nftables-1:0.9.3-16.el8.x86_64 5/22
Running scriptlet: nftables-1:0.9.3-16.el8.x86_64 5/22
Installing : policycoreutils-python-utils-2.9-9.el8.noarch 6/22
Running scriptlet: container-selinux-2:2.144.0-1.module+el8.3.0+8221+ 7/22
Installing : container-selinux-2:2.144.0-1.module+el8.3.0+8221+ 7/22
Running scriptlet: container-selinux-2:2.144.0-1.module+el8.3.0+8221+ 7/22
Installing : libvarlink-18-3.el8.x86_64 8/22
Running scriptlet: libvarlink-18-3.el8.x86_64 8/22
/sbin/ldconfig: /lib64/libhdfs.so.0 is not a symbolic link/sbin/ldconfig: /lib64/libhadoop.so.1 is not a symbolic link
Installing : fuse3-libs-3.2.1-12.el8.x86_64 9/22
Running scriptlet: fuse3-libs-3.2.1-12.el8.x86_64 9/22
/sbin/ldconfig: /lib64/libhdfs.so.0 is not a symbolic link/sbin/ldconfig: /lib64/libhadoop.so.1 is not a symbolic link
Installing : fuse-overlayfs-1.1.2-3.module+el8.3.0+8221+97165c3 10/22
Installing : conmon-2:2.0.20-2.module+el8.3.0+8221+97165c3f.x86 11/22
Installing : libslirp-4.3.1-1.module+el8.3.0+8221+97165c3f.x86_ 12/22
Installing : slirp4netns-1.1.4-2.module+el8.3.0+8221+97165c3f.x 13/22
Installing : containers-common-1:1.1.1-3.module+el8.3.0+8221+97 14/22
Installing : containernetworking-plugins-0.8.6-2.module+el8.3.0 15/22
Installing : protobuf-c-1.3.0-4.el8.x86_64 16/22
Installing : libnet-1.1.6-15.el8.x86_64 17/22
Running scriptlet: libnet-1.1.6-15.el8.x86_64 17/22
/sbin/ldconfig: /lib64/libhdfs.so.0 is not a symbolic link/sbin/ldconfig: /lib64/libhadoop.so.1 is not a symbolic link
Installing : criu-3.14-2.module+el8.3.0+8221+97165c3f.x86_64 18/22
Installing : runc-1.0.0-68.rc92.module+el8.3.0+8221+97165c3f.x8 19/22
Installing : podman-catatonit-2.0.5-5.module+el8.3.0+8221+97165 20/22
Installing : podman-2.0.5-5.module+el8.3.0+8221+97165c3f.x86_64 21/22
Installing : podman-docker-2.0.5-5.module+el8.3.0+8221+97165c3f 22/22
Running scriptlet: container-selinux-2:2.144.0-1.module+el8.3.0+8221+ 22/22
Running scriptlet: podman-docker-2.0.5-5.module+el8.3.0+8221+97165c3f 22/22
/sbin/ldconfig: /lib64/libhdfs.so.0 is not a symbolic link/sbin/ldconfig: /lib64/libhadoop.so.1 is not a symbolic link
Verifying : libnet-1.1.6-15.el8.x86_64 1/22
Verifying : protobuf-c-1.3.0-4.el8.x86_64 2/22
Verifying : fuse-overlayfs-1.1.2-3.module+el8.3.0+8221+97165c3 3/22
Verifying : container-selinux-2:2.144.0-1.module+el8.3.0+8221+ 4/22
Verifying : podman-docker-2.0.5-5.module+el8.3.0+8221+97165c3f 5/22
Verifying : slirp4netns-1.1.4-2.module+el8.3.0+8221+97165c3f.x 6/22
Verifying : containernetworking-plugins-0.8.6-2.module+el8.3.0 7/22
Verifying : containers-common-1:1.1.1-3.module+el8.3.0+8221+97 8/22
Verifying : libslirp-4.3.1-1.module+el8.3.0+8221+97165c3f.x86_ 9/22
Verifying : podman-catatonit-2.0.5-5.module+el8.3.0+8221+97165 10/22
Verifying : podman-2.0.5-5.module+el8.3.0+8221+97165c3f.x86_64 11/22
Verifying : conmon-2:2.0.20-2.module+el8.3.0+8221+97165c3f.x86 12/22
Verifying : runc-1.0.0-68.rc92.module+el8.3.0+8221+97165c3f.x8 13/22
Verifying : criu-3.14-2.module+el8.3.0+8221+97165c3f.x86_64 14/22
Verifying : fuse3-libs-3.2.1-12.el8.x86_64 15/22
Verifying : libnfnetlink-1.0.1-13.el8.x86_64 16/22
Verifying : libnetfilter_conntrack-1.0.6-5.el8.x86_64 17/22
Verifying : libvarlink-18-3.el8.x86_64 18/22
Verifying : policycoreutils-python-utils-2.9-9.el8.noarch 19/22
Verifying : iptables-1.8.4-10.el8.x86_64 20/22
Verifying : libnftnl-1.1.5-4.el8.x86_64 21/22
Verifying : nftables-1:0.9.3-16.el8.x86_64 22/22Installed:
conmon-2:2.0.20-2.module+el8.3.0+8221+97165c3f.x86_64
container-selinux-2:2.144.0-1.module+el8.3.0+8221+97165c3f.noarch
containernetworking-plugins-0.8.6-2.module+el8.3.0+8221+97165c3f.x86_64
containers-common-1:1.1.1-3.module+el8.3.0+8221+97165c3f.x86_64
criu-3.14-2.module+el8.3.0+8221+97165c3f.x86_64
fuse-overlayfs-1.1.2-3.module+el8.3.0+8221+97165c3f.x86_64
fuse3-libs-3.2.1-12.el8.x86_64
iptables-1.8.4-10.el8.x86_64
libnet-1.1.6-15.el8.x86_64
libnetfilter_conntrack-1.0.6-5.el8.x86_64
libnfnetlink-1.0.1-13.el8.x86_64
libnftnl-1.1.5-4.el8.x86_64
libslirp-4.3.1-1.module+el8.3.0+8221+97165c3f.x86_64
libvarlink-18-3.el8.x86_64
nftables-1:0.9.3-16.el8.x86_64
podman-2.0.5-5.module+el8.3.0+8221+97165c3f.x86_64
podman-catatonit-2.0.5-5.module+el8.3.0+8221+97165c3f.x86_64
podman-docker-2.0.5-5.module+el8.3.0+8221+97165c3f.noarch
policycoreutils-python-utils-2.9-9.el8.noarch
protobuf-c-1.3.0-4.el8.x86_64
runc-1.0.0-68.rc92.module+el8.3.0+8221+97165c3f.x86_64
slirp4netns-1.1.4-2.module+el8.3.0+8221+97165c3f.x86_64 Complete!
It’s too long…
Pulling OS Image
Pulling the Operating System in Docker refers to Installing an Operating System as a container in Docker over Host Machine. In Docker, we can set up multiple Docker Images (OS) in a single host Operating System.
In my case, I am using CentOS as a Docker Image. To install CentOS in Docker, we need to follow the below steps
Step 1) First, in Console, we need to run the following command to pull the CentOS image into Docker.
docker pull centos:latest
In this command, I’ve pulled CentOS Latest version.
The output will be
Emulate Docker CLI using podman. Create /etc/containers/nodocker to quiet msg.
Trying to pull registry.access.redhat.com/centos:latest...
name unknown: Repo not found
Trying to pull registry.redhat.io/centos:latest...
unable to retrieve auth token: invalid username/password: unauthorized: Please login to the Red Hat Registry using your Customer Portal credentials. Further instructions can be found here: https://access.redhat.com/RegistryAuthentication
Trying to pull docker.io/library/centos:latest...
Getting image source signatures
Copying blob 3c72a8ed6814 done
Copying config 0d120b6cca done
Writing manifest to image destination
Storing signatures
0d120b6ccaa8c5e149176798b3501d4dd1885f961922497cd0abef155c869566
Step 2) Then, we start the new CentOS container by running the following command
docker run -it --name shobhitCentOS centos:latest
In this command, I’ve used “shobhitCentOS” as my docker OS name and CentOS latest version as Image.
The output will be
Emulate Docker CLI using podman. Create /etc/containers/nodocker to quiet msg.
and finally, we will see this output showing a new docker image terminal is started.
[root@95019aeaa8d6 /]#
Setup Python Interpreter and running Python Program on CentOS in Docker
In this section, I’ll set up the python interpreter and run a simple python program. To perform this operation we need to follow the below steps
First of all, we need to install Python (if not installed) by using the following command
yum install python3
“python3” is for Python version 3.
The output will be
Failed to set locale, defaulting to C.UTF-8
CentOS-8 - AppStream 12 MB/s | 5.8 MB 00:00
CentOS-8 - Base 4.3 MB/s | 2.2 MB 00:00
CentOS-8 - Extras 39 kB/s | 8.1 kB 00:00
Dependencies resolved.
================================================================================
Package Arch Version Repo Size
================================================================================
Installing:
python36 x86_64 3.6.8-2.module_el8.1.0+245+c39af44f AppStream 19 k
Installing dependencies:
platform-python-pip noarch 9.0.3-16.el8 BaseOS 1.8 M
python3-pip noarch 9.0.3-16.el8 AppStream 19 k
python3-setuptools noarch 39.2.0-5.el8 BaseOS 162 k
Enabling module streams:
python36 3.6Transaction Summary
================================================================================
Install 4 PackagesTotal download size: 2.0 M
Installed size: 7.8 M
Is this ok [y/N]: y
Downloading Packages:
(1/4): python3-pip-9.0.3-16.el8.noarch.rpm 370 kB/s | 19 kB 00:00
(2/4): python3-setuptools-39.2.0-5.el8.noarch.r 2.7 MB/s | 162 kB 00:00
[MIRROR] python36-3.6.8-2.module_el8.1.0+245+c39af44f.x86_64.rpm: Status code: 404 for http://d36uatko69830t.cloudfront.net/centos/8/AppStream/x86_64/os/Packages/python36-3.6.8-2.module_el8.1.0+245+c39af44f.x86_64.rpm (IP: 13.33.155.222)
(3/4): platform-python-pip-9.0.3-16.el8.noarch. 10 MB/s | 1.8 MB 00:00
(4/4): python36-3.6.8-2.module_el8.1.0+245+c39a 51 kB/s | 19 kB 00:00
--------------------------------------------------------------------------------
Total 3.3 MB/s | 2.0 MB 00:00
warning: /var/cache/dnf/AppStream-02e86d1c976ab532/packages/python3-pip-9.0.3-16.el8.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS-8 - AppStream 1.6 MB/s | 1.6 kB 00:00
Importing GPG key 0x8483C65D:
Userid : "CentOS (CentOS Official Signing Key) <security@centos.org>"
Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
Is this ok [y/N]: y
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : python3-setuptools-39.2.0-5.el8.noarch 1/4
Installing : platform-python-pip-9.0.3-16.el8.noarch 2/4
Installing : python36-3.6.8-2.module_el8.1.0+245+c39af44f.x86_64 3/4
Running scriptlet: python36-3.6.8-2.module_el8.1.0+245+c39af44f.x86_64 3/4
Installing : python3-pip-9.0.3-16.el8.noarch 4/4
Running scriptlet: python3-pip-9.0.3-16.el8.noarch 4/4
Verifying : python3-pip-9.0.3-16.el8.noarch 1/4
Verifying : python36-3.6.8-2.module_el8.1.0+245+c39af44f.x86_64 2/4
Verifying : platform-python-pip-9.0.3-16.el8.noarch 3/4
Verifying : python3-setuptools-39.2.0-5.el8.noarch 4/4Installed:
platform-python-pip-9.0.3-16.el8.noarch
python3-pip-9.0.3-16.el8.noarch
python3-setuptools-39.2.0-5.el8.noarch
python36-3.6.8-2.module_el8.1.0+245+c39af44f.x86_64Complete!
CREATING Sample Python Program
After successful installation of Python, we can use the online interpreter to run a python program or functions or can save a file and then execute it.
Open python using the following command to run a sample program using the online interpreter
python3
The interpreter will look like
Python 3.6.8 (default, Apr 16 2020, 01:36:27)
[GCC 8.3.1 20191121 (Red Hat 8.3.1-5)] on linux
Type "help", "copyright", "credits" or "license" for more information.
And Finally running the sample print function
>>> print("I am Shobhit! Working on CENTOS in Docker")
I am Shobhit! Working on CENTOS in Docker
After hitting enter, this will be the output
I am Shobhit! Working on CENTOS in Docker
It is successfully executed…
Finally, the conclusion is that we can run a python interpreter on Linux in Hadoop over Host operating system.
…
This article is written, edited, and published by Shobhit Sharma