reflex-dev / reflex

🕸️ Web apps in pure Python 🐍
https://reflex.dev
Apache License 2.0
20.48k stars 1.18k forks source link

reflex init => error: failed to save lockfile: AccessDenied #1687

Closed nick-youngblut closed 5 months ago

nick-youngblut commented 1 year ago

Describe the bug

reflex run --loglevel debug
────────────────────────────────────────────────────────────────────────────────────────────────────────── System Info ──────────────────────────────────────────────────────────────────────────────────────────────────────────
Debug: Config file: '/workspaces/NGS_order_submission/rxconfig.py'
Debug: Config: app_name='NGS_order_submission' loglevel=<LogLevel.INFO: 'info'> frontend_port=3000 backend_port=8000 api_url='http://localhost:8000' deploy_url='http://localhost:3000' backend_host='0.0.0.0' 
db_url='sqlite:///reflex.db' redis_url=None telemetry_enabled=True bun_path='/home/dev_user/.reflex/.bun/bin/bun' cors_allowed_origins=['*'] tailwind=None timeout=120 next_compression=True event_namespace=None 
frontend_packages=[] rxdeploy_url=None username=None
Debug: Running command: ['/home/dev_user/.reflex/.nvm/versions/node/v18.17.0/bin/node', '-v']
Debug: Running command: ['/home/dev_user/.reflex/.bun/bin/bun', '-v']
Debug: [Reflex 0.2.6 with Python 3.9.16 (PATH: /opt/conda/bin/python)]
Debug: [Node 18.17.0 (Expected: 18.17.0) (PATH:/home/dev_user/.reflex/.nvm/versions/node/v18.17.0/bin/node)]
Debug: [NVM 0.39.1 (Expected: 0.39.1) (PATH: /home/dev_user/.reflex/.nvm/nvm.sh)]
Debug: [Bun 0.7.0 (Expected: 0.7.0) (PATH: /home/dev_user/.reflex/.bun/bin/bun)]
Debug: [OS Linux Debian GNU/Linux 11 (bullseye)]
Debug: Using package installer at: /home/dev_user/.reflex/.bun/bin/bun
Debug: Using package executer at: /home/dev_user/.reflex/.nvm/versions/node/v18.17.0/bin/npm
Debug: Unzip path: /usr/bin/unzip
────────────────────────────────────────────────────────────────────────────────────────────────────── Starting Reflex App ──────────────────────────────────────────────────────────────────────────────────────────────────────
Compiling:  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 1/1 0:00:00
Debug: Running command: ['/home/dev_user/.reflex/.bun/bin/bun', 'install', '--loglevel', 'silly']
Debug: Installing base frontend packages
Debug: bun install v0.7.0 (aa1ad7f0)
Debug:  Resolving dependencies
Debug:  Resolved, downloaded and extracted [2780]
Debug: error: failed to save lockfile: AccessDenied
Installing base frontend packages failed with exit code 1
bun install v0.7.0 (aa1ad7f0)
 Resolving dependencies
 Resolved, downloaded and extracted [2780]
error: failed to save lockfile: AccessDenied
Run with --loglevel debug  for the full log.

To Reproduce

devcontainer.json:

{
    "name": "arc_challenge",
    "build": {
        "dockerfile": "Dockerfile"
    },
    "remoteUser": "dev_user",
    "customizations": {
        "vscode": {
            "settings": {
                "python.defaultInterpreterPath": "/opt/conda/bin/python",
                "python.linting.enabled": true,
                "python.linting.pylintEnabled": true,
                "python.formatting.autopep8Path": "/opt/conda/bin/autopep8",
                "python.formatting.yapfPath": "/opt/conda/bin/yapf",
                "python.linting.flake8Path": "/opt/conda/bin/flake8",
                "python.linting.pycodestylePath": "/opt/conda/bin/pycodestyle",
                "python.linting.pydocstylePath": "/opt/conda/bin/pydocstyle",
                "python.linting.pylintPath": "/opt/conda/bin/pylint"
            },
            "extensions": ["ms-python.python", 
                           "ms-python.vscode-pylance", 
                           "EditorConfig.EditorConfig",
                           "codezombiech.gitignore",
                           "Gruntfuggly.todo-tree", 
                           "redhat.vscode-yaml", 
                           "streetsidesoftware.code-spell-checker",
                           "ms-azuretools.vscode-docker", 
                           "george-alisson.html-preview-vscode"]
        }
        },
        "features": {
            "ghcr.io/devcontainers/features/docker-in-docker:2": {
            "version": "latest"
        }
    }
 }

Dockerfile:

FROM mambaorg/micromamba:1.1.0

USER root
RUN apt-get update \
  && apt-get install -y build-essential procps git wget sudo unzip vim tree htop \
  && apt-get clean \
  && apt-get purge \
  && rm -rf /var/lib/apt/lists/* /tmp/*

RUN useradd -ms /bin/bash dev_user \
  && echo "dev_user ALL=(root) NOPASSWD:ALL" > /etc/sudoers.d/dev_user \
  && chmod 0440 /etc/sudoers.d/dev_user
USER dev_user

COPY --chown=dev_user:dev_user env.yaml /tmp/env.yaml
RUN micromamba install -y -n base -f /tmp/env.yaml \
  && micromamba clean --all --yes

WORKDIR /data

env.yaml:

channels:
  - conda-forge
  - bioconda
  - defaults
dependencies:
  - python=3.9.16
  - pip
  - pandas
  - plotly
  - pydantic
  - pandera
  - pip:
    - reflex

Specifics (please complete the following information):

nick-youngblut commented 1 year 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.

Jsalaz1989 commented 11 months ago

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.

Jsalaz1989 commented 11 months ago

@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!