ameen-mart - Departmental Store
Notes: We are using code of previous steps but have removed the requirement of sslmode in the main.py file:
engine = create_engine(
connection_string, connect_args={"sslmode": "require"}, pool_recycle=300)
All we have included .env file, you do not have to create a new one.
In order to validate your compose.yaml file give the following command (docker compose config renders the actual data model to be applied on the Docker Engine. It resolves variables in the Compose file, and expands short-notation into the canonical format):
docker compose config
Example: Python and PostgreSQL with Network
This example demonstrates a Compose file with a Python service and a PostgreSQL service connected by a custom network named "my-api-net":
Details
https://hub.docker.com/_/postgres
https://github.com/docker-library/docs/blob/master/postgres/README.md
version: "3.9"
name: myapi
services:
api:
build:
context: ./todo
dockerfile: Dockerfile.dev
depends_on:
- postgres_db
ports:
- "8000:8000" # Expose container port 8000 to host port 8000
networks:
- my-api-net
postgres_db:
image: postgres:latest # Use the official PostgreSQL image
restart: always
container_name: PostgresCont
environment:
- POSTGRES_USER=ziakhan
- POSTGRES_PASSWORD=my_password
- POSTGRES_DB=mydatabase
ports:
- '5433:5432'
volumes:
- postgres_db:/var/lib/postgresql/data
networks:
- my-api-net
volumes:
postgres_db:
driver: local
networks:
my-api-net: # Define the custom network
Explanation:
Here's a detailed explanation of the Docker Compose file:
File Structure:
3.9
specifies the Compose file format for compatibility.myapi
labels the project for organization.Services:
api:
context: ./todo
: Instructs Compose to build the image from the ./todo
directory.dockerfile: Dockerfile.dev
: Specifies the Dockerfile named Dockerfile.dev
for building instructions.postgres_db
: Ensures the database starts before this service.8000:8000
: Maps container port 8000 to host port 8000 for accessibility.my-api-net
: Connects the service to the designated network.postgres_db:
postgres:latest
: Uses the official PostgreSQL image for the database.always
: Automatically restarts the container if it crashes.PostgresCont
: Assigns a friendly name to the container for identification.POSTGRES_USER=ziakhan
: Database username.POSTGRES_PASSWORD=my_password
: Database password.POSTGRES_DB=mydatabase
: Name of the database to create.5433:5432
: Exposes container port 5432 (PostgreSQL's default) to host port 5433 for access.postgres_db:/var/lib/postgresql/data
: Persistently stores database data in a volume named postgres_db
.my-api-net
: Connects to the custom network.Volumes:
Networks:
Summary:
This Compose file defines a two-service application:
Both services are connected to a shared network for seamless communication. The database data is preserved using a persistent volume.
Running the application:
With this Compose file saved as compose.yml
, you can use the following commands to manage your application:
docker compose up -d
: This builds the images (if needed) and starts both containers in detached mode (background). You can check it by going open: http://0.0.0.0:8000/ in browser.docker compose stop
: This stops both containers.docker compose down
: This stops and removes both containers, as well as volumes associated with them.Here's the connection string you can use to connect to the Postgres database from another container running in the same Docker network:
postgresql://ziakhan:my_password@PostgresCont:5432/mydatabase
Let's break down the connection string components:
postgresql://
: This specifies the database driver to be used (in this case, PostgreSQL).ziakhan:my_password
: This defines the username and password for connecting to the database. Replace my_password
with your actual Postgres user's password.@PostgresCont
: This indicates the hostname or IP address of the Postgres container. By default, Docker links container names to hostnames within the Docker network. If your Postgres container is named differently (e.g., my-postgres
), replace PostgresCont
with the actual name.:5432
: This specifies the port on which the Postgres server is listening. The default port for Postgres is 5432, but you can change it if your container configuration uses a non-standard port./mydatabase
: This defines the name of the specific database you want to connect to within the Postgres instance.Important Note:
mydatabase
with the actual name of the database you want to connect to.Additional Considerations:
-p
, update the connection string's port number accordingly (e.g., :5433
if mapped to host port 5433).By following these guidelines and using the provided connection string format, you should be able to connect to your Postgres database from another container within your Docker environment.
pgAdmin is the most popular and feature rich Open Source administration and development platform for PostgreSQL, the most advanced Open Source database in the world.
Download and Install pgAdmin 4:
https://www.pgadmin.org/download/
Connect to our PostgresSQL Container:
Host name/address: localhost
Post: 5433 (note it is not the default 5432)
Maintaince database: mydatabase
Username: ziakhan
Password: my_password
Create a seprate database container for testing in compose file. You dont need to even use a volume for test database since we don't care to persist test data between runs. Once you have created the test database and updated the .env file, run the tests.