Python is a popular general-purpose programming language used for a wide variety of applications. It is used by software developers as a support language for build control and management, testing, and other purposes.

Docker is an open-source containerized platform that allows you to run your application in a lightweight container. This way your application runs in exactly the same manner as it will in production using the same operating system.

In this post, we will show you how to dockerize Python applications with Miniconda on CentOS 8.

Step 1 – Install Docker CE

By default, the latest version of Docker CE is not included in the CentOS 8 default repo, so you will need to add a repo from Docker’s official website.

You can add it with the following command:

dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo

Once the repo is created, run the following command to install the Docker CE on your system.

dnf install docker-ce --nobest

Once the installation is finished, start the Docker service and enable it to start at system reboot with the following command:

systemctl start docker
systemctl enable docker

Next, verify the installed version of Docker with the following command:

docker --version

You should get the following output:

Docker version 20.10.5, build 55c4c88

Step 2 – Create a Docker File to Deploy Python App

In this section, we will create a directory for the Python project and create a Dockerfile with all required information to deploy the Python app.

First, create a directory with the following command:

mkdir project

Next, change the directory to project and create a Dockerfile with the following command:

cd project
nano Dockerfile

Add the following lines:

FROM python:slim
RUN apt-get update && apt-get -y upgrade \
  && apt-get install -y --no-install-recommends \
    git \
    wget \
    g++ \
    ca-certificates \
    && rm -rf /var/lib/apt/lists/*
ENV PATH="/root/miniconda3/bin:${PATH}"
ARG PATH="/root/miniconda3/bin:${PATH}"
RUN wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh \
    && mkdir /root/.conda \
    && bash Miniconda3-latest-Linux-x86_64.sh -b \
    && rm -f Miniconda3-latest-Linux-x86_64.sh \
    && echo "Running $(conda --version)" && \
    conda init bash && \
    . /root/.bashrc && \
    conda update conda && \
    conda create -n python-app && \
    conda activate python-app && \
    conda install python=3.6 pip && \
    echo 'print("Hello World!")' > python-app.py
RUN echo 'conda activate python-app \n\
alias python-app="python python-app.py"' >> /root/.bashrc
ENTRYPOINT [ "/bin/bash", "-l", "-c" ]
CMD ["python python-app.py"]

Save and close the file when you are finished.

The above file will download Python minimal image, install Miniconda, create a Python environment, and create a simple Hello World application.

Step 3 – Build the Python Application Image

Now, you will need to build the image using the Dockerfile that we have created in the previous step.

First, change the directory to project with the following command:

cd project

Next, build the Python application by running the following command:

docker build -t python-app .

You should get the following output:

Downloading and Extracting Packages
setuptools-52.0.0    | 724 KB    | ########## | 100% 
certifi-2020.12.5    | 140 KB    | ########## | 100% 
python-3.6.13        | 29.7 MB   | ########## | 100% 
pip-21.0.1           | 1.8 MB    | ########## | 100% 
Preparing transaction: ...working... done
Verifying transaction: ...working... done
Executing transaction: ...working... done
Removing intermediate container 10f97804ad82
 ---> 3662950574f9
Step 6/8 : RUN echo 'conda activate python-app \nalias python-app="python python-app.py"' >> /root/.bashrc
 ---> Running in faab9e188650
Removing intermediate container faab9e188650
 ---> 8ee98d205c5c
Step 7/8 : ENTRYPOINT [ "/bin/bash", "-l", "-c" ]
 ---> Running in 3e74eb8fd8b7
Removing intermediate container 3e74eb8fd8b7
 ---> 6c6d83dfd01f
Step 8/8 : CMD ["python python-app.py"]
 ---> Running in 4d8c066aeefc
Removing intermediate container 4d8c066aeefc
 ---> 78e4f8e7e05e
Successfully built 78e4f8e7e05e
Successfully tagged python-app:latest

You can now check the downloaded images with the following command:

docker images

You should see all images in the following output:

REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
python-app   latest    78e4f8e7e05e   24 seconds ago   907MB
python       slim      ce689abb4f0d   10 days ago      118MB

Step 4 – Launch the Container

At this, point the Python application image is ready. You can now launch the container from the python-app image with the following command:

docker run python-app

You should see the output of your python application in the following output:

Hello World!

You can also connect to your Python application container and run the application.

To do so, run the container again with the following command:

docker run -it python-app /bin/bash

You will be connected to the python-app container as shown below:

(python-app) root@42e627b899fe:/#

Now, run your Python Hello World application using the following command:

(python-app) root@42e627b899fe:/# python-app

You should get the following output:

Hello World!

You can also verify your Python version using the following command:

(python-app) root@42e627b899fe:/# python --version

Output:

Python 3.6.13 :: Anaconda, Inc.

You can now exit from the container with the following command:

(python-app) root@42e627b899fe:/# exit

Conclusion

In the above guide, you learned how to dockerize a Python application with Miniconda on CentOS 8. Now you can deploy your Python application in a lightweight and containerized environment on your VPS hosting account from Atlantic.Net.