Skip to content

Instantly share code, notes, and snippets.

@mokztk
Last active June 5, 2022 23:50
Show Gist options
  • Save mokztk/37f6806e0d8734a500ab1ff766eff53b to your computer and use it in GitHub Desktop.
Save mokztk/37f6806e0d8734a500ab1ff766eff53b to your computer and use it in GitHub Desktop.
[2022-06-06 UpDate] rocker/r-ver:4.1.3 に code-server (https://github.com/coder/code-server) を追加した docker image

About this image

r-ver:4.1.3 をベースに、UIとして code-server を導入したもの。ARM64 の環境でも使える解析環境を目指す。

  • rocker/r-ver:4.1.3
    • CRAN repository は Public Rstudio Package Manager の 2022-04-21 で固定されている
  • code-server
    • https://github.com/coder/code-server のインストールスクリプトを使用
    • Extensions:
      • R Extension for Visual Studio Code (Ikuyadeu.r)
      • R Debugger (RDebugger.r-debugger)
      • R LSP Client for VS Code (REditorSupport.r-lsp) 2022-06-02 削除
    • エディタフォントは UDEV Gothic (BIZ UD Gothic + JetBrains Mono)のリガチャ対応版を使用
    • RStudio のように Ctrl + Shift + m で %>% を入力できるように設定
    • Plot は {httpgd} を使って表示するよう設定(0.0.0.0:59531 を使用)
  • Ubuntu mirror
    • 自動選択の mirror://mirrors.ubuntu.com/mirrors.txt に変更
    • x86_64 の場合は日本のミラーサーバーで一番回線が太い ICSCoE(IPA産業サイバーセキュリティセンター)に変更
    • Ref: https://launchpad.net/ubuntu/+archivemirrors
  • 日本語ロケール
    • Ubuntu の language-pack-ja, language-pack-ja-base
    • 環境変数で ja_JP.UTF-8 ロケールとタイムゾーン Asia/Tokyo を指定
    • フォントは容量節約のためパッケージを使わず下記を手動で追加
      • Noto Sans/Serif JP(Google Fonts で配布されている日本語サブセット版)
      • UDEV Gothic LG(BIZ UD Gothic + JetBrains Mono のリガチャ対応版)
  • R pachages
    • rocker/tidyverse に導入されているものから、容量の大きな database backend を省略したもの
    • 関連して必要となる Pandoc は、Ubuntu 20.04LTS のものはバージョンが古いので公式サイトの deb を使用
    • 個人的な頻用パッケージも追加しておく
    • install2.r --ncpus -1 ... で並列化すると、ARM64でうまくいかない場合があった(big.LITTLE構成のため?)ので --ncpus 指定を削除
  • radian: A 21 century R console
  • Python
    • Python3 のインストールには /rocker_scripts/install_python.sh を使用
    • グローバルに pandas と matplotlib/seaborn を入れておく

rocker-org/rocker-versioned2 のように、目的別のスクリプトを使って Dockerfile 自体は極力シンプルにしてみる。

Gist ではディレクトリが使えないので、各インストールスクリプトは "my_scripts__*" として保存してある。
docker image build の際は Dockerfile と同じ階層の "my_scripts" というディレクトリに "install_*.sh" と改名して格納しておく。改行コードが LF(UNIX) でないとエラーになるので注意。

unzip {id_of_this_gist}.zip
cd {id_of_this_gist}
mkdir my_scripts
find my_scripts* | sed -e 's%\(my_scripts__\(.*\)\)%mv \1 my_scripts\/\2%g' | sh
docker image build -t "mokztk/rcodeserver:4.1.3" .
# rocker/r-ver:4.1.3 に Radian + Code-server と頻用パッケージを追加する
# CRAN snapshot: https://packagemanager.rstudio.com/cran/__linux__/focal/2022-04-21
FROM rocker/r-ver:4.1.3
# Ubuntuミラーサイトの設定
#RUN sed -i.bak -e 's%http://[^ ]\+%mirror://mirrors.ubuntu.com/mirrors.txt%g' /etc/apt/sources.list
RUN if [ `uname -m` = "x86_64" ]; then \
sed -i.bak -e "s%http://[^ ]\+%http://ftp.udx.icscoe.jp/Linux/ubuntu/%g" /etc/apt/sources.list; \
fi
# 日本語設定と必要なライブラリ(Rパッケージ用は別途スクリプト内で導入)
RUN set -x \
&& apt-get update \
&& apt-get install -y --no-install-recommends \
language-pack-ja-base \
ssh \
libxt6 \
patch \
&& /usr/sbin/update-locale LANG=ja_JP.UTF-8 LANGUAGE="ja_JP:ja" \
&& /bin/bash -c "source /etc/default/locale" \
&& ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
# setup script
# 各スクリプトは改行コード LF(UNIX) でないとエラーになる
COPY my_scripts /my_scripts
RUN chmod 775 my_scripts/*
RUN /my_scripts/install_tidyverse.sh
RUN /my_scripts/install_r_packages.sh
RUN /my_scripts/install_radian.sh
RUN /my_scripts/install_codeserver.sh
RUN /my_scripts/install_fonts.sh
# non-root user を作成
# Ref: https://github.com/rocker-org/rocker-versioned2/blob/master/scripts/default_user.sh
ENV DEFAULT_USER=rserver
RUN useradd -s /bin/bash -m "$DEFAULT_USER" \
&& echo "${DEFAULT_USER}:${DEFAULT_USER}" | chpasswd \
&& addgroup "${DEFAULT_USER}" staff \
&& chown -R ${DEFAULT_USER}:${DEFAULT_USER} /home/${DEFAULT_USER}
USER ${DEFAULT_USER}
RUN /my_scripts/setup_codeserver.sh
# ${R_HOME}/etc/Renviron のタイムゾーン指定(Etc/UTC)を上書き
RUN echo "TZ=Asia/Tokyo" >> /home/${DEFAULT_USER}/.Renviron
ENV LANG=ja_JP.UTF-8 \
LC_ALL=ja_JP.UTF-8 \
TZ=Asia/Tokyo
EXPOSE 8080
EXPOSE 59531
CMD /usr/bin/code-server /home/${DEFAULT_USER}
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<alias>
<family>serif</family>
<prefer>
<family>Noto Serif JP</family>
</prefer>
</alias>
<alias>
<family>sans-serif</family>
<prefer>
<family>Noto Sans JP</family>
</prefer>
</alias>
<alias>
<family>Noto Serif CJK JP</family>
<prefer>
<family>Noto Serif JP</family>
</prefer>
</alias>
<alias>
<family>Noto Sans CJK JP</family>
<prefer>
<family>Noto Sans JP</family>
</prefer>
</alias>
</fontconfig>
#!/bin/bash
set -x
apt-get update
# Code-server のインストール(セットアップは別にユーザーアカウントで行う)
curl -fsSL https://code-server.dev/install.sh | sh
# aptキャッシュを消去
apt-get clean
rm -rf /var/lib/apt/lists/*
# R Language server の導入
install2.r languageserver
#!/bin/bash
set -x
# Noto Sans/Serif JP フォントのインストール
# Google Fonts から日本語フォントのみダウンロードして手動でインストールする
# フォント名は、以前の Noto Sans/Serif CJK JP から "CJK" なしの Noto Sans/Serif JP になった
wget -q -O NotoSansJP.zip https://fonts.google.com/download?family=Noto%20Sans%20JP
wget -q -O NotoSerifJP.zip https://fonts.google.com/download?family=Noto%20Serif%20JP
unzip NotoSansJP.zip NotoSans*.otf
unzip NotoSerifJP.zip NotoSerif*.otf
mkdir -p /usr/share/fonts/notojp
mv NotoSerifJP-Light.otf /usr/share/fonts/notojp/
mv NotoSerifJP-Regular.otf /usr/share/fonts/notojp/
mv NotoSerifJP-Bold.otf /usr/share/fonts/notojp/
mv NotoSansJP-Regular.otf /usr/share/fonts/notojp/
mv NotoSansJP-Bold.otf /usr/share/fonts/notojp/
mv NotoSansJP-Black.otf /usr/share/fonts/notojp/
mv NotoSansJP-Medium.otf /usr/share/fonts/notojp/
rm -rf Noto*
# Coding font として、UDEV Gothic LG (BIZ UD Gothic + JetBrains Mono) https://github.com/yuru7/udev-gothic/ をインストール
# 半角:全角 1:2、リガチャ有効バージョンを使用
wget -q https://github.com/yuru7/udev-gothic/releases/download/v1.0.0/UDEVGothic_v1.0.0.zip
unzip UDEVGothic_v1.0.0.zip
mkdir -p /usr/share/fonts/UDEVGothic
mv UDEVGothic_v1.0.0/UDEVGothicLG-Regular.ttf /usr/share/fonts/UDEVGothic/
mv UDEVGothic_v1.0.0/UDEVGothicLG-Italic.ttf /usr/share/fonts/UDEVGothic/
mv UDEVGothic_v1.0.0/UDEVGothicLG-Bold.ttf /usr/share/fonts/UDEVGothic/
mv UDEVGothic_v1.0.0/UDEVGothicLG-BoldItalic.ttf /usr/share/fonts/UDEVGothic/
rm -rf UDEVGothic*
# 標準フォント sans/serif として手動で入れた Noto fonts を認識できるようにする
# Noto Sans/Serif CJK JP を Noto Sans/Serif JP の別名として登録しておく(過去のコードの文字化け回避)
# 設定しておけば、最低限グラフの文字化けはなくなる
cp /my_scripts/fonts.conf /etc/fonts/local.conf
chmod 644 /usr/share/fonts/notojp/*
chmod 644 /usr/share/fonts/UDEVGothic/*
fc-cache -fv
# Code-server で UDEV Gothic LG を使えるようにする
# Refs: https://qiita.com/BonyChops/items/2f78ec57d55db63cb7e3
# https://github.com/coder/code-server/issues/1374
ln -s /usr/share/fonts/UDEVGothic/*.ttf /usr/lib/code-server/src/browser/media/
patch -u /usr/lib/code-server/lib/vscode/out/vs/code/browser/workbench/workbench.html << EOF
@@ -32,6 +32,33 @@
<link rel="manifest" href="{{VS_BASE}}/manifest.json" crossorigin="use-credentials" />
<link data-name="vs/workbench/workbench.web.main" rel="stylesheet" href="{{VS_BASE}}/static/out/vs/workbench/workbench.web.main.css">
+ <!-- Custom fonts -->
+ <style>
+ @font-face {
+ font-family: "UDEV Gothic LG";
+ src: url("{{BASE}}/_static/src/browser/media/UDEVGothicLG-Regular.ttf") format("truetype");
+ font-weight: 400;
+ font-style: normal;
+ }
+ @font-face {
+ font-family: "UDEV Gothic LG";
+ src: url("{{BASE}}/_static/src/browser/media/UDEVGothicLG-Italic.ttf") format("truetype");
+ font-weight: 400;
+ font-style: italic;
+ }
+ @font-face {
+ font-family: "UDEV Gothic LG";
+ src: url("{{BASE}}/_static/src/browser/media/UDEVGothicLG-Bold.ttf") format("truetype");
+ font-weight: 700;
+ font-style: normal;
+ }
+ @font-face {
+ font-family: "UDEV Gothic LG";
+ src: url("{{BASE}}/_static/src/browser/media/UDEVGothicLG-BoldItalic.ttf") format("truetype");
+ font-weight: 700;
+ font-style: italic;
+ }
+ </style>
</head>
<body aria-label="">
EOF
#!/bin/bash
set -x
# 依存ライブラリ
apt-get update
apt-get install -y --no-install-recommends \
libcairo2-dev \
libcurl4-openssl-dev \
libfontconfig1-dev \
libfreetype6-dev \
libgl1-mesa-dev \
libglu1-mesa-dev \
libjpeg-dev \
libpng-dev \
libssl-dev \
libxft-dev \
zlib1g-dev
apt-get clean
rm -rf /var/lib/apt/lists/*
# RSPMのcheckpointが変わった場合に対応するため、まずcheckpointの状態まで更新する
Rscript -e "update.packages(ask = FALSE)"
# CRANパッケージをRSPMからインストール
# --deps TRUE をつけると依存関係 Suggests までインストールされ膨大になる
install2.r --error --skipinstalled \
pacman \
here \
tidylog \
furrr \
glmnetUtils \
pROC \
cmprsk \
psych \
clinfun \
car \
survminer \
GGally \
ggfortify \
gghighlight \
ggsci \
ggrepel \
patchwork \
tableone \
gt \
gtsummary \
flextable \
formattable \
ftExtra \
minidown \
palmerpenguins \
svglite \
httpgd
# install dev version of {export} from GitHub repo (commit 1afc8e2 / 2021-03-09)
install2.r --error --skipinstalled \
officer \
rvg \
openxlsx \
flextable \
xtable \
rgl \
stargazer \
devEMF
installGithub.r tomwenseleers/export@1afc8e2
# cleaning
rm /tmp/downloaded_packages/*
#!/bin/bash
set -x
# Python3 のインストール
source /rocker_scripts/install_python.sh
install2.r --skipinstalled reticulate
# aptキャッシュを消去
apt-get clean
rm -rf /var/lib/apt/lists/*
# グローバルに pandas と matplotlib/seaborn を入れておく
python3 -m pip --no-cache-dir install pandas seaborn
# radian: A 21 century R console. のインストール
python3 -m pip --no-cache-dir install radian jedi
#!/bin/bash
set -x
# まず、rocker/tidyverse 相当のパッケージを導入
# 容量の大きな database backend は省略
# Ref: https://github.com/eitsupi/r-ver/blob/main/scripts/install_editorsupports.sh
# https://github.com/rocker-org/rocker-versioned2/blob/master/scripts/install_tidyverse.sh
# 依存ライブラリの追加
apt-get update
apt-get install -y --no-install-recommends \
cmake \
file \
gdebi-core \
git \
lsb-release \
procps \
psmisc \
python-setuptools \
sudo \
unixodbc-dev \
wget \
libapparmor1 \
libcairo2-dev \
libclang-dev \
libcurl4-openssl-dev \
libedit2 \
libgc1c2 \
libgit2-dev \
libobjc4 \
libsasl2-dev \
libssh2-1-dev \
libssl-dev \
libxml2-dev \
libxtst6 \
libsqlite3-dev
# Pandoc は Ubuntu 20.04 LTS のものは ver.2.5 と古いので、新しいものをインストールする
if [ `uname -m` = "aarch64" ]; then
PANDOC_DLFILE="pandoc-2.17.1.1-1-arm64.deb"
else
PANDOC_DLFILE="pandoc-2.17.1.1-1-amd64.deb"
fi
wget -q https://github.com/jgm/pandoc/releases/download/2.17.1.1/${PANDOC_DLFILE}
gdebi -n $PANDOC_DLFILE
rm $PANDOC_DLFILE
apt-get clean
rm -rf /var/lib/apt/lists/*
# R packages
install2.r --error --skipinstalled \
tidyverse \
devtools \
rmarkdown \
BiocManager \
vroom \
gert
#!/bin/bash
# Code-server のセットアップ
# 拡張機能の導入
code-server --install-extension Ikuyadeu.r
code-server --install-extension RDebugger.r-debugger
# 設定ファイル
cat << EOF > ~/.config/code-server/config.yaml
bind-addr: 0.0.0.0:8080
auth: none
cert: false
EOF
cat << EOF > ~/.local/share/code-server/User/settings.json
{
"r.rpath.linux": "/usr/local/bin/R",
"r.rterm.linux": "/usr/local/bin/radian",
"r.bracketedPaste": true,
"r.plot.useHttpgd": true,
"workbench.colorTheme": "Monokai",
"files.autoSave": "off",
"editor.fontFamily": "'UDEV Gothic LG'",
"editor.fontLigatures": true,
"editor.fontSize": 16,
"terminal.integrated.fontSize": 16,
"editor.tabSize": 2
}
EOF
cat << EOF > ~/.local/share/code-server/User/keybindings.json
[
{
"key": "ctrl+shift+m",
"command": "type",
"args": {
"text": " %>% "
},
"when": "editorTextFocus && editorLangId == 'r'"
},
{
"key": "ctrl+shift+m",
"command": "type",
"args": {
"text": " %>% "
},
"when": "editorTextFocus && editorLangId == 'rmd'"
},
]
EOF
cat << EOF > ~/.radian_profile
options(radian.auto_match = TRUE)
options(radian.highlight_matching_bracket = TRUE)
options(radian.prompt = "\033[0;32mr$>\033[0m ")
options(radian.escape_key_map = list(
list(key = "-", value = " <- "),
list(key = "m", value = " %>% ")
))
options(radian.force_reticulate_python = TRUE)
EOF
cat << EOF > ~/.Rprofile
# httpgd settings
options(httpgd.host = "0.0.0.0")
options(httpgd.port = 59531)
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment