I am making a docker setup for a class on microservices I am taking. Can someone please tell me why the frontend container cannot make requests to the Flask server running on the user container through it’s alias? The aliasing does seems to work between the connection of the user and user-db containers, so why not between the user and frontend containers?
docker-compose.yaml
services:
# frontend service
frontend:
container_name: frontend
build: ./gui
# Specify internal network
networks:
host:
user-service:
# Expose the frontend service on port 5000
ports:
- "5000:5000"
# User service
user:
container_name: user
build: ./user
# Specify internal network
networks:
user-service:
aliases: [ "user" ]
user-db:
# Wait for the user-db service to be healthy before starting the user service
depends_on:
user-db:
condition: service_healthy
# User service database
user-db:
container_name: user-db
image: postgres:16.3-bookworm
# Specify volume
volumes:
- user-db-data:/var/lib/postgresql/data
# Specify internal network
networks:
user-db:
aliases: [ "user-db" ]
# Set environment variables
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: glowing-banana
POSTGRES_DB: user_db
# Check for correct startup
healthcheck:
test: "pg_isready -U 'root' -d 'user_db' || exit 1"
interval: 10s
timeout: 10s
retries: 5
volumes:
# Define volumes to ensure data persistence of service databases
user-db-data: {}
networks:
host: {}
# Define networks to be used internally
user-service:
internal: true
user-db:
internal: true
code in backend
from flask import Flask, request, Response
from flask_restful import Api, Resource
app = Flask("User")
api = Api(app)
class Test(Resource):
def get(self) -> Response:
return Response("Hello World!", status=200)
api.add_resource(Test, "/")
if __name__ == "__main__":
app.run(debug=True)
code in frontend
response = requests.get('http://user:5001/')
Because this is a project for a class on microservices, exposing all containers and having them communicate through localhost is not an optimal solution, altough it would work. I am looking for a way to make this work using internal networks.
2