Docker is a tool that makes it easier to create, deploy, and run applications by using containers. These containers allow packaging an application with needed parts such as libraries and dependencies, and deploy it as a single package. In this simple walkthrough, we’ll try to launch a centos container using docker and then install and run python files in that container. I have used Red Hat Enterprise Linux 8 as my base OS here.
In RHEL8, we need to enable a repository to install packages. To install Docker Comunity Edition (docker-ce) we need to first enable the docker-ce repository, which can be done by running the command,
dnf config-manager — add-repo=https://download.docker.com/linux/centos/docker-ce.repo
We can now install docker by running the command
dnf install docker-ce --nobest ( the
--nobest option is to get the version of docker-ce without any dependency errors).
We can run
rpm -q docker-ce to check if docker is installed, start the service using
systemctl start docker , and run
systemctl status docker to check the status of the service.
Pulling an image
To create a container, we need an image as a base. Here I’m using centos as my base image and to get this image run the command,
docker pull centos.
Before we create the container, we need to stop the system firewall so that the container can access the public network. We do this by running
systemctl stop firewalld.
Running a container
To run a container using the pulled image, use the command
docker run -it name pycontainer centos:latest(the name
pycontainer can be replaced with any name). This command launches an interactive pseudo terminal for the container.
Running python code
Yum/dnf is preconfigured with the AppStream and BaseOS repos, thus we can directly install python using yum:
yum install python38.
Open a separate terminal tab for the host and run docker cp to copy the desired python files from the host to the container:
docker cp <src_path> pycontainer:<dest_path>. Here, I’m copying a dataset and Python program that trains a LinearRegression model based on that dataset to predict profits.
Depending on the python program, there might be libraries that you need to install. In the program I’m using I need the pandas, numpy, and scikit-learn python libraries. These libraries can be installed using pip3 in the container terminal:
pip3 install <library>.
We can now run the python file using the command,
That’s all there is to this simple task. I Hope it was informative.