R
Railway4mo ago
Krøn

FastAPI + Alembic + Docker cant detect database url env variable

I have a fastapi project that uses alembic and loads the env variables using pydantic_settings. My build always fails saying it cant find "SERVICE_DATABASE_URL" which is my db url. below is my dockerfile
FROM python:3.11

WORKDIR /app

COPY . /app

RUN pip install --no-cache-dir -r requirements.txt

EXPOSE 8001

ARG SERVICE_DATABASE_URL
RUN echo $SERVICE_DATABASE_URL


RUN alembic upgrade head

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8001"]
FROM python:3.11

WORKDIR /app

COPY . /app

RUN pip install --no-cache-dir -r requirements.txt

EXPOSE 8001

ARG SERVICE_DATABASE_URL
RUN echo $SERVICE_DATABASE_URL


RUN alembic upgrade head

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8001"]
4 Replies
Percy
Percy4mo ago
Project ID: eff98eea-4f33-41b5-be10-86353122045c
Krøn
Krøn4mo ago
eff98eea-4f33-41b5-be10-86353122045c error
#10 ERROR: process "/bin/sh -c alembic upgrade head" did not complete successfully: exit code: 1



-----

> [6/6] RUN alembic upgrade head:

0.874 settings = Configuration()

0.874 ^^^^^^^^^^^^^^^

0.874 File "/usr/local/lib/python3.11/site-packages/pydantic_settings/main.py", line 71, in __init__

0.874 super().__init__(

0.874 File "/usr/local/lib/python3.11/site-packages/pydantic/main.py", line 165, in __init__

0.875 __pydantic_self__.__pydantic_validator__.validate_python(data, self_instance=__pydantic_self__)

0.875 pydantic_core._pydantic_core.ValidationError: 1 validation error for Configuration

0.875 SERVICE_DATABASE_URL

0.875 Input should be a valid string [type=string_type, input_value=None, input_type=NoneType]

0.875 For further information visit https://errors.pydantic.dev/2.3/v/string_type

-----



Dockerfile:16

-------------------

14 | ENV SERRVICE_DATABASE_URL = ${SERRVICE_DATABASE_URL}

15 |

16 | >>> RUN alembic upgrade head

17 |

18 | CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8001"]

-------------------

ERROR: failed to solve: process "/bin/sh -c alembic upgrade head" did not complete successfully: exit code: 1
#10 ERROR: process "/bin/sh -c alembic upgrade head" did not complete successfully: exit code: 1



-----

> [6/6] RUN alembic upgrade head:

0.874 settings = Configuration()

0.874 ^^^^^^^^^^^^^^^

0.874 File "/usr/local/lib/python3.11/site-packages/pydantic_settings/main.py", line 71, in __init__

0.874 super().__init__(

0.874 File "/usr/local/lib/python3.11/site-packages/pydantic/main.py", line 165, in __init__

0.875 __pydantic_self__.__pydantic_validator__.validate_python(data, self_instance=__pydantic_self__)

0.875 pydantic_core._pydantic_core.ValidationError: 1 validation error for Configuration

0.875 SERVICE_DATABASE_URL

0.875 Input should be a valid string [type=string_type, input_value=None, input_type=NoneType]

0.875 For further information visit https://errors.pydantic.dev/2.3/v/string_type

-----



Dockerfile:16

-------------------

14 | ENV SERRVICE_DATABASE_URL = ${SERRVICE_DATABASE_URL}

15 |

16 | >>> RUN alembic upgrade head

17 |

18 | CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8001"]

-------------------

ERROR: failed to solve: process "/bin/sh -c alembic upgrade head" did not complete successfully: exit code: 1
Brody
Brody4mo ago
do you have the applicable service variable set on your service?
Krøn
Krøn4mo ago
yes, no worries i fixed it
FROM python:3.11

WORKDIR /app

COPY . /app

RUN pip install --no-cache -r requirements.txt

EXPOSE 8002

ARG SERVICE_DATABASE_URL
RUN echo $SERVICE_DATABASE_URL

"ENV SERVICE_DATABASE_URL=$SERVICE_DATABASE_URL" <===== HERE

RUN alembic upgrade head

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8002"]
FROM python:3.11

WORKDIR /app

COPY . /app

RUN pip install --no-cache -r requirements.txt

EXPOSE 8002

ARG SERVICE_DATABASE_URL
RUN echo $SERVICE_DATABASE_URL

"ENV SERVICE_DATABASE_URL=$SERVICE_DATABASE_URL" <===== HERE

RUN alembic upgrade head

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8002"]