Learn Docker to Make Deployment Easy (step-by-step)

3 min read 21 days ago
Published on Sep 13, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

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:

  1. Download Docker:

    • Go to the Docker website.
    • Choose the appropriate version for your operating system (Windows, macOS, or Linux).
  2. Install Docker:

    • Follow the installation instructions for your OS.
    • After installation, verify by running the command:
      docker --version
      
  3. 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.

  1. Create a Project Directory:

    • Open your terminal and run:
      mkdir fastapi-docker
      cd fastapi-docker
      
  2. 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`
      
  3. Install FastAPI and Uvicorn:

    • Install the necessary packages:
      pip install fastapi uvicorn
      
  4. 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"}
      

Step 3: Create a Dockerfile

The Dockerfile contains instructions for building your Docker image.

  1. 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"]
      
  2. Create a requirements.txt:

    • List your project dependencies:
      fastapi
      uvicorn
      

Step 4: Build the Docker Image

With your Dockerfile ready, you can now build your Docker image.

  1. Run the Build Command:

    • In your terminal, execute:
      docker build -t fastapi-docker .
      
  2. Verify the Image:

    • Check your built images:
      docker images
      

Step 5: Run the Docker Container

Now that you have built your image, you can run it as a container.

  1. Run the Container:

    • Start your application in a container:
      docker run -d --name fastapi-container -p 8000:8000 fastapi-docker
      
  2. Access the Application:

    • Open a web browser and navigate to http://localhost:8000 to see your FastAPI app in action.

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.