Lädt...


🔧 Dockerized deployments, CI/CD, automated workflows for production in cloud environments


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Image description

Introduction

In this blog, we’ll learn how to deploy a FastAPI app using Docker and automate it with CI/CD. We’ll go over why Docker is better than traditional SSH-based deployment, and how it simplifies the process of running apps in the cloud.

Why Docker?

With normal file setup, you manually upload files to a server via SSH. This often leads to issues like mismatched environments (e.g., different Python versions or missing libraries). Docker eliminates this problem by packaging everything (code, libraries, configurations) into a container.

  • Consistency: Docker ensures the app works the same on every machine.
  • Simplicity: Once the container is created, you don’t have to worry about setting up environments.
  • Scalability: Docker makes scaling your application easier, especially in cloud environments.

Why Not Traditional SSH Deployment?

In traditional deployment, you often use scp or rsync to upload code, and manually configure environments via SSH, which can cause:

  1. Environment issues: Different setups on local vs. server.
  2. Manual errors: Forgetting to install dependencies.
  3. Time-consuming: Manual steps every time you update the app.

Docker fixes this by packaging everything together. You create an image once, and then run it anywhere with Docker.

What is Docker?

Docker is a platform for running applications in containers. A Docker container is a self-contained unit that packages your code and all its dependencies. With Docker, your app works the same in development and production.

  • Dockerfile: Instructions to build the Docker image.
  • Image: Blueprint for the container.
  • Container: Running instance of an image.

What is CI/CD?

CI/CD (Continuous Integration/Continuous Delivery) automates testing, building, and deploying applications.

  • CI (Continuous Integration): Automatically test and integrate new code changes.
  • CD (Continuous Delivery): Automatically deploy tested code into production.

Creating a FastAPI App

We will create a simple FastAPI app and Dockerize it. Then, we'll automate the deployment using GitHub Actions.

1. FastAPI App (main.py)

# main.py
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"message": "Hello, Dockerized FastAPI World!"}

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)

2. Dockerfile

The Dockerfile is used to create a Docker image for our FastAPI app.

# 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
COPY . /app

# Install FastAPI and Uvicorn
RUN pip install fastapi uvicorn

# Expose the port FastAPI will run on
EXPOSE 8000

# Command to run the app
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

3. Build and Run Docker Container

To build and run the Docker container:

docker build -t fastapi-app .
docker run -d -p 8000:8000 fastapi-app

This will make the app accessible at http://localhost:8000.

Push to Docker Hub

To share your Docker image, push it to Docker Hub.

  1. Log in to Docker Hub:

    docker login
    
  2. Tag your image:

    docker tag fastapi-app yourdockerhubusername/fastapi-app:latest
    
  3. Push the image to Docker Hub:

    docker push yourdockerhubusername/fastapi-app:latest
    

CI/CD Workflow with GitHub Actions

Here’s how to automate Docker image building and deployment using GitHub Actions.

GitHub Actions Workflow (.github/workflows/docker.yml)

name: CI/CD for FastAPI Docker App

on:
  push:
    branches:
      - main

jobs:
  build-and-push:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v1

    - name: Login to DockerHub
      uses: docker/login-action@v2
      with:
        username: ${{ secrets.DOCKER_USERNAME }}
        password: ${{ secrets.DOCKER_PASSWORD }}

    - name: Build and Push Docker image
      run: |
        docker build -t yourdockerhubusername/fastapi-app:latest .
        docker push yourdockerhubusername/fastapi-app:latest

    - name: Log out from DockerHub
      run: docker logout

This workflow builds and pushes your Docker image to Docker Hub automatically when changes are pushed to the main branch. To set up:

  1. Add your Docker Hub credentials (DOCKER_USERNAME and DOCKER_PASSWORD) as GitHub secrets.
  2. Create the .github/workflows/docker.yml file.

Deploying Docker on RunPod

To deploy your Docker container on RunPod:

  1. Create an account at RunPod.
  2. Create a new pod (choose the appropriate machine type).
  3. ** Create a new template**

Image description

  1. Deploy the template based on your hardware requirements, such as CPU or GPU. It will automatically pull the Docker image from Docker Hub and initiate the container accordingly.

Now your FastAPI app will be running on the cloud via RunPod.

Docker Commands

Docker Installation instruction and commands

Conclusion

In this guide, we learned how to create a FastAPI app, Dockerize it, and automate its deployment using CI/CD. Docker simplifies the deployment process by ensuring the app runs consistently across environments. With tools like GitHub Actions and platforms like RunPod, you can automate the entire deployment process.

...

🔧 Dockerized deployments, CI/CD, automated workflows for production in cloud environments


📈 80.94 Punkte
🔧 Programmierung

🔧 The Gatekeeper: Why Approval-Based Deployments are Essential for Production Environments on Kubernetes


📈 33.47 Punkte
🔧 Programmierung

🔧 DevOps in Game Development: Accelerate Production with Automated Workflows


📈 31.56 Punkte
🔧 Programmierung

🔧 Setting Up Service Principle Deployments for Gitlab CI/CD Terraform Virtual Machine Deployments


📈 26.73 Punkte
🔧 Programmierung

📰 Sonrai Dig offers automated prevention of data breaches in public cloud deployments


📈 26.39 Punkte
📰 IT Security Nachrichten

🔧 100 Days of Cloud: Day 4 - Conquering Postgres in Our Dockerized Go App!


📈 25.41 Punkte
🔧 Programmierung

🔧 🟢 GitHub Workflows reimagined - A visual node system for GitHub workflows


📈 24.7 Punkte
🔧 Programmierung

🔧 🟢 GitHub Workflows reimagined - A visual node system for GitHub workflows


📈 24.7 Punkte
🔧 Programmierung

🔧 Deployment Window: Execute Controlled Deployments in Kubernetes Environments


📈 23.98 Punkte
🔧 Programmierung

🔧 Skipping Test Environments for Faster and Safer Deployments


📈 23.98 Punkte
🔧 Programmierung

🔧 Automated Security Testing in Cloud-Native Environments


📈 23.63 Punkte
🔧 Programmierung

🔧 Sample code on Service-to-Service Authentication in Google Cloud Run for Production and Local environments


📈 23.41 Punkte
🔧 Programmierung

📰 Und Microsoft so: Cloud, Cloud, Cloud, Cloud, Cloud, Cloud, Cloud


📈 23.14 Punkte
📰 IT Security Nachrichten

🔧 Automated Deployments of Meteor.js bundle using Terraform


📈 23.08 Punkte
🔧 Programmierung

🔧 Automated Deployments of Meteor.js bundle using Terraform


📈 23.08 Punkte
🔧 Programmierung

🔧 Capistrano: The Ultimate Guide to Automated Deployments for Rails Applications


📈 23.08 Punkte
🔧 Programmierung

🔧 SRE Deployment Engineer Managing Reliable & Automated Deployments


📈 23.08 Punkte
🔧 Programmierung

🔧 Day 20: Advanced CI/CD Practices in AWS – Blue-Green Deployments and Automated Testing


📈 23.08 Punkte
🔧 Programmierung

🔧 Eliminate Human-Based Actions With Automated Deployments: Improving Commit-to-Deploy Ratios Along the Way


📈 23.08 Punkte
🔧 Programmierung

🔧 Host a Next.js Blog with AWS Amplify and Setup Automated Deployments


📈 23.08 Punkte
🔧 Programmierung

🔧 Docker in Development and Testing Environments: Streamlining DevOps Workflows


📈 22.96 Punkte
🔧 Programmierung

🔧 Asynchronous I/O: A Practical Guide for Optimizing HPC Workflows with xiRAID in Lustre Environments


📈 22.96 Punkte
🔧 Programmierung

🔧 Module Federation in Production: architecture, development workflow, and deployments


📈 22.86 Punkte
🔧 Programmierung

🎥 ML engineering for production ML deployments with TFX (TensorFlow Fall 2020 Updates)


📈 22.86 Punkte
🎥 Video | Youtube

🔧 Testing in Production with Canary Deployments: A How-To Guide


📈 22.86 Punkte
🔧 Programmierung

📰 How I Dockerized Apache Flink, Kafka, and PostgreSQL for Real-Time Data Streaming


📈 22.1 Punkte
🔧 AI Nachrichten

🔧 Deploy a Dockerized Application on AWS ECS


📈 22.1 Punkte
🔧 Programmierung

🔧 Day 2 of My Devops Journey: Automating Dockerized Application Deployment with GitHub Actions


📈 22.1 Punkte
🔧 Programmierung

🔧 Dockerized Deployment of a Full Stack Application with Reverse Proxy, Monitoring & Observability


📈 22.1 Punkte
🔧 Programmierung

🔧 Building and Deploying Your First Dockerized Application 🚀


📈 22.1 Punkte
🔧 Programmierung

🔧 Relative Python imports in a Dockerized lambda function


📈 22.1 Punkte
🔧 Programmierung

🎥 Securing Dockerized apps in the Microsoft ecosystem | ODFP604


📈 22.1 Punkte
🎥 Video | Youtube

matomo