This guide will walk you through the process of using Docker Compose to manage a multi-container application consisting of a PostgreSQL database, PgAdmin for database management, a FastAPI application, and Nginx as a reverse proxy.
- Ensure Docker and Docker Compose are installed on your computer.
- Basic understanding of Docker, PostgreSQL, FastAPI, and how web applications work.
- To start all services defined in the Docker Compose file, navigate to the directory containing your
docker-compose.ymlfile and run:docker-compose up -d- This command starts the containers in the background.
- Open your web browser and visit
http://localhost:5050to access PgAdmin. - Login with the following credentials:
- Email:
admin@example.com - Password:
adminpassword
- Email:
- Configure the PostgreSQL server in PgAdmin:
- Right-click on 'Servers' in the left pane and select 'Create' > 'Server'.
- In the 'General' tab, give your server a name (e.g.,
MyAppDB). - Switch to the 'Connection' tab and enter:
- Hostname/address:
postgres - Port:
5432 - Username:
user - Password:
password - These credentials correspond to the environment variables set in the
docker-compose.ymlfor the PostgreSQL service.
- Hostname/address:
- Execute database migrations within the FastAPI container:
docker-compose exec fastapi alembic upgrade head- This command runs the Alembic upgrade command to apply migrations to your PostgreSQL database.
- To run tests inside the FastAPI container, ensuring they interact with the PostgreSQL service:
docker-compose exec fastapi pytest- This command runs all tests defined in your FastAPI application.
- To run a specific test file:
docker-compose exec fastapi pytest /myapp/tests/test_specific_file.py
- For executing tests with coverage reports:
docker-compose exec fastapi pytest --cov=myapp- To generate an HTML coverage report:
docker-compose exec fastapi pytest --cov=myapp --cov-report=html
- If you need to reset your environment, e.g., to clear test data:
- Stop all services and remove volumes:
docker-compose down -v
- Restart the services:
docker-compose up -d
- Stop all services and remove volumes:
- To build a Docker image for your FastAPI application, ensure you have a Dockerfile in the same directory as your
docker-compose.yml. Then run:docker-compose build
- Visit Docker Hub and sign up for an account.
- Once registered, you can create repositories to store your Docker images.
docker login- Enter your Docker Hub username and password.
docker tag local-image:tagname username/repository:tag- For example:
docker tag myfastapi:latest john/myfastapi:latest
- For example:
docker push username/repository:tag- For example:
docker push john/myfastapi:latest
- For example:
- To view logs for troubleshooting or monitoring application behavior:
docker-compose logs -f- The
-fflag tails the log output.
- To stop and remove all running containers:
docker-compose down
This guide is structured to provide clear, step-by-step instructions on how to interact with the Dockerized environment defined by your Docker Compose setup, ideal for educational purposes and ensuring students are well-equipped to manage their development environment effectively.