Track 2: Volumes and Docker Compose


Step-by-Step Guide

  1. Open Terminal:

    • Launch PowerShell if not default

  2. Create a New Project Directory:

  • Navigate to the location where you want to create your project directory.

    • Create a new directory for your project:

mkdir fastapi-docker
cd fastapi-docker
code .
  • This will open a new VScode window

  • Open the terminal in VSCode (Ctrl + `).

  1. Create the src Directory:

    • Inside your project directory, create a src directory:

mkdir src
  1. Create main.py Inside src:

    • Navigate into the src directory and create the main.py file:

  2. Add Code to main.py:

    • Open main.py in VSCode and add the following code:

  1. Create requirements.txt:

    • Go back to the root of your project directory and create a requirements.txt file:

  • Add the following dependencies to requirements.txt:

  1. Create the Dockerfile:

    • In the root of your project directory, create a Dockerfile: Dockerfile

    • Add the following content to the Dockerfile:

  1. Build and Run the Docker Container:

    • Build the Docker image:

  1. Analyzing the use of Volume

  • Run the Docker container without the volume mounting

  • Run the Docker container with volume mounting:

Access the Application

Open your web browser and go to http://localhost:8000. Any changes you make to main.py in the src directory will be automatically reflected in the running container.

Automate Docker creation using YML

Create docker-compose.yml file in parent directory

Run docker compose

Last updated