0

I am trying to containerize my Reflex app (python) using Docker

Requirements.txt

reflex==0.4.9
<INTERNAL_PACKAGE_NAME> @ git+https://<GITHUB_TOKEN>:[email protected]/<ORGANIZATION>/<REPO_NAME>.git

Dockerfile

# This Dockerfile is used to deploy a single-container Reflex app instance
# to services like Render, Railway, Heroku, GCP, and others.

# It uses a reverse proxy to serve the frontend statically and proxy to backend
# from a single exposed port, expecting TLS termination to be handled at the
# edge by the given platform.
FROM python:3.12

# If the service expects a different port, provide it here (f.e Render expects port 10000)
ARG PORT=8080
# Only set for local/direct access. When TLS is used, the API_URL is assumed to be the same as the frontend.
ARG API_URL

# Define arguments
ARG DB_HTTP
ARG DB_SERVER
ARG ACCESS_TOKEN
 
# Set environment variables
ENV DB_HTTP=$DB_HTTP
ENV DB_SERVER=$DB_SERVER
ENV ACCESS_TOKEN=$ACCESS_TOKEN

ENV PORT=$PORT API_URL=${API_URL:-http://localhost:$PORT}

# Install Caddy server inside image
RUN apt-get update -y && apt-get install -y caddy && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Create a simple Caddyfile to serve as reverse proxy
RUN cat > Caddyfile <<EOF
:{\$PORT}

encode gzip

@backend_routes path /_event/* /ping /_upload /_upload/*
handle @backend_routes {
    reverse_proxy localhost:8000
}

root * /srv
route {
    try_files {path} {path}/ /404.html
    file_server
}
EOF

# Copy local context to `/app` inside container (see .dockerignore)
COPY . .

# Install app requirements and reflex in the container
RUN pip install -r requirements.txt

# Deploy templates and prepare app
RUN reflex init

# Download all npm dependencies and compile frontend
RUN reflex export --frontend-only --no-zip && mv .web/_static/* /srv/ && rm -rf .web

# Needed until Reflex properly passes SIGTERM on backend.
STOPSIGNAL SIGKILL

EXPOSE $PORT

# Apply migrations before starting the backend.
CMD [ -d alembic ] && reflex db migrate; \
    caddy start && reflex run --env prod --backend-only --loglevel debug 

And this is the GITHUB Internal repo package file:

enter image description here

Image words covered are DB. Same as the DB_HTTP and DB_SERVER.

The problem is in RUN reflex export --frontend-only --no-zip && mv .web/_static/* /srv/ && rm -rf .web command where it is unable to find the environment variable needed in the Internal Package GITHUB REPO.

Not able to understand why even after giving the environment variables in Dockerfile it is still not able to pick it up and pass it to the self.server, self.http_path, and the self.access_token. What it gives the error is the AttributeError: 'NoneType' object has no attribute 'startswith' from that self.http_path value.

I tried checking the container environment variables and found out that the environment variables exist but still the issue occurs.

2
  • how are you building the image? also why cross out the environment variable names, aren't they the same as in your dockerfile?
    – gold_cy
    Commented May 15 at 2:17
  • Hi @gold_cy, yes, I have updated the question, thanks for that, the environment variable names are the same as in the dockerfile. And this is the build command: docker build -t ACR_REGISTRY.azurecr.io/APP_NAME:v0.0.1 --build-arg DB_HTTP=<SECRET_1> --build-arg DB_SERVER=<SECRET_2> --build-arg ACCESS_TOKEN=<SECRET_3> . Commented May 15 at 4:46

0

Browse other questions tagged or ask your own question.