การทำ FastAPI ให้เป็นคอนเทนเนอร์
เรียนรู้การบรรจุบริการ FastAPI ลงในคอนเทนเนอร์ด้วย Docker พร้อมสร้างอิมเมจสำหรับการนำไปใช้งานที่มีประสิทธิภาพและเคลื่อนย้ายได้
การทำ FastAPI ให้เป็นคอนเทนเนอร์ เป็นบทเรียน FastAPI Backend Development Bootcamp ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน FastAPI Backend Development Bootcamp และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส FastAPI Backend Development Bootcamp มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Docker & FastAPI: Why Containerize?
Welcome! In this lesson, we'll learn how to package your FastAPI application using Docker. This makes your app incredibly consistent and easy to deploy anywhere.
- What is Docker? It's a platform that uses OS-level virtualization to deliver software in packages called containers.
- Why use it for FastAPI? It solves the "it works on my machine" problem by packaging your app and its dependencies together.
Docker's Core: Images & Containers
Before we dive into Dockerizing, let's understand two key concepts:
- Docker Image: Think of an image as a blueprint or a template. It's a static, immutable file that contains your application code, libraries, dependencies, and configuration.
- Docker Container: A container is a runnable instance of an image. It's an isolated environment where your application runs, completely separate from your host system.
You build an image, then run a container from it.
A Basic FastAPI Service
Let's start with a simple FastAPI application that we'll containerize. This file will be named main.py.
It has a single endpoint that returns a 'Hello' message.
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def read_root():
return {"message": "Hello from FastAPI!"}
# To run locally (without Docker):
# uvicorn main:app --host 0.0.0.0 --port 8000The Dockerfile: Starting Strong
A Dockerfile is a text file that contains all the commands a user could call on the command line to assemble an image. It's your recipe for building the image.
We start by picking a base image and setting our working directory:
FROM: Specifies the base image (e.g., Python version).WORKDIR: Sets the current working directory inside the container.
# Dockerfile
FROM python:3.9-slim-buster
WORKDIR /appFastAPI Dependencies: requirements.txt
Your FastAPI app needs specific Python packages to run. We list these in a requirements.txt file. This file tells Docker which packages to install inside the container.
Here's a typical requirements.txt for our simple FastAPI app:
# requirements.txt
fastapi==0.104.1
uvicorn[standard]==0.24.0.post1Dockerfile: Installing Dependencies
Now, let's add commands to our Dockerfile to copy and install these dependencies:
COPY requirements.txt .: Copies therequirements.txtfrom your local machine to the container's/appdirectory.RUN pip install ...: Executes the command to install all packages listed inrequirements.txt.--no-cache-dirhelps keep the image size small.
# Dockerfile (continued)
FROM python:3.9-slim-buster
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txtDockerfile: App Code & Entrypoint
Finally, we add our application code and tell Docker how to run it:
COPY . .: Copies all remaining files from your current directory (includingmain.py) into the container's/appdirectory.EXPOSE 8000: Informs Docker that the container listens on port 8000. It's documentation, not a firewall rule.CMD [...]: Specifies the command to run when the container starts. This is how Uvicorn will serve your FastAPI app.
# Dockerfile (final)
FROM python:3.9-slim-buster
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]Building Your Docker Image
With your Dockerfile and FastAPI app ready, it's time to build the Docker image!
Open your terminal in the same directory as your Dockerfile and main.py, then run:
docker build .: Tells Docker to build an image using the Dockerfile in the current directory.-t myfastapi-app: Tags the image with a name (myfastapi-app) for easy reference.
# Terminal Command
docker build -t myfastapi-app .Running Your FastAPI Container
Once the image is built, you can run your FastAPI application in a container!
Use the docker run command:
-p 8000:8000: This maps port 8000 on your host machine to port 8000 inside the container.myfastapi-app: The name of the image we just built.
After running, open your browser or use curl to visit http://localhost:8000.
# Terminal Command
docker run -p 8000:8000 myfastapi-appIntroducing Docker Compose
For applications with multiple services (like a FastAPI app and a database), Docker Compose simplifies management. It lets you define and run multi-container Docker applications using a YAML file.
Instead of running multiple docker run commands, you define everything in docker-compose.yml and use docker-compose up.
# docker-compose.yml (simplified)
version: '3.8'
services:
web:
build: .
ports:
- "8000:8000"Dockerfile Command Check
Which Dockerfile command is used to copy files or directories from your local machine into the Docker image?
Recap: FastAPI in a Box!
Great job! You've learned the fundamentals of Dockerizing your FastAPI application. We covered:
- The difference between Docker Images and Containers.
- Writing a Dockerfile with commands like
FROM,WORKDIR,COPY,RUN,EXPOSE, andCMD. - Building and running your FastAPI app in a Docker container.
- A brief introduction to Docker Compose for multi-service apps.
This skill is crucial for consistent and scalable deployments!
คำถามที่พบบ่อย
บทเรียน “การทำ FastAPI ให้เป็นคอนเทนเนอร์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การทำ FastAPI ให้เป็นคอนเทนเนอร์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส FastAPI Backend Development Bootcamp ให้อัปเกรดเป็น CoddyKit PRO คอร์ส FastAPI Backend Development Bootcamp มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การทำ FastAPI ให้เป็นคอนเทนเนอร์”
เรียนรู้การบรรจุบริการ FastAPI ลงในคอนเทนเนอร์ด้วย Docker พร้อมสร้างอิมเมจสำหรับการนำไปใช้งานที่มีประสิทธิภาพและเคลื่อนย้ายได้ คุณปฏิบัติ FastAPI Backend Development Bootcamp ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน FastAPI Backend Development Bootcamp หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน FastAPI Backend Development Bootcamp บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การทำ FastAPI ให้เป็นคอนเทนเนอร์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน FastAPI Backend Development Bootcamp นี้ได้ไหม
ได้ บทเรียน FastAPI Backend Development Bootcamp ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การทำ FastAPI ให้เป็นคอนเทนเนอร์
- การนำไปใช้งานด้วย Gunicorn และ Uvicorn
- กลยุทธ์การนำไปใช้งานบนคลาวด์
- การจัดการตัวแปรสภาพแวดล้อมและข้อมูลลับ