I’m probably not the only one guilty of a “Pull and Pray.” You know, when you clone your application using git, install the dependencies with pip, and pray it all works out. You’re asking for all kinds of trouble though. Bloated deployment time; errors in the build; Not to mention the app may not even be the same server-to-server.
What you need – what we all want – is good up-time and scale for everything. Especially for large deployments.
You want CI/CD for Python.
You won’t often hear the two spoken in the same sentence, but the principles and efficiency of CI/CD can 100% apply to Python development. In this article we’ll look at applying CI/CD to Python using Docker and BuildMaster, avoiding all the pitfalls of “Pulling and Praying”.
The Hurdles of Python Deployment
We already know that using requirements.txt is pretty much a no-brainer when it comes to consistent builds. It’s ability to make sure dependencies in your development remain consistent eliminates most of the headaches of “pulling and praying” But it doesn’t solve all your problems.
Even using requirements.txt comes with it’s caveats.
pip install is slow and may even fail in some cases. What’s more, it may run differently due to operating system configurations. This can pose great issues to a production deployment, such as prolonged deployment time, increased downtime, and potential disruptions
This is where Docker comes to the rescue. I know what you’re thinking… intimidating?
Not to worry, I’ll talk you through it.
So, What is Docker Exactly?
Ok, quick 101. Docker is a platform designed to make it easier to develop, deploy, and run applications by using containers. So, what’s a container? Imagine it as a lightweight, standalone, executable package that includes everything needed to run a piece of software. It includes code, runtime, libraries, and system tools, encapsulating an application and its dependencies.
There are legit reasons Docker is so widely used:
✔ All in One: Containers package an application and its dependencies into a single unit. This avoids the classic problem of “it works on my machine”. It guarantees that what works on a developer’s machine will also work on a tester’s or production server.
✔ Environment Consistency: With Docker, the application’s environment is consistent, regardless of where it runs. This eliminates unexpected behavior caused by differences between development and production environments.
✔ Simple Deployment: Docker simplifies the deployment process. Once an application is containerized, it can be easily moved and executed on any system that supports Docker, streamlining the deployment pipeline.
Sound good? It is, except for just one thing — Using it means everyone in your team suddenly needs a PhD in Docker. Getting to grips with it can be a bit much, especially on top of Git and everything else your team is working with.
Thankfully, BuildMaster takes care of this so you don’t have to.
BuildMaster: Containerize without Learning Docker
BuildMaster lets you containerize applications as part of the build/deploy process without everyone on the team needing to become a Docker expert.
This means that your developers can:
⭐ Use their preferred tools to code, build, test, and debug applications
⭐ Not worry about installing a Docker Engine on their desktop, which is often resource-intensive and slows things down
⭐ Work with Windows without having to context-switch to Linux tools
Besides that, there are a lot of other benefits to containerizing your application, such as improved portability, easier scalability, enhanced isolation, and simplified deployment and management processes
Creating a Dockerized Application in BuildMaster
Containerizing your applications is quick and easy in BuildMaster, and involves creating, building, and finally containerizing the application.
When creating a new application, BuildMaster will help connect your application to the repository with your Python application code.
You’ll then have the choice to deploy your application as a Docker image, and then set additional configuration options.
BuildMaster will then help you “build” the application. At least, “build” is what it’s called here. What BuildMaster is basically doing is running
pip install and running tests. This is easily done by using a simple build script template, which will also create files needed when deploying/releasing the application, called the “Build Artifacts”. This script can also run unit tests or static analysis as needed.
Note that BuildMaster will automatically run
pip install. This will let you verify the “Build Artifacts” before containerizing them.
After that BuildMaster will create a container image using the build artifacts created. You can do this easily using the “Build Docker Image” script template. This uses the “Build Artifacts” to build the Docker image, which will then push the new image to your Docker registry and associate it with your build.
To complete the Docker Image script, you’ll also need a “Dockerfile”. A Dockerfile is a script that instructs Docker how to build a container image for your application. If you haven’t worked with Docker before, the syntax can be a bit intimidating
BuildMaster helps here by managing the file for you. So, once you get it working, you don’t have to ever worry about using or mastering Docker.
Creating a Basic Docker File for Python
So, let’s look at some best practices for writing a basic Docker file for Python. It might seem scary at first but I assure you it’s pretty straightforward –the main thing is to keep it simple.
Make sure you first have a directory with all of your Python files, including the main script or application file and the requirements.txt. You’ll now need to create a file named Dockerfile in this directory with the following content:
# creates a layer from the ubuntu:22.04 Docker image. FROM ubuntu:22.04 # adds files from your Docker client's current directory. COPY . /app # builds your application with make. RUN make /app # specifies what command to run within the container CMD python /app/app.py
Here is a detailed breakdown of their Dockerfiles if you’re still having trouble.
Now you have your Dockerfile, simply running the “Build Docker Image” script you created will create your Dockerized App. Easy!
Deploying Your Dockerized Application the Easy Way
So now you have a Dockerized Application ready, you’ll want to get it deployed.
Your Application in a CI/CD Pipeline
When creating a dockerized application, BuildMaster will work through a Docker CI/CD pipeline. Pipelines are repeatable release processes that define the servers and environments your application will be deployed to.
The Docker pipeline includes five stages:
The first two steps, “Build” and “Containerize” will have been worked through when containerizing the application. The next steps will be the “Integration”, “Testing”, and “Production” stages where BuildMaster will deploy the application by pulling the container image from the Docker registry and then running it on our host.
Deploying your Application With Docker Run Config
A “Docker Run Config” is a type of file used when deploying dockerized applications. When deploying a Docker container, you have to deal with a bunch of complex tasks such as mapping volumes, configuring ports, and setting up environment variables. BuildMaster streamlines all of this with the “Docker Run Config” file.
Creating one is just a simple case of following the setup steps in BuildMaster
A “Docker Run Config” file has multiple “instances” with different configurations based on the environment you’ll deploy to. You can control who has access to view or edit these instances based on the environment.
The easiest way to deploy a container image is to use this file with a “Deploy Docker Image” Script Template to deploy the Docker image associated with the current build.
Once you deploy the current build it will go through various environments in the pipeline (Integration, Testing, and Production). You’ll have the option to customize each of these environments, such as creating checks and approvals at each stage, deciding if the build moves to each stage manually or automatically, and specifying where you’ll be deploying to.
Move Away from Pull & Pray
Steering clear of the unreliable “Pull and Pray” to CI/CD approach for Python applications can improve your development by:
🚀 Streamlining the development process
🚀 Shortening deployment time
🚀 Eliminating errors in the build
By incorporating Docker and BuildMaster, containerization is simplified, offering an efficient deployment process.
Ready to experience a paradigm shift in Python deployment? Start your free trial of BuildMaster today! And if you’re looking for more ways to enhance your Python development, why not sign up for our free “Python for the Enterprise” eBook!