Imagine that you created amazing Python app and want to share it with the world. Or maybe you developed a weather app using Python that provides real-time weather data for any place. You first share it with your friends and wait for their feedback on the app. However, things do not go as planned as:
- Some friends have different OS versions than yours which leads to compatibility issues.
- Some have the same version, however do not have the dependencies installed that are required by your application.
Now you cannot ask everyone to download the same OS version as yours and the dependencies. People just want to install an app and start using it directly. So how to tackle this problem?
This is where Docker comes into play. Docker is an amazing tool that allows you to deploy your application and let the users use that application without any issues. Today, we will understand what Docker is, create one simple Python app using Docker, and ultimately deploy it.
So in this blog, we are going to cover the following topics:
Table of Contents
What is Docker?
Docker is a free software developed by Docker Inc. It allows users to create independent and isolated environments to launch and deploy their applications. These environments are then called containers. A container is a lightweight, standalone, and executable package that includes everything needed to run the software, such as code, libraries, and dependencies.
Docker is a powerful tool that allows developers to package and distribute applications along with their dependencies in a consistent and isolated environment. Hence, when you run that Docker image, your application uses the OS within that environment. This allows users to run your application on any OS.
Enough of the theory! Now let us create our first application.
[The steps are simple and should work without any problem. However, if you happen to face any error or have difficulty with any step, feel free to DM me on Instagram @machinelearningsite.]
Deploying Python app using Docker
Before we get started with our project, you need to create an account on Docker Hub as you’ll be uploading your application over there [At least you’ll have an account on some “HUB” ;)]. After creating an account, create a repo as that will hold our Docker image. You can name it whatever you want but I suggest naming it “myapp” for convenience. We will need it later when we push our python app.
Step 1: Create Project Structure
1. Create a new folder for your project.
2. Inside the project folder, create a file named app.py
. This is our main file that will contain the source code.
3. Create another file called Dockerfile
in the same folder. This file has no extension and contains instructions for building a Docker environment.
Step 2: Write a Simple Python Program
Open the app.py file and write a simple Python program. For this exercise, we will create an application that takes in input from the users and outputs its factorial.
def factorial(n):
"""
Calculate the factorial of a given number.
Parameters:
- n: The number for which to calculate the factorial.
Returns:
- The factorial of the input number.
"""
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
def main():
# Get user input for the number
num = int(input("Enter a number to calculate its factorial: "))
# Check if the input is non-negative
if num < 0:
print("Factorial is not defined for negative numbers.")
else:
# Calculate and display the factorial
result = factorial(num)
print(f"The factorial of {num} is: {result}")
if __name__ == "__main__":
main()
Step 3: Configure the Dockerfile
Open the Dockerfile
in a text editor. Write the following lines to configure the Docker image:
FROM python:2.7
WORKDIR /app
COPY . /app
CMD ["python", "app.py"]
Understanding the code
FROM python:2.7
=> This sets the base image as an official Python image. The FROM instruction specifies what kind of image we want to create. We can pull pre-built images from DockerHub, including full-scale Operating Systems. Let us say that our Python app requires the old version, i.e., Python 2.7. Hence, in this case, the resulting image will have Python 2.7 installed as the base environment.
WORKDIR /app
=> This specifies the working directory in the container’s filesystem. Think of this as the path where your image will sit and run in the container.
COPY . /app
=> This copies the contents of the current directory into the /app directory within the container. The COPY instruction lets you copy files from your machine to the image. The syntax is COPY <source> <target>, where <source> is the file path on your local machine and <target> is the file path on the container starting from the working directory.
CMD [“python”, “app.py”]
=> This specifies that the ‘app.py’ script should be executed using the Python interpreter when a container is started.
Step 4: Build the Docker Image
Open a terminal or command prompt in the folder that contains the Dockerfile
. Run the following command to build the Docker image:
docker build -t myapp .
This command builds an image named myapp
using the current directory (.) as the build context.
Step 5: Tagging your Docker image
Tagging is used when you deploy different versions of your app. For instance, you can use the tag “latest” which suggests that it is the latest version of your app.
docker tag myapp {your-repo}/myapp:latest
Step 6: Testing your Python app
Now that we have built and tagged our project, we will run it using the following command.
docker run {your-repo}/mypythonapp:latest
Step 7:
Now that everything is running nicely, we are ready to make it available to the world!
docker push {your-repo}/myapp:latest
And DONE! Your app is out there and everyone can use your app. All the users have to do is “pull” your Python app from the “HUB” and run it on their computers:
docker pull {your-repo}/myapp
Summary
In this small exercise, we created and presented our own Python app and made it available to everyone using Docker. I hope you were able to successfully deploy the app. However, as I mentioned above if you had any difficulty in any step, feel free to write me a mail kolbenkraft.kk@gmail.com OR much better, DM me on Instagram @machinelearningsite.
If you successfully deployed your application, share it on Instagram and tag me in it. I would be happy to see your application. For more interesting posts, follow me on social media: