Tools: How to Containerize Your Python App and Deploy to VPS (2026)

Tools: How to Containerize Your Python App and Deploy to VPS (2026)

Why Containerize Your Python App?

Understanding Docker

What is a Docker Image?

What is a Docker Container?

Getting Started with Docker

Creating a Dockerfile

Creating requirements.txt

Building the Docker Image

Running the Docker Container Locally

Deploying to a VPS

Setting Up Your VPS

Transferring Your Application

Building the Image on the VPS

Running the Container on the VPS

Considerations for Production

Port Mapping and Firewalls

Reverse Proxy (Nginx/Apache)

Persistent Storage

Container Orchestration

Conclusion Are you tired of the "it works on my machine" problem when deploying your Python applications? Containerization offers a solution by packaging your application and its dependencies into a portable unit. This article will guide you through containerizing your Python app using Docker and deploying it to a Virtual Private Server (VPS). Deploying applications can be a complex process. Different environments often have varying system libraries, Python versions, or installed packages, leading to unexpected errors. Containerization, using tools like Docker, solves this by bundling your application with everything it needs to run. This ensures consistency across development, testing, and production environments. Think of a container like a self-contained apartment. It has its own plumbing, electricity, and furniture – everything needed for a resident to live comfortably, without interfering with other apartments or the building's main infrastructure. Similarly, a Docker container includes your application, its runtime, system tools, libraries, and settings, all isolated from the host system and other containers. Docker is an open-source platform that automates the deployment, scaling, and management of applications using containers. It provides a standardized way to package applications, ensuring they run reliably across different computing environments. A Docker image is a lightweight, standalone, executable package that includes everything needed to run a piece of software, including the code, runtime, system tools, system libraries, and settings. Images are immutable, meaning they cannot be changed after they are built. A Docker container is a runnable instance of a Docker image. You can create, start, stop, and delete containers. A container is the actual running process. Before you can containerize your Python app, you need to install Docker on your development machine. You can find installation instructions for various operating systems on the official Docker website. Once Docker is installed, you can start building your container. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Docker reads this file and executes the commands sequentially to build the image. Let's create a simple Flask application to demonstrate. This is a basic Flask web application that will serve a simple greeting. The host='0.0.0.0' is crucial for allowing the application to be accessible from outside the container. Now, let's create our Dockerfile. Let's break down this Dockerfile: You'll need a requirements.txt file listing your Python dependencies. For our example, it would be: Navigate to your project directory in your terminal (the one containing app.py, Dockerfile, and requirements.txt). Then, build the Docker image using the following command: This command will execute the steps in your Dockerfile, download the base image, copy your code, install dependencies, and create your custom image. Before deploying, it's a good practice to test your container locally. Now, open your web browser and go to http://localhost:5000. You should see "Hello from my containerized Python app!". To deploy your application to the internet, you'll need a Virtual Private Server (VPS). A VPS is a virtual machine sold as a service by an internet hosting service. It provides dedicated resources like CPU, RAM, and storage, giving you more control than shared hosting. When choosing a VPS provider, consider factors like performance, pricing, and support. Providers like PowerVPS offer competitive pricing and robust infrastructure, making them a good option for hosting containerized applications. Another excellent choice is Immers Cloud, which provides flexible plans suitable for various deployment needs. For a comprehensive overview of server rental options, the Server Rental Guide is a valuable resource. Install Docker: Install Docker on your VPS. The installation process will vary slightly depending on your VPS's operating system. For Ubuntu, you can typically use: It's also a good idea to add your user to the docker group to run Docker commands without sudo: You can transfer your application files and Dockerfile to the VPS using scp or by cloning your Git repository. Replace /path/to/your/app/directory with the actual path on your local machine, user with your VPS username, and your_vps_ip with your VPS's IP address. Once your files are on the VPS, navigate to your application directory and build the Docker image: Now, run your container on the VPS. You'll want to map a port on the VPS to your container's port. For public accessibility, you'll typically use port 80 (HTTP) or 443 (HTTPS). Now, you should be able to access your application by navigating to your VPS's IP address in a web browser. While the above steps get your application running, production deployments often require more robust solutions. Ensure your VPS's firewall is configured to allow traffic on the port you're exposing (e.g., port 80). If you're using a cloud provider's firewall, you'll need to open the relevant ports there as well. For production, it's common to use a reverse proxy like Nginx. A reverse proxy sits in front of your application container, handles incoming requests, and forwards them to your application. This offers benefits like SSL termination, load balancing, and caching. You would typically run Nginx in its own Docker container and configure it to proxy requests to your Python application container. If your application needs to store data (e.g., user uploads, database files), you'll need to use Docker volumes or bind mounts to persist data outside the container's lifecycle. Otherwise, any data stored inside the container will be lost when the container is removed. For more complex deployments with multiple containers, microservices, or the need for automatic scaling and self-healing, consider container orchestration platforms like Docker Swarm or Kubernetes. These tools manage the deployment, scaling, and networking of containerized applications. Containerizing your Python applications with Docker and deploying them to a VPS provides a consistent, reliable, and scalable way to run your code. By following these steps, you can move beyond the "it works on my machine" dilemma and gain better control over your application's deployment environment. Remember to prioritize security and consider advanced deployment strategies for production-ready applications. Templates let you quickly answer FAQs or store snippets for re-use. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse

Code Block

Copy

from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello from my containerized Python app!' if __name__ == '__main__': app.run(debug=True, host='0.0.0.0') from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello from my containerized Python app!' if __name__ == '__main__': app.run(debug=True, host='0.0.0.0') from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello from my containerized Python app!' if __name__ == '__main__': app.run(debug=True, host='0.0.0.0') # Use an official Python runtime as a parent image FROM python:3.9-slim # Set the working directory in the container WORKDIR /app # Copy the current directory contents into the container at /app COPY . /app # Install any needed packages specified in requirements.txt RUN pip install --no-cache-dir -r requirements.txt # Make port 5000 available to the world outside this container EXPOSE 5000 # Define environment variable ENV NAME World # Run app.py when the container launches CMD ["python", "app.py"] # Use an official Python runtime as a parent image FROM python:3.9-slim # Set the working directory in the container WORKDIR /app # Copy the current directory contents into the container at /app COPY . /app # Install any needed packages specified in requirements.txt RUN pip install --no-cache-dir -r requirements.txt # Make port 5000 available to the world outside this container EXPOSE 5000 # Define environment variable ENV NAME World # Run app.py when the container launches CMD ["python", "app.py"] # Use an official Python runtime as a parent image FROM python:3.9-slim # Set the working directory in the container WORKDIR /app # Copy the current directory contents into the container at /app COPY . /app # Install any needed packages specified in requirements.txt RUN pip install --no-cache-dir -r requirements.txt # Make port 5000 available to the world outside this container EXPOSE 5000 # Define environment variable ENV NAME World # Run app.py when the container launches CMD ["python", "app.py"] Flask==2.0.1 Flask==2.0.1 Flask==2.0.1 docker build -t my-python-app . docker build -t my-python-app . docker build -t my-python-app . docker run -p 5000:5000 my-python-app docker run -p 5000:5000 my-python-app docker run -p 5000:5000 my-python-app sudo apt update sudo apt install docker.io -y sudo systemctl start docker sudo systemctl enable docker sudo apt update sudo apt install docker.io -y sudo systemctl start docker sudo systemctl enable docker sudo usermod -aG docker $USER newgrp docker # Apply group changes to the current session sudo usermod -aG docker $USER newgrp docker # Apply group changes to the current session # On your local machine scp -r /path/to/your/app/directory user@your_vps_ip:/home/user/app # On your local machine scp -r /path/to/your/app/directory user@your_vps_ip:/home/user/app # On your local machine scp -r /path/to/your/app/directory user@your_vps_ip:/home/user/app cd /home/user/app # Or wherever you copied your app docker build -t my-python-app . cd /home/user/app # Or wherever you copied your app docker build -t my-python-app . cd /home/user/app # Or wherever you copied your app docker build -t my-python-app . docker run -d -p 80:5000 my-python-app docker run -d -p 80:5000 my-python-app docker run -d -p 80:5000 my-python-app - FROM python:3.9-slim: This specifies the base image. We're using a lightweight official Python 3.9 image. Using -slim variants often results in smaller image sizes. - WORKDIR /app: This sets the working directory inside the container. All subsequent commands will be executed from this directory. - COPY . /app: This copies the files from your local project directory (where the Dockerfile is located) into the /app directory inside the container. - RUN pip install --no-cache-dir -r requirements.txt: This command installs the Python dependencies. We assume you have a requirements.txt file. The --no-cache-dir flag helps reduce the image size by not storing the pip cache. - EXPOSE 5000: This informs Docker that the container will listen on port 5000 at runtime. It's documentation and doesn't actually publish the port. - ENV NAME World: This sets an environment variable named NAME with the value World. This is an example of how you can pass configuration into your container. - CMD ["python", "app.py"]: This specifies the command to run when the container starts. It executes our Flask application. - docker build: This command initiates the image building process. - -t my-python-app: The -t flag tags the image with a name (my-python-app). You can choose any name. - .: This dot indicates that the Dockerfile is in the current directory. - docker run: This command creates and starts a new container from an image. - -p 5000:5000: This maps port 5000 on your host machine to port 5000 inside the container. The format is host_port:container_port. - my-python-app: This is the name of the image you want to run. - Provision a VPS: Choose a Linux distribution (like Ubuntu or Debian) and provision your VPS. - Connect via SSH: Securely connect to your VPS using SSH. - Install Docker: Install Docker on your VPS. The installation process will vary slightly depending on your VPS's operating system. For Ubuntu, you can typically use: sudo apt update sudo apt install docker.io -y sudo systemctl start docker sudo systemctl enable docker It's also a good idea to add your user to the docker group to run Docker commands without sudo: sudo usermod -aG docker $USER newgrp docker # Apply group changes to the current session - -d: This runs the container in detached mode, meaning it will run in the background. - -p 80:5000: This maps port 80 on your VPS to port 5000 inside the container.