Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 14, 2023 07:53 am GMT

Dockerize FastAPI project like a pro - Step-by-step Tutorial

Introduction

Are you a developer looking to containerize your FastAPI project using Docker?

In this step-by-step tutorial, we'll show you how to Dockerize your FastAPI app like a pro. From setting up your development environment to deploying your app to Docker Hub, we'll cover everything you need to know to get started with Docker and FastAPI.

Watch the video

For a better understanding, watch the tutorial video on Stackless Tech YouTube Channel.

Let's go

Start with creating and activating python virtual environment.

$ python3 -m venv env$ source env/bin/activate(env) $ 

Install FastAPI and Uvicorn.

(env) $ pip install fastapi uvicorn

In main.py file, Write Hello, World! program.

from fastapi import FastAPIapp = FastAPI()@app.get("/")def index():    return {"details": "Hello, World!"}

run the server

(env) $ uvicorn main:app --reload --port=8000 --host=0.0.0.0

Open 127.0.0.1:8000 and see if it works.

Write dependencies into requirements.txt

(env) $ pip freeze > requirements.txt

Create Dockerfile, .dockerignore and docker-compose.yaml files

FROM python:3.8.10-slimWORKDIR /appCOPY . /appRUN pip install -r requirements.txt
env/__pycache__/*.env*.env.*env.*
version: '3'services:  web:    build: .    command: sh -c "uvicorn main:app --reload --port=8000 --host=0.0.0.0"    ports:      - 8000:8000

Run the docker container

(env) $ docker compose up --build

Go to 127.0.0.1:8000, And it's now running from the Docker Container.

I hope you guys liked this quick tutorial

Cheers


Original Link: https://dev.to/rajeshj3/dockerize-fastapi-project-like-a-pro-step-by-step-tutorial-7i8

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To