From 66396aeb6f7c89eae2acf57589a6f9141ffb95d5 Mon Sep 17 00:00:00 2001 From: "A.G. Bocsardi" Date: Wed, 15 May 2024 12:33:39 +0200 Subject: [PATCH] first working version --- .devcontainer/devcontainer.json | 21 +++++++++++++ .gitignore | 17 ++++++++++ Dockerfile | 50 ++++++++++++++++++++++++++++++ README.md | 55 ++++++++++++++++++++++++++++++++- install_packages.R | 2 ++ requirements.txt | 4 +++ 6 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 .devcontainer/devcontainer.json create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 install_packages.R create mode 100644 requirements.txt diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..1648248 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,21 @@ +{ + "name": "Python and R Environment", + "build": { + "dockerfile": "../Dockerfile", + "options": [ + "--platform=linux/amd64" + ] + }, + "workspaceFolder": "/workspace", + "settings": { + "terminal.integrated.shell.linux": "/bin/bash" + }, + "extensions": [ + "ms-python.python", + "ikuyadeu.r", + "ms-azuretools.vscode-docker" + ], + // "forwardPorts": [ + // 8787 + // ] +} \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d64e264 --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# R data files +*.RData +*.rdata + +# RStudio files +.Rproj.user/ + +# Data files +data/* + +# VS Code settings +.vscode/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2be1708 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,50 @@ +FROM ubuntu:22.04 + +USER root + +ENV DEBIAN_FRONTEND noninteractive + +## INSTALL R +RUN apt update -y \ + && apt upgrade -y \ + && apt install -y \ + software-properties-common \ + dirmngr \ + gnupg2 \ + wget + +RUN wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc \ + | tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc \ + && add-apt-repository -y "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/" \ + && apt-get update -y \ + && apt-get install -y r-base + + +# Install RStudio Server for Ubuntu 22.04 +RUN wget https://download2.rstudio.org/server/jammy/amd64/rstudio-server-2023.09.0-463-amd64.deb && \ + apt-get install -y gdebi-core && \ + gdebi -n rstudio-server-2023.09.0-463-amd64.deb && \ + rm rstudio-server-2023.09.0-463-amd64.deb + +EXPOSE 8787 + + +RUN useradd -m -s /bin/bash -d /home/rstudio rstudio && \ + echo "rstudio:rstudio" | chpasswd && adduser rstudio sudo + +# install python +RUN apt-get install -y python3 python3-pip python3-dev python3-venv + + +# # Install Python packages +WORKDIR /workspace + + +COPY requirements.txt /workspace/ +RUN pip3 install --no-cache-dir -r requirements.txt + +COPY install_packages.R /workspace/ +RUN Rscript install_packages.R + + +CMD /usr/lib/rstudio-server/bin/rserver --server-daemonize=0 \ No newline at end of file diff --git a/README.md b/README.md index 35e7443..d1f21de 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,55 @@ # r_python_docker_template -This is my template for projects where I have to use both R and Python, and want to dockerize my environment. + +This repository is a template for creating a Dockerized development environment that includes the latest versions of Python and R, along with RStudio Server. It is designed to facilitate mixed-language projects and make development easier and more reproducible. + +## Getting Started + +### Prerequisites + +- Docker +- Visual Studio Code +- Remote - Containers Extension for VS Code + +### Setup + +1. Clone this repository: + ```sh + git clone https://github.com/gbocsardi/r_python_docker_template.git + cd r_python_docker_template + ``` + +2. Build container: +```sh + docker build -t python-r-env --platform linux/amd64 . +``` + +3. Run the container manually: +```sh +docker run --platform linux/amd64 -it --rm \ + -v $(pwd)/data:/workspace/data \ + -v $(pwd)/code:/workspace/code \ + -p 8787:8787 \ + python-r-env +``` +### Usage + +**Make sure your container is running!** + +#### VS Code for Python +1. Open VS Code, and hit CMD+Shift+P to open the command palette. +2. Seach for "Dev Containers: Attach to Running Container..." +3. From the opened context menu select your container. +4. You're in your `workspace` directory, the parent directory of `code/` and `data/`. + +#### RStudi Server for R + +- You can access RStudio Server at `http://localhost:8787`. +- Login credentials are `rstudio` for both username and password (without quotes). +- You're in your `workspace` directory, the parent directory of `code/` and `data/`. + +### Customization + +- **Adding Python Packages**: Update the `requirements.txt` file with the required packages. +- **Adding R Packages**: Update the `install_packages.R` file with the required packages. + + diff --git a/install_packages.R b/install_packages.R new file mode 100644 index 0000000..26bd8f8 --- /dev/null +++ b/install_packages.R @@ -0,0 +1,2 @@ +install.packages("dplyr") +install.packages("ggplot2") diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..f30f7b3 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +numpy +pandas +matplotlib + -- 2.51.2