I created a FASTAPI service that I containerized and made 3 service replicas of it. Then I added nginx loadbalancer service in docker as well and created nginx.conf with upstream.
When I open the defined address, i do not get the correct page but instead get nginx default page.
Folder Structure
└── ????ngnx-fastapi-docker
└── README.md
└── docker-compose.yaml
└── ????nginx
└── nginx.conf
└── ????project
└── .dockerignore
└── Dockerfile
└── Pipfile
└── Pipfile.lock
└── main.py
nginx.conf
http {
upstream projectapp {
server app1:8000;
server app2:8000;
server app3:8000;
}
server {
listen 80;
server_name _;
location / {
proxy_pass http://projectapp;
}
}
}
docker-compose.yaml
version: "3"
services:
app1:
build: project
container_name: project_app
command: pipenv run uvicorn main:app --host 0.0.0.0 --port 8000 --reload
ports:
- 8010:8000
volumes:
- ./project:/app
app2:
build: project
container_name: project_app2
command: pipenv run uvicorn main:app --host 0.0.0.0 --port 8000 --reload
ports:
- 8011:8000
volumes:
- ./project:/app
app3:
build: project
container_name: project_app3
command: pipenv run uvicorn main:app --host 0.0.0.0 --port 8000 --reload
ports:
- 8012:8000
volumes:
- ./project:/app
nginx:
image: nginx:alpine
container_name: project_LB
ports:
- 8000:80
volumes:
- ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf;
Dockerfile
FROM python:3.12.0-alpine
WORKDIR /app
COPY Pipfile Pipfile.lock /app/
RUN python -m pip install --upgrade pip && pip install pipenv
RUN pipenv install
RUN pipenv run pip list
COPY . /app
I’m on a MAC using vscode Version: 1.89.0
I tried adding network in the compose.yaml but it still resulted in the same default page.
I am new to docker and nginx. Please help me.