I have app on the app platform that consists of a React front end, a Django backend, and a managed PostgreSQL database. The frontend is deployed from source (github) and builds and deploys without issues. The backend (django) is a container built using github actions and pushed to a DigitalOcean container registy. The Dockerfile is below (2 part build, wheels copied to runtime container, no entrypoint run):
###########
# BUILDER #
###########
# pull official base image
FROM --platform=linux/amd64 python:3.11-bookworm AS visivi4_builder
# set work directory
WORKDIR /usr/src/visivi4
# set environment variables
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
# install psycopg2 dependencies
RUN apt-get update \
&& apt-get install gcc git proj-bin libgdal-dev -y \
&& pip install --upgrade pip
# install dependencies
COPY ./requirements.txt .
COPY ./requirements/ ./requirements/
RUN pip wheel --no-cache-dir --wheel-dir /usr/src/visivi4/wheels -r requirements/production.txt
#########
# FINAL #
#########
# pull official base image
FROM --platform=linux/amd64 python:3.11-slim-bookworm AS visivi4_runtime
# create directory for the app user
RUN mkdir -p /home/visivi4
ARG USERNAME=visivi4
ARG USER_UID=1000
ARG USER_GID=$USER_UID
# create the appropriate directories
ENV HOME=/home/visivi4
ENV APP_HOME=/home/visivi4/web
# create the app user
RUN groupadd --gid $USER_GID $USERNAME \
&& useradd --uid $USER_UID --gid $USER_GID -m $USERNAME \
&& mkdir $APP_HOME \
&& mkdir $APP_HOME/staticfiles
WORKDIR $APP_HOME
# install geo libraries, note these paths need to be set in .env files
RUN apt-get update \
&& RUN_DEPS=" \
libgdal32 \
libgeos-c1v5 \
" \
# && UNUSED=" \
# # gcc-12 \
# # mariadb-common \
# # mysql-common \
# " \
&& apt-get install --no-install-recommends -y $RUN_DEPS \
&& rm -rf /var/lib/apt/lists/*
# && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false $UNUSED
COPY --from=visivi4_builder /usr/src/visivi4/wheels /wheels
RUN pip install --no-cache --no-deps /wheels/*
# copy project
COPY --chown=visivi4:visivi4 . $APP_HOME
# copy entrypoint.prod.sh
COPY --chown=visivi4:visivi4 ./entrypoint.vis4.sh .
RUN sed -i 's/\r$//g' ./entrypoint.vis4.sh \
&& chmod +x ./entrypoint.vis4.sh \
&& rm -f .env
RUN chown visivi4:visivi4 $APP_HOME/staticfiles
# change to the app user
USER visivi4
# run entrypoint.prod.sh
# ENTRYPOINT ["./entrypoint.vis4.sh"]
EXPOSE 8000
EXPOSE 25060
ENV DJANGO_SETTINGS_MODULE=config.settings.production
# DigitalOcean AppPlatform allows setting of a RunCommand, use that
# CMD ["gunicorn", "-w", "2", "-b", "0.0.0.0:8000", "config.wsgi:application"]
This deploys happily.
Once I attempt to connect to the backend I receive an error that it cannot resolve the host for the database.
[2024-08-14 12:27:59,702] ERROR django.request:241 -> Internal Server Error: /api/logincheck/
Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 289, in ensure_connection
self.connect()
File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 270, in connect
self.connection = self.get_new_connection(conn_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/contrib/gis/db/backends/postgis/base.py", line 112, in get_new_connection
connection = super().get_new_connection(conn_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/backends/postgresql/base.py", line 275, in get_new_connection
connection = self.Database.connect(**conn_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/psycopg2/__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
psycopg2.OperationalError: could not translate host name "db-postgisql-lon1-vis4-staging-do-user-1111702-0.m.db.ondigitalocean.com" to address: Name or service not known
visivi4-backend 2024-08-14T12:28:00.164954040Z
The above exception was the direct cause of the following exception:
visivi4-backend 2024-08-14T12:28:00.164960470Z Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/contextlib.py", line 80, in inner
with self._recreate_cm():
File "/usr/local/lib/python3.11/site-packages/django/db/transaction.py", line 198, in __enter__
if not connection.get_autocommit():
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 464, in get_autocommit
self.ensure_connection()
File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 288, in ensure_connection
with self.wrap_database_errors:
File "/usr/local/lib/python3.11/site-packages/django/db/utils.py", line 91, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 289, in ensure_connection
self.connect()
File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 270, in connect
self.connection = self.get_new_connection(conn_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/contrib/gis/db/backends/postgis/base.py", line 112, in get_new_connection
connection = super().get_new_connection(conn_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/backends/postgresql/base.py", line 275, in get_new_connection
connection = self.Database.connect(**conn_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/psycopg2/__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
django.db.utils.OperationalError: could not translate host name "db-postgisql-lon1-vis4-staging-do-user-1111702-0.m.db.ondigitalocean.com" to address: Name or service not known
visivi4-backend 2024-08-14T12:28:00.176060723Z [2024-08-14 12:27:59,698] ERROR django.request:241 -> Internal Server Error: /api/users/user-info/
Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 289, in ensure_connection
self.connect()
File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 270, in connect
self.connection = self.get_new_connection(conn_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/contrib/gis/db/backends/postgis/base.py", line 112, in get_new_connection
connection = super().get_new_connection(conn_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/backends/postgresql/base.py", line 275, in get_new_connection
connection = self.Database.connect(**conn_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/psycopg2/__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
psycopg2.OperationalError: could not translate host name "db-postgisql-lon1-vis4-staging-do-user-1111702-0.m.db.ondigitalocean.com" to address: Name or service not known
visivi4-backend 2024-08-14T12:28:00.176173954Z
The above exception was the direct cause of the following exception:
visivi4-backend 2024-08-14T12:28:00.176180871Z Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/contextlib.py", line 80, in inner
with self._recreate_cm():
File "/usr/local/lib/python3.11/site-packages/django/db/transaction.py", line 198, in __enter__
if not connection.get_autocommit():
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 464, in get_autocommit
self.ensure_connection()
File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 288, in ensure_connection
with self.wrap_database_errors:
File "/usr/local/lib/python3.11/site-packages/django/db/utils.py", line 91, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 289, in ensure_connection
self.connect()
File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 270, in connect
self.connection = self.get_new_connection(conn_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/contrib/gis/db/backends/postgis/base.py", line 112, in get_new_connection
connection = super().get_new_connection(conn_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/backends/postgresql/base.py", line 275, in get_new_connection
connection = self.Database.connect(**conn_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/psycopg2/__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
django.db.utils.OperationalError: could not translate host name "db-postgisql-lon1-vis4-staging-do-user-1111702-0.m.db.ondigitalocean.com" to address: Name or service not known
visivi4-backend 2024-08-14T12:28:00.672151627Z [2024-08-14 12:28:00,355] ERROR django.request:241 -> Internal Server Error: /api/accounts/login/
Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 289, in ensure_connection
self.connect()
File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 270, in connect
self.connection = self.get_new_connection(conn_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/contrib/gis/db/backends/postgis/base.py", line 112, in get_new_connection
connection = super().get_new_connection(conn_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/backends/postgresql/base.py", line 275, in get_new_connection
connection = self.Database.connect(**conn_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/psycopg2/__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
psycopg2.OperationalError: could not translate host name "db-postgisql-lon1-vis4-staging-do-user-1111702-0.m.db.ondigitalocean.com" to address: Name or service not known
visivi4-backend 2024-08-14T12:28:00.672266270Z
The above exception was the direct cause of the following exception:
visivi4-backend 2024-08-14T12:28:00.672274758Z Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/contextlib.py", line 80, in inner
with self._recreate_cm():
File "/usr/local/lib/python3.11/site-packages/django/db/transaction.py", line 198, in __enter__
if not connection.get_autocommit():
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 464, in get_autocommit
self.ensure_connection()
File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 288, in ensure_connection
with self.wrap_database_errors:
File "/usr/local/lib/python3.11/site-packages/django/db/utils.py", line 91, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 289, in ensure_connection
self.connect()
File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 270, in connect
self.connection = self.get_new_connection(conn_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/contrib/gis/db/backends/postgis/base.py", line 112, in get_new_connection
connection = super().get_new_connection(conn_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/db/backends/postgresql/base.py", line 275, in get_new_connection
connection = self.Database.connect(**conn_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/psycopg2/__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
django.db.utils.OperationalError: could not translate host name "db-postgisql-lon1-vis4-staging-do-user-1111702-0.m.db.ondigitalocean.com" to address: Name or service not known
For clarification, the above hostname is my managed postgres cluster
Things I’ve tried:
I have been able to use the console to confirm that DNS is working (used python REPL in the console to send a GET to www.google.com and received 200) I can connect to the database using the connection string and psql from my local machine.
I’m hoping this is something basic I am missing as the django backend will happily connect to a dev database, but that is not sufficient for my production needs.
Thank you for your time
Simon
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Now seems to be solved The issue was a combination of the run command in app platform and the last line of the docker file. When I removed the run command from the app platform config and added it as CMD to the Dockerfile I was able to get connection to the managed database.