Home

R Coding in VS Code via Docker Container

Ever tried to share your R code with a colleague, only to spend hours debugging “but it works on my machine” issues? Docker containers are like shipping containers for code—they package your R environment, libraries, and dependencies into a sealed box that works the same everywhere. Plus, you get access to tens of thousands of pre-built images on Docker Hub, where software developers publish ready-to-use environments, skipping the pain of manual software installation. This tutorial shows you how to run R in an isolated, reproducible environment using VS Code and Docker Desktop.

Key Concepts

What You’ll Need

Step 1: Install Docker Desktop

Step 2: Install Dev Containers Extension

Step 3: Clone the Vibe Project with GitHub Desktop

Step 4: Open Project in VS Code

Step 5: Reopen in Container

Note: The container automatically includes the R extension and languageserver package. The Dockerfile and devcontainer.json handle this for you.

Step 6: Understand the Container Environment

Now you’re coding inside a Linux container. Let’s explore what this means.

pwd

You’ll see /workspaces/vibe - this is your project folder inside the container.

ls

You’ll see the same files from the project: R/, .devcontainer/, README.md, etc.

cd ..
ls

You’ll only see vibe/ - the container is isolated. You can’t access your computer’s other folders, Desktop, or Documents. This isolation ensures your R environment is clean and reproducible.

cd vibe

Step 7: Run R Code Line by Line

The container has R pre-installed with common packages. Let’s run a simple data analysis script.

Step 8: Run the App

The project includes a demo Shiny app that creates an interactive histogram.

Step 9: Make a Simple Change

Let’s modify the app to see how development works.

titlePanel("My First R Docker App")

Step 10: Understanding the Dockerfile (Optional)

# choose a Dockerhub base image with R, Shiny Server, and tidyverse packages
FROM rocker/shiny-verse:latest

# 1. System deps commonly needed by R packages
RUN apt-get update && apt-get install -y \
    libcurl4-openssl-dev libssl-dev libxml2-dev git curl && \
    rm -rf /var/lib/apt/lists/*

# 2. R packages for VS Code integration: language server + debugger
RUN R -q -e 'install.packages(c("rstudioapi", "languageserver"), repos="https://cloud.r-project.org")'

# 3. Install Node.js LTS from NodeSource
RUN curl -fsSL https://deb.nodesource.com/setup_lts.x | bash - \
    && apt-get install -y nodejs \
    && npm install -g npm@latest
    
# 4. Install Claude Code globally
RUN npm install -g @anthropic-ai/claude-code

# 5. Give shiny passwordless sudo for updating Claude Code from VS Code
RUN echo 'shiny ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers && \
    rm -rf /var/lib/apt/lists/*

# Expose Shiny server port
EXPOSE 3838

Key parts:

Other Rocker images you can use:

After changing the base image, rebuild the container to apply changes.

Step 11: Install R Packages in the Docker Image (Optional)

Packages installed via the R console (install.packages()) are temporary and disappear when you rebuild the container. To make packages permanent, add them to the Dockerfile.

library(data.table)

If it loads without errors, the package is installed permanently.

Next Steps

Troubleshooting

Workflow Overview

This setup gives you a professional R development environment:

Everyday Workflow

Once everything is set up, here’s your daily routine:

  1. Start Docker Desktop - Open the app and wait for the green status indicator (Docker must be running)
  2. Open VS Code - Launch VS Code and open your project folder
  3. Reopen in Container - If not already in the container, click the green icon (bottom-left) and select Reopen in Container
  4. Write and run code - Edit .R files, run line-by-line with Ctrl+Enter/Cmd+Enter, or run Shiny apps with the ▶ Run Shiny App button
  5. Save your work - Your code files (.R, .Rmd) are saved to your computer and persist across sessions
  6. Commit and push - Use GitHub Desktop to commit your changes and push to the repository

What Is Docker Desktop?

Docker Desktop is an application that makes Docker easier to use on your computer. It provides the background services that Docker needs, along with a visual window where you can see and manage your containers, images, storage, and other Docker resources.

You can think of Docker Desktop as the control center for your containers. When you open Docker Desktop, it starts the Docker engine. The Docker engine is the part of Docker that actually creates and runs containers. You usually do not need to interact with the engine directly because Docker Desktop manages it for you.

What Does Docker Desktop Do?

Docker Desktop performs several important jobs:

How Docker Desktop Fits This Tutorial

There are three pieces working together:

  1. Docker Desktop runs the Docker engine on your computer.
  2. VS Code gives you the editor where you write and run R code.
  3. The Dev Container provides a prepared Linux environment containing R, R packages, and the tools used by this project.

When you choose Reopen in Container, VS Code asks Docker Desktop to build or start the project’s container. Your computer is still running the application, but the R environment is inside the container. This keeps the R version, packages, and system tools separate from the rest of your computer.

Docker Desktop does not replace VS Code, R, or your web browser. It provides the environment that runs the container. VS Code is where you work, R is the language you use, and your browser is where you view the Shiny app.

Why Use Docker Desktop?

Installing R and every required package directly on your computer can sometimes lead to version conflicts. One project may need a different package version from another project. Docker Desktop helps avoid these conflicts by giving each project its own controlled environment.

For example, if a classmate uses the same project files and Docker settings, Docker can build an environment that closely matches yours. This makes it less likely that you will see an error caused only by different software versions. Your R code and project files remain on your computer, while the tools needed to run them are organized inside the container.

Docker Desktop must be open and running before you build or reopen this project in a container. If Docker Desktop is closed, VS Code cannot start the container, and you may see an error saying that Docker is not running.


Created by Steven Ge on December 7, 2025.