Introduction to Dockerization
Dockerization is the process of creating a Docker container that encapsulates our application along with its runtime environment and dependencies. This approach ensures consistency and reproducibility, making it easier to share and deploy applications across different systems.
Prerequisites
Before we begin, make sure you have the following installed on your system:
Docker: Download and install Docker Desktop or Docker Engine depending on your operating system.
Understand Python and have it installed on your machine.
That being said, let me proceed by telling you a short story. I was assigned to lead my team to achieve the following:
1- "Make sure everyone has Docker Desktop or any other alternatives installed.
2- Create a simple Dockerfile that has busybox/alpine as base image.
3- And from this dockerfile I want you all to containerize a Python application.
The application is an application written with Python called a test.py. It has a simple print "hello world" functionality.
As seamless as that may look, I will share with you the difficulties I ran into when trying to lead my team to success in this task.
As a leader working in a team, the first thing I did was to create a github repo and invite my colleagues to dine with me to collectively solve the problem. Then we decided to write the Dockerfile and build the image from the command.
Below is the snippet of the Dockerfile we created and I will tell you why you should avoid it.
FROM alpine:latest
WORKDIR /app
ADD https://raw.githubusercontent.com/adefemi171/test-malhub/main/test.py .
RUN chmod +x test.py
CMD ["./test.py"]
If you are not familiar with Docker, don't worry, I will guide you through it. Just bear with me. The above Dockerile you see is the basic INSTRUCTIONS we provide Docker with its respective arguments to achieve our goal. Followingwhich we will then use the below command to build and run it respectively:
docker build -t python-alpine-container .
docker run python-alpine-container
Under normal circumstances, these codes should work well, but they did not. You might have guessed why? Well, if you could not guess, no issues, that is why I am here. Let's do some troubleshooting.
However, before we get to the troubleshooting, let us have an overview of what Docker is. And why you should learn it.
Docker in Simple Terms:
Docker is like a virtual container for software applications. It bundles together everything an app needs to run, like code, libraries, and settings, into a single package. This makes it easy to move and run the app consistently on different computers or servers.
Docker in Simple Terms: Docker is like a virtual container for software applications. It bundles together everything an app needs to run, like code, libraries, and settings, into a single package. This makes it easy to move and run the app consistently on different computers or servers.
Troubleshooting Our Dockerfile:
However, before doing that, we should see to it that we have a simple python app saved in the same directory with the Dockerfile as demonstrated in the picture below:
The Dockerfile is based on the alpine:latest
image, which is a minimalist Linux distribution. It doesn't include Python by default, which is why we were getting an error when Docker tries to run the Python executable (try and run the command).
To solve this, what we need to is to modify our Dockerfile to install Python. Note that in Alpine Linux, packages are installed with the apk
command (Alpine Package Keeper). Here's how we can update our Dockerfile to include Python henceforth:
FROM alpine:latest
# Update apk repositories
RUN apk update
# Install Python
RUN apk add --no-cache python3 py3-pip
# Create a working directory
WORKDIR /app
# Copy your Python script to the Docker image
COPY test.py .
# Run the Python script
CMD ["python3", "test.py"]
This Dockerfile updates the apk
repositories with apk update
and installs Python with apk add --no-cache python3 py3-pip
. The --no-cache
option is used to keep the image size small.
Now if we build and run our Docker image, we should see our Python script execute successfully.
To build the Docker image, use the following command in the directory of the Dockerfile:
docker build -t my-python-app .
To run the Docker image, use the following command:
docker run my-python-app
So finally, since my Python script is written to show gratitude to my mentor, I will share a screenshot of it after running the Dockerfile below:
Before the concluding remarks, I will briefly give you five (5) key reasons why you should learn Docker:
Docker in Simple Terms: Docker is like a virtual container for software applications. It bundles together everything an app needs to run, like code, libraries, and settings, into a single package. This makes it easy to move and run the app consistently on different computers or servers.
Importance of Learning Docker: Learning Docker is important because it revolutionizes how software is developed and deployed. Here's why:
Consistency: Docker ensures that your app runs the same way on any environment, reducing "it works on my machine" issues.
Isolation: Docker containers isolate apps from each other and the underlying system, preventing conflicts and making them more secure.
Efficiency: Containers share the host OS's resources, making better use of hardware and enabling more efficient scaling.
Portability: You can create a container locally, and then easily move and run it on different cloud services or servers without compatibility worries.
DevOps: Docker plays a key role in DevOps practices, allowing developers and operations teams to work together smoothly, automate deployments, and improve collaboration.
Conclusion
Congratulations! You have successfully dockerized a simple Python application using Docker containers. This approach ensures that your application and its dependencies are isolated, reproducible, and can be easily deployed across different environments.
Docker provides a powerful platform for managing applications, and this guide has provided a foundational understanding of how to dockerize a Python application. As you explore Docker further, you'll discover even more advanced features and techniques to enhance your development and deployment workflows.
If you find this article useful or have constructive criticism or any suggestions, be kind to comment below and let us share ideas.