Learn Docker to Make Deployment Easy (step-by-step)
Table of Contents
Introduction
This tutorial will guide you through the process of using Docker to create a consistent deployment environment for your FastAPI applications. Docker simplifies the deployment process by ensuring that your local development environment mirrors your production setup. This is essential for preventing "works on my machine" issues and streamlining deployment.
Step 1: Install Docker
Before you start, ensure you have Docker installed on your machine. Follow these steps:
-
Download Docker:
- Go to the Docker website.
- Choose the appropriate version for your operating system (Windows, macOS, or Linux).
-
Install Docker:
- Follow the installation instructions for your OS.
- After installation, verify by running the command:
docker --version
-
Start Docker:
- Launch Docker Desktop and ensure it is running.
Step 2: Set Up a FastAPI Project
Next, create a FastAPI project that you will later containerize using Docker.
-
Create a Project Directory:
- Open your terminal and run:
mkdir fastapi-docker cd fastapi-docker
- Open your terminal and run:
-
Set Up a Virtual Environment (optional but recommended):
- Create a virtual environment:
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
- Create a virtual environment:
-
Install FastAPI and Uvicorn:
- Install the necessary packages:
pip install fastapi uvicorn
- Install the necessary packages:
-
Create Your FastAPI Application:
- Create a file named
main.py
and add the following code:from fastapi import FastAPI app = FastAPI() @app.get("/") def read_root(): return {"Hello": "World"}
- Create a file named
Step 3: Create a Dockerfile
The Dockerfile contains instructions for building your Docker image.
-
Create a Dockerfile in the project directory:
- Create a file named
Dockerfile
and add the following content:# Use the official Python image FROM python:3.9 # Set the working directory WORKDIR /app # Copy the requirements file (create requirements.txt first) COPY requirements.txt . # Install dependencies RUN pip install --no-cache-dir -r requirements.txt # Copy the application code COPY . . # Expose the application on port 8000 EXPOSE 8000 # Command to run the application CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
- Create a file named
-
Create a requirements.txt:
- List your project dependencies:
fastapi uvicorn
- List your project dependencies:
Step 4: Build the Docker Image
With your Dockerfile ready, you can now build your Docker image.
-
Run the Build Command:
- In your terminal, execute:
docker build -t fastapi-docker .
- In your terminal, execute:
-
Verify the Image:
- Check your built images:
docker images
- Check your built images:
Step 5: Run the Docker Container
Now that you have built your image, you can run it as a container.
-
Run the Container:
- Start your application in a container:
docker run -d --name fastapi-container -p 8000:8000 fastapi-docker
- Start your application in a container:
-
Access the Application:
- Open a web browser and navigate to
http://localhost:8000
to see your FastAPI app in action.
- Open a web browser and navigate to
Conclusion
Congratulations! You have successfully set up a FastAPI application using Docker. By following these steps, you can ensure a consistent environment for development and production.
Next Steps
- Explore more advanced Docker features like multi-stage builds.
- Integrate a database with your FastAPI application.
- Check out additional resources on FastAPI and Docker for further learning.