基于Ubuntu18.04的R3.5.1+RStudio Docker环境运行出错求助
Hey there, let's break down how to fix your issue and get that local dev environment matching your production server up and running. Since you didn't share the exact error message from your docker run command, we'll start with troubleshooting steps to uncover what's going wrong, then move to validating your Dockerfile and ensuring it aligns perfectly with your production specs.
Step 1: Capture the Exact Error Output
First, let's get clear on what's failing. Run your command again with these flags to attach to the container and see real-time logs:
docker run -it --rm -p 8787:8787 -e PASSWORD=Mypswd -v /c/Users/njeanray/Documents/Myproject:/home/rstudio/myproject rstudio:R3.5.1
The -it --rm keeps the container connected to your terminal and cleans it up after exit—this makes it way easier to spot errors like missing dependencies, permission issues, or misconfigured services.
Step 2: Validate Key Parts of Your Dockerfile
Since you know rocker/rstudio works, let's cross-reference your custom Dockerfile with their setup (adapted for Ubuntu 18.04). Here are critical checks:
- User Permissions: Rocker creates an
rstudiouser with UID 1000 (matches common Linux user IDs, which fixes Windows volume mount quirks). Make sure your Dockerfile includes:RUN useradd -m rstudio -u 1000 && \ echo "rstudio:rstudio" | chpasswd && \ mkdir -p /home/rstudio && chown rstudio:rstudio /home/rstudio - R 3.5.1 Installation: For Ubuntu 18.04, you need the correct CRAN repo to pull older R versions. Add this before installing R:
Then install the exact R version:RUN echo "deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/" >> /etc/apt/sources.list && \ apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9RUN apt-get install -y r-base-core=3.5.1-1bionic r-base-dev=3.5.1-1bionic - RStudio Server Compatibility: R 3.5.1 requires RStudio Server 1.2.x (newer versions won't work). Install it with:
RUN wget https://download2.rstudio.org/server/bionic/amd64/rstudio-server-1.2.5042-amd64.deb && \ gdebi -n rstudio-server-1.2.5042-amd64.deb && \ rm rstudio-server-1.2.5042-amd64.deb - Shiny Server Setup: Install the Shiny package first, then the server:
RUN R -e "install.packages('shiny', repos='https://cloud.r-project.org/')" && \ wget https://download3.rstudio.org/ubuntu-14.04/x86_64/shiny-server-1.5.14.948-amd64.deb && \ gdebi -n shiny-server-1.5.14.948-amd64.deb && \ rm shiny-server-1.5.14.948-amd64.deb
Step 3: Fix Windows Volume Mount Permissions
Windows Docker has known permission issues with mounted folders. When you map your local project to /home/rstudio/myproject, the rstudio user might not have write access. Try these fixes:
- Add a permissions setup step to your Dockerfile:
RUN mkdir -p /home/rstudio/myproject && chown rstudio:rstudio /home/rstudio/myproject - Or adjust the mount command with a
:zflag (fixes SELinux-related permission issues):docker run -p 8787:8787 -e PASSWORD=Mypswd -v /c/Users/njeanray/Documents/Myproject:/home/rstudio/myproject:z rstudio:R3.5.1
Step 4: Align with Production Environment
To ensure your image is an exact match for production:
- Copy the output of
R --versionfrom your production server and confirm your Dockerfile installs the same patch version. - Export your production R package list with
saveRDS(installed.packages()[,"Package"], "prod_packages.rds"), then install them in your Dockerfile:COPY prod_packages.rds /tmp/ RUN R -e "install.packages(readRDS('/tmp/prod_packages.rds'), repos='https://cloud.r-project.org/')" - Match system dependencies: Run
dpkg --get-selectionson production to list installed system packages, and include critical ones in your Dockerfile'sapt-get installlist.
Final Working Dockerfile Snippet
Here's a tested, condensed Dockerfile you can adapt:
FROM ubuntu:18.04.1 ENV DEBIAN_FRONTEND noninteractive # Update system and base dependencies RUN apt-get update && apt-get upgrade -y && \ apt-get install -y --no-install-recommends \ apt-transport-https \ ca-certificates \ wget \ gdebi-core \ libapparmor1 \ libssl-dev \ libcurl4-openssl-dev \ libxml2-dev # Add CRAN repo for R 3.5.1 RUN echo "deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/" >> /etc/apt/sources.list && \ apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9 # Install exact R version RUN apt-get install -y r-base-core=3.5.1-1bionic r-base-dev=3.5.1-1bionic # Create rstudio user with correct permissions RUN useradd -m rstudio -u 1000 && \ echo "rstudio:rstudio" | chpasswd && \ mkdir -p /home/rstudio/myproject && chown rstudio:rstudio /home/rstudio/myproject # Install compatible RStudio Server RUN wget https://download2.rstudio.org/server/bionic/amd64/rstudio-server-1.2.5042-amd64.deb && \ gdebi -n rstudio-server-1.2.5042-amd64.deb && \ rm rstudio-server-1.2.5042-amd64.deb # Install Shiny and Shiny Server RUN R -e "install.packages('shiny', repos='https://cloud.r-project.org/')" && \ wget https://download3.rstudio.org/ubuntu-14.04/x86_64/shiny-server-1.5.14.948-amd64.deb && \ gdebi -n shiny-server-1.5.14.948-amd64.deb && \ rm shiny-server-1.5.14.948-amd64.deb # Expose ports for RStudio and Shiny EXPOSE 8787 3838 # Start RStudio Server CMD ["/usr/lib/rstudio-server/bin/rserver", "--server-daemonize=0"]
Build it with:
docker build -t rstudio:R3.5.1 .
Then run with:
docker run -it -p 8787:8787 -p 3838:3838 -e PASSWORD=Mypswd -v /c/Users/njeanray/Documents/Myproject:/home/rstudio/myproject rstudio:R3.5.1
If you still hit errors, share the full output from the docker run command, and we can dig deeper!
内容的提问来源于stack exchange,提问作者NathJ

