This project demonstrates a multi-container application using Docker Compose, including a web application served by Nginx and a PostgreSQL database. The application, running in a Python Flask container, interacts with the PostgreSQL database. Nginx serves as a reverse proxy to the Flask application.
- Docker
- Docker Compose
- Clone the repository:
git clone <repository_url> cd my_docker_compose_project
Build and run the containers:
docker-compose up --buildAccess the web application at http://localhost.
Defines the multi-container setup:
-
web:
- Builds the application container from the
appdirectory. - Sets the
DATABASE_URLenvironment variable. - Maps port
5000on the host to port5000in the container.
- Builds the application container from the
-
nginx:
- Builds the Nginx container from the
nginxdirectory. - Maps port
80on the host to port80in the container. - Forwards requests to the
webservice on port5000.
- Builds the Nginx container from the
-
db:
- Builds the PostgreSQL container from the
postgresdirectory. - Sets environment variables for PostgreSQL (
POSTGRES_USER,POSTGRES_PASSWORD,POSTGRES_DB). - Maps port
5432on the host to port5432in the container.
- Builds the PostgreSQL container from the
Builds the Nginx image:
- Uses the official Nginx image.
- Copies the Nginx configuration file and static HTML files into the container.
Configures Nginx to proxy requests to the Flask application:
- Forwards requests from port
80to thewebservice on port5000.
A simple static HTML page served by Nginx:
- Displays a welcome message indicating that the application is connected to a PostgreSQL database.
Builds the Flask application container:
- Uses the Python 3.9 slim image.
- Installs dependencies from
requirements.txt. - Runs the Flask application.
Lists the Python packages needed for the application:
flaskfor the web framework.psycopg2-binaryfor PostgreSQL database connectivity.
A simple Flask application:
- Connects to PostgreSQL using
psycopg2. - Exposes an endpoint that retrieves and returns data from the
userstable in PostgreSQL.
Builds the PostgreSQL image:
- Uses the official PostgreSQL image.
- Copies the initialization SQL script into the container.
Initial SQL script for PostgreSQL:
- Creates a
userstable. - Inserts sample data into the table.
This project provides a practical example of using Docker Compose to set up a multi-container application with Nginx, a Flask application, and PostgreSQL. It demonstrates how to configure and connect these components in a development environment.
- Nginx: Serves as the reverse proxy for the Flask application.
- Flask App: Connects to the PostgreSQL database and provides an endpoint to retrieve data.
- PostgreSQL: Stores data and is initialized with a sample dataset.
This project is licensed under the MIT License.