From a47b17252c7afa8442a6fbf013eb0b781e3c4790 Mon Sep 17 00:00:00 2001 From: davidycliao Date: Sun, 12 Jan 2025 11:55:51 +0000 Subject: [PATCH] fix: update Docker environment configuration fix: update Docker environment configuration - Replace ENV with ARG for user credentials - Update Python virtual environment path to /opt/venv - Fix RETICULATE_PYTHON path configuration --- .github/workflows/docker-publish.yml | 154 ++++++++++++++++----------- Dockerfile | 52 ++++----- 2 files changed, 115 insertions(+), 91 deletions(-) diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml index 5a19daa7..e094ab9b 100644 --- a/.github/workflows/docker-publish.yml +++ b/.github/workflows/docker-publish.yml @@ -1,60 +1,94 @@ -FROM rocker/r-ver:latest - -# Install system dependencies -RUN apt-get update && apt-get install -y \ - python3-minimal \ - python3-pip \ - python3-venv \ - libssl-dev \ - gdebi-core \ - wget \ - sudo \ - curl - -# Create rstudio user -ARG USER=rstudio -ARG PASSWORD=rstudio123 -RUN useradd -m $USER && \ - echo "$USER:$PASSWORD" | chpasswd && \ - adduser $USER sudo && \ - echo "$USER ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers - -# Install RStudio Server -RUN wget https://download2.rstudio.org/server/jammy/amd64/rstudio-server-2023.12.1-402-amd64.deb && \ - gdebi -n rstudio-server-2023.12.1-402-amd64.deb && \ - rm rstudio-server-*.deb - -# Create and configure Python virtual environment -RUN python3 -m venv /opt/venv && \ - chown -R $USER:$USER /opt/venv - -ENV PATH="/opt/venv/bin:$PATH" -ENV RETICULATE_PYTHON="/opt/venv/bin/python" - -# Setup R environment config -RUN mkdir -p /usr/local/lib/R/etc && \ - echo "RETICULATE_PYTHON=/opt/venv/bin/python" >> /usr/local/lib/R/etc/Renviron.site && \ - echo "options(reticulate.prompt = FALSE)" >> /usr/local/lib/R/etc/Rprofile.site - -# Install Python packages -RUN /opt/venv/bin/pip install --no-cache-dir \ - numpy==1.26.4 \ - scipy==1.12.0 \ - transformers \ - torch \ - flair - -# Install R packages with proper setup -RUN R -e 'install.packages("reticulate", repos="https://cloud.r-project.org/", dependencies=TRUE)' && \ - R -e 'if(require(reticulate)) { \ - Sys.setenv(RETICULATE_PYTHON="/opt/venv/bin/python"); \ - reticulate::use_python("/opt/venv/bin/python", required=TRUE); \ - install.packages("remotes", repos="https://cloud.r-project.org/", dependencies=TRUE); \ - remotes::install_github("davidycliao/flaiR", dependencies=TRUE) \ - }' - -WORKDIR /home/$USER -USER $USER -EXPOSE 8787 - -CMD ["/usr/lib/rstudio-server/bin/rserver", "--server-daemonize=0"] +name: flaiR-Docker +on: + push: + branches: [main, master] + pull_request: + branches: [main, master] + + +jobs: + R-CMD-check: + runs-on: ubuntu-latest + name: R-CMD-check + env: + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + R_KEEP_PKG_SOURCE: yes + steps: + - uses: actions/checkout@v3 + - uses: r-lib/actions/setup-r@v2 + with: + use-public-rspm: true + - uses: r-lib/actions/setup-pandoc@v2 + + - name: Setup Python + uses: actions/setup-python@v2 + with: + python-version: '3.9' + + - name: Install Python dependencies + run: | + python -m pip install --upgrade pip + pip install flair + - name: Install R dependencies + run: | + install.packages('remotes') + remotes::install_github("davidycliao/flaiR", force = TRUE) + shell: Rscript {0} + + - uses: r-lib/actions/setup-r-dependencies@v2 + with: + extra-packages: rcmdcheck + + docker: + needs: R-CMD-check + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + + - name: Log in to GitHub Container Registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + with: + buildkitd-flags: --debug + + - name: Extract metadata for Docker + id: meta + uses: docker/metadata-action@v4 + with: + images: ghcr.io/${{ github.repository_owner }}/flair-rstudio + tags: | + type=ref,event=branch + type=ref,event=pr + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=sha + type=raw,value=latest,enable={{is_default_branch}} + - name: Build and push RStudio image + uses: docker/build-push-action@v4 + with: + context: . + platforms: linux/amd64 + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + build-args: | + DEFAULT_USER=rstudio + secrets: | + PASSWORD=${{ secrets.RSTUDIO_PASSWORD }} + cache-from: type=gha + cache-to: type=gha,mode=max + provenance: false diff --git a/Dockerfile b/Dockerfile index 075edd82..5a19daa7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,12 +9,11 @@ RUN apt-get update && apt-get install -y \ gdebi-core \ wget \ sudo \ - curl \ - virtualenv # 添加virtualenv + curl # Create rstudio user -ENV USER=rstudio -ENV PASSWORD=rstudio123 +ARG USER=rstudio +ARG PASSWORD=rstudio123 RUN useradd -m $USER && \ echo "$USER:$PASSWORD" | chpasswd && \ adduser $USER sudo && \ @@ -25,46 +24,37 @@ RUN wget https://download2.rstudio.org/server/jammy/amd64/rstudio-server-2023.12 gdebi -n rstudio-server-2023.12.1-402-amd64.deb && \ rm rstudio-server-*.deb -# 修改虚拟环境路径到用户目录 -ENV VENV_PATH=/home/$USER/flair_env -RUN python3 -m venv $VENV_PATH && \ - chown -R $USER:$USER $VENV_PATH +# Create and configure Python virtual environment +RUN python3 -m venv /opt/venv && \ + chown -R $USER:$USER /opt/venv -# 更新环境变量 -ENV PATH="$VENV_PATH/bin:$PATH" -ENV RETICULATE_PYTHON="$VENV_PATH/bin/python" +ENV PATH="/opt/venv/bin:$PATH" +ENV RETICULATE_PYTHON="/opt/venv/bin/python" # Setup R environment config RUN mkdir -p /usr/local/lib/R/etc && \ - echo "RETICULATE_PYTHON=$VENV_PATH/bin/python" >> /usr/local/lib/R/etc/Renviron.site && \ - chown -R $USER:$USER /usr/local/lib/R/etc/Renviron.site && \ - chmod 644 /usr/local/lib/R/etc/Renviron.site + echo "RETICULATE_PYTHON=/opt/venv/bin/python" >> /usr/local/lib/R/etc/Renviron.site && \ + echo "options(reticulate.prompt = FALSE)" >> /usr/local/lib/R/etc/Rprofile.site -# Install Python packages in the virtual environment -RUN $VENV_PATH/bin/pip install --no-cache-dir \ +# Install Python packages +RUN /opt/venv/bin/pip install --no-cache-dir \ numpy==1.26.4 \ scipy==1.12.0 \ transformers \ torch \ flair -# Install R packages -RUN R -e "install.packages('reticulate', repos='https://cloud.r-project.org/', dependencies=TRUE)" && \ - R -e "install.packages('remotes', repos='https://cloud.r-project.org/', dependencies=TRUE)" && \ - R -e "options(timeout=600); Sys.setenv(RETICULATE_PYTHON='$VENV_PATH/bin/python'); library(reticulate); use_virtualenv('$VENV_PATH', required = TRUE); remotes::install_github('davidycliao/flaiR', dependencies=TRUE)" +# Install R packages with proper setup +RUN R -e 'install.packages("reticulate", repos="https://cloud.r-project.org/", dependencies=TRUE)' && \ + R -e 'if(require(reticulate)) { \ + Sys.setenv(RETICULATE_PYTHON="/opt/venv/bin/python"); \ + reticulate::use_python("/opt/venv/bin/python", required=TRUE); \ + install.packages("remotes", repos="https://cloud.r-project.org/", dependencies=TRUE); \ + remotes::install_github("davidycliao/flaiR", dependencies=TRUE) \ + }' -# Set working directory WORKDIR /home/$USER -RUN chown -R $USER:$USER /home/$USER - -# Add healthcheck -HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ - CMD curl -f http://localhost:8787/ || exit 1 - -# Expose port +USER $USER EXPOSE 8787 -# Run service as rstudio user -USER $USER -CMD ["/usr/lib/rstudio-server/bin/rserver", "--server-daemonize=0"] CMD ["/usr/lib/rstudio-server/bin/rserver", "--server-daemonize=0"]