DEV Community

Ishwor Subedi
Ishwor Subedi

Posted on

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

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)
Enter fullscreen mode Exit fullscreen mode

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"]
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)