Closed nick-youngblut closed 5 months ago
It would be great to have instructions at https://reflex.dev/docs/getting-started/installation/ on how to set up a dockerfile (& VS Code devcontainer) for development of reflex apps.
I agree with @nick-youngblut, it would be nice to have a devcontainer example.
I have everything working with the Dockerfile(s), docker-compose.yaml, etc. found in /docker-example
, but I can't get debugging to work with the given devcontainer.json
. I've also tried with my own devcontainer.json
, running the docker-compose.yaml, but breakpoints don't work. I've read this could be a Python3.11 issue but an official example would clear things up.
@nick-youngblut Actually after spending a good amount of time I think I just got it to work!
First I've added debugpy==1.8.0
to requirements.txt
and to my main __init__.py
:
import debugpy
debugpy.listen(("0.0.0.0", 5678))
Using the files in /docker-example, modifying the Dockerfile
a tiny bit in stage 2:
# Stage 1: init
FROM python:3.11 as init
# Pass `--build-arg API_URL=http://app.example.com:8000` during build
ARG API_URL
# Copy local context to `/app` inside container (see .dockerignore)
WORKDIR /app
COPY . .
# Create virtualenv which will be copied into final container
ENV VIRTUAL_ENV=/app/.venv
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
RUN python3.11 -m venv $VIRTUAL_ENV
# Install app requirements and reflex inside virtualenv
RUN pip install -r requirements.txt
# Deploy templates and prepare app
RUN reflex init
# Export static copy of frontend to /app/.web/_static
RUN reflex export --frontend-only --no-zip
# Copy static files out of /app to save space in backend image
RUN mv .web/_static /tmp/_static
RUN rm -rf .web && mkdir .web
RUN mv /tmp/_static .web/_static
# Stage 2: copy artifacts into slim image
FROM python:3.11-slim
ARG API_URL
WORKDIR /app
RUN adduser --disabled-password --home /app reflex
COPY --chown=reflex --from=init /app /app
# added this to avoid permission error
RUN chmod -R 777 /app
# moved this part around a bit
USER reflex
ENV PATH="/app/.venv/bin:$PATH" API_URL=$API_URL
CMD reflex db migrate && reflex run --env prod --backend-only
The compose.yaml
is identical but added ports: - 5678:5678
to the app
service.
My devcontainer.json
:
{
"name": "Existing Docker Compose (Extend)",
"dockerComposeFile": "../compose.yaml",
"service": "app",
"workspaceFolder": "/app",
}
And finally my launch.json
:
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug",
"type": "python",
"request": "attach",
"connect": {
"host": "localhost", \\ also works with "host.docker.internal"
"port": 5678
},
"pathMappings": [
{
"localRoot": "${workspaceRoot}",
"remoteRoot": "/app"
}
]
}
]
}
The debugger is stopping at the breakpoints!
Describe the bug
To Reproduce
devcontainer.json:
Dockerfile:
env.yaml:
Specifics (please complete the following information):