I have three dockerized projects that I have deployed successfully (apparently) to fly.io, they are menufrontapp (which is a react app), menubackapp (which is a django/Python app), and menudbapp (which is an image of postgres doenloaded from Docker).
The issue that I’m facing now is that the backend (menubackapp) doesn’t connect with menudbapp (the database), the flyctl logs of menubackapp says:
2023-07-29T21:45:27Z app[17811612a54758] scl [info]Waiting for the database to become available...
2023-07-29T21:45:28Z app[17811612a54758] scl [info]nc: getaddrinfo for host "menudbapp" port 5432: Name or service not known
The Dockerfile of menudbapp is:
# Use the official PostgreSQL base image from Docker Hub
FROM postgres:latest
# Optionally, set environment variables for PostgreSQL configuration
ENV POSTGRES_USER=postgres
ENV POSTGRES_PASSWORD=<my password>
ENV POSTGRES_DB=menu_db
# Optionally, copy the initdb.d directory into the container
COPY initdb.d /docker-entrypoint-initdb.d/
# Optionally, expose PostgreSQL port if needed (5432 is the default port)
EXPOSE 5432
The fly.toml is:
# fly.toml
app = "menudbapp"
[regions]
# Remove any existing entry for scl
iad = { pools = ["default"] }
[http_service]
internal_port = 5432
force_https = true
auto_start_machines = true
auto_stop_machines = false
min_machines_running = 0
[[services]]
# Use the existing PostgreSQL image
image = "postgres:latest"
internal_port = 5432
protocol = "tcp"
[services.concurrency]
hard_limit = 1
soft_limit = 1
[[services.ports]]
port = "5432"
[services.env]
POSTGRES_USER = "postgres"
POSTGRES_PASSWORD = <my password>
POSTGRES_DB = "menu_db"
After deploying menudbapp I got:
--> Pushing image done
image: registry.fly.io/menudbapp:deployment-01H6HRWAM2SNXF59AMB8EGBSYH
image size: 412 MB
Watch your app at https://fly.io/apps/menudbapp/monitoring
Updating existing machines in 'menudbapp' with rolling strategy
[1/2] Machine e2865652f79e78 [app] update finished: success
[2/2] Machine 178175df593589 [app] update finished: success
Finished deploying
Visit your newly deployed app at https://menudbapp.fly.dev/
It is well
Then the end of flyctl deploy of menubackapp is:
Updating existing machines in 'menubackapp' with rolling strategy
[1/1] Machine 17811612a54758 [app] update finished: success
WARNING The app is not listening on the expected address and will not be reachable by fly-proxy.
You can fix this by configuring your app to listen on the following addresses:
- 0.0.0.0:8000
Found these processes inside the machine with open listening sockets:
PROCESS | ADDRESSES
-----------------*---------------------------------------
/.fly/hallpass | [fdaa:2:825d:a7b:100:9923:8883:2]:22
Finished deploying
I think that here is an issue
The Dockerfile of menubackapp is:
# Use the official Python image as the base image
FROM python:3.9
# Set environment variables for Python
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Set the working directory in the container
WORKDIR /app
# Install system dependencies
RUN apt-get update && \
apt-get install -y netcat-openbsd
# Copy the requirements file and install Python dependencies
COPY requirements.txt /app/
RUN pip install --upgrade pip && pip install -r requirements.txt
# Copy the Django project files to the container
COPY backend /app/backend
# Set the entrypoint script with Gunicorn
COPY run_migrations.bat /run_migrations.bat
RUN chmod +x /run_migrations.bat
ENTRYPOINT ["/run_migrations.bat"]
# Expose the port Gunicorn will listen on (usually 8000)
EXPOSE 8000
# Set environment variables for the database connection
ENV DATABASE_HOST=<the IP Address associated to menudbapp>
ENV DATABASE_PORT=5432
ENV DATABASE_NAME=menu_db
ENV DATABASE_USER=postgres
ENV DATABASE_PASSWORD=<my password>
And run_migrations.bat is:
#!/bin/bash
set -e
# Wait for the database to become available
while ! nc -zv $FLY_APP_menudbapp_PRIVATE_IP 5432; do
echo "Waiting for the database to become available..."
sleep 1
done
# Run migrations
python backend/manage.py migrate
# Start the Gunicorn server
gunicorn backend.wsgi:application --bind 0.0.0.0:8000
I’m replacing the development server of django for gunicorn
So, what could I be missing here?
Thanks in advance
Rafael