Notes on preventing garbled characters in RStudio on Docker and sharing filesystems between host and container
Contents
Until now, I used the desktop version of RStudio for data analysis, but I recently moved to RStudio Server in a Docker environment. Because I use both Mac and Windows machines at home and at work, building an identical environment on Docker (especially for character encoding) has made data analysis very comfortable.
Docker Hub offers rocker/geospatial((rocker/geospatial. Accessed February 21, 2021. https://hub.docker.com/r/rocker/geospatial)) as an RStudio Server environment capable of GIS analysis. However, when I simply copied the steps to run `docker pull` and `docker run rocker/geospatial`, I ran into the following two problems.
- Plots generated with tools like ggplot2 do not support Japanese (the characters become garbled).
- If the filesystem is not shared between the Host and Container, I cannot access the files I usually use in the desktop version. (If you do not configure this when executing `docker run`, you cannot change the working directory with `setwd` after starting it up.)
I am leaving these notes on how I resolved these two points. Regarding Docker, I gained a detailed and clear understanding from the Docker course by Kame-Leon, a data scientist in the US((Docker Course Taught from Scratch by a US AI Developer. Udemy. Accessed February 21, 2021. https://www.udemy.com/course/aidocker/)). He also has free blog posts((Docker Super-Introduction #1: What is Docker? [For Beginners]. Accessed February 21, 2021. https://datawokagaku.com/whatisdocker/)), which I recommend checking out. The following content assumes familiarity with the material in that course.
Creating a Dockerfile
First, create a Dockerfile((Docker Super-Introduction #5: Using a Dockerfile [For Beginners]. Accessed February 21, 2021. https://datawokagaku.com/howtousedockerfile/)) in a location of your choice on the Host (computer) side. Here, I create a directory named `docker` on Dropbox and place the Dockerfile inside it. (e.g., (Mac) `/Users/user/Dropbox/docker`, (Windows) `D:\Dropbox\R\docker`)
Using Notepad or any text editor, create a file named `Dockerfile` and place it inside the `docker` directory. As a rule, do not put any files other than the Dockerfile in this directory (doing so will affect `docker build`).
I created the Dockerfile using this page((Installing RStudio Server. Accessed February 21, 2021. https://k-metrics.github.io/cabinet/env_install_svr.html)) as a reference. Because an image layer is created for each `RUN` instruction((Docker Super-Introduction #7: List of Frequently Used Dockerfile Commands [For Beginners]. Accessed February 21, 2021. https://datawokagaku.com/dockerfile_commands/)), it is best to chain `RUN` commands together using backslashes `\` whenever possible.
# ベースとするイメージ(任意のrocker/*に変更可能)
FROM rocker/geospatial:latest
# OS環境(rockerはdebianベース)に日本語ロケールを追加し切り替え
ENV LANG ja_JP.UTF-8
ENV LC_ALL ja_JP.UTF-8
RUN sed -i '$d' /etc/locale.gen \
&& echo "ja_JP.UTF-8 UTF-8" >> /etc/locale.gen \
&& locale-gen ja_JP.UTF-8 \
&& /usr/sbin/update-locale LANG=ja_JP.UTF-8 LANGUAGE="ja_JP:ja"
RUN /bin/bash -c "source /etc/default/locale"
RUN ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
# 日本語フォントをインストール
RUN apt-get update && apt-get install -y \
fonts-ipaexfont \
fonts-noto-cjkCode language: PHP (php)
Docker build
(On Mac) Connect via SSH in Terminal. On Windows, use TeraTerm((Impress Corporation. Mado no Mori. Accessed February 21, 2021. https://forest.watch.impress.co.jp/library/software/utf8teraterm/)) or Windows Terminal((Windows Terminal Finally Complete: Summary of Features and How to Use It. @IT. Accessed February 21, 2021. https://www.atmarkit.co.jp/ait/articles/2005/28/news018.html)). For basic Linux commands, this series((dotinstall. [Old Edition] Introduction to UNIX Commands [General User Edition] (All 24 lessons). Dotinstall. Accessed February 21, 2021. https://dotinstall.com/lessons/basic_unix_v2)) provides thorough video explanations. Kame-Leon's Docker course((Docker Course Taught from Scratch by a US AI Developer. Udemy. Accessed February 21, 2021. https://www.udemy.com/course/aidocker/)) also explains basic commands.
# 自分(Mac)の場合 /Users/user から /Users/user/Dropbox/docker へ移動
cd Dropbox
cd dockerCode language: PHP (php)
# /Users/user/Dropbox/docker
docker build -t geo:geospatial .Code language: PHP (php)
After building, execute `docker run`, using the `-v` option to share the filesystem and the `-p` option to connect the Host and Container ports. I also referred to this article.
For Mac
docker run -p 8787:8787 -v C:\Users\user\Dropbox\R\:/home/rstudio -d geo:geospatialCode language: JavaScript (javascript)
Replace `C:\Users\user\Dropbox\R\` with the path to the folder you are actually using.
When using the D drive on Windows
docker run -p 8787:8787 -v D:\Dropbox\R:/home/rstudio -d geo:geospatialCode language: JavaScript (javascript)
It will look something like the above, but if you cannot mount a directory on the D drive properly, check your Local Security Policy settings.
Also, on Windows, it seems better to set the Virtual Hard Disk to the D drive((Wand_ta. How to Deal with Running Out of Space on the C Drive When Using Docker for Windows. Study Diary. Published 1544067149. Accessed February 21, 2021. https://wand-ta.hatenablog.com/entry/2018/12/06/123229)).
Accessing RStudio from a browser
If you enter the command above and get no errors, RStudio has started up properly. Check it at http://localhost:8787. Since we did not set a custom password this time, replace `C:\Users\user\Dropbox\R\` with the path to the folder you are actually using.
Update: Managing with Docker Compose
You can also manage RStudio using Docker Compose. The method in the article above works fine, but writing and remembering a long command every time you execute `docker run` is a chore. You may also want to use various other containers alongside RStudio Server in the future. Docker Compose is useful in such cases. Please see the article below.