Avoid garbled characters and share file system with Docker environment Rstudio (also Docoker-compose)

Garbled characters and file system sharing issues

Docker Hub has rocker / geospatial as an Rstudio Server environment that can also analyze GIS. However, even if I did docker pull and docker run rocker / geospatial by imitating it, there were the following two problems.

  1. The figure plotted with ggplot2 etc. does not support Japanese (characters are garbled)
  2. If you do not share the file system between Host / Container, you will not be able to access the files that you always used in the desktop version. (If you do not set it at the time of Docker run, you can not try setwd after starting it.)

I will leave a memorandum of understanding that addresses these two points.

Creating a Dockerfile

First, create a Dockerfile anywhere on the Host (computer) side. This time, create a directory named docker on Dropbox and create a Dockerfile. (Example: (Mac) C: \ Users \ user \ Dropbox \ docker, (Windows) D: \ Dropbox \ R \ docker)

You can use Notepad or an editor, so create a file named Dockerfile and place it in the docker directory. As a general rule, do not put files other than Dockerfile in the directory (it will affect Docker build). Since an image is created for each RUN, it is desirable to connect RUNs with a backslash \ as much as possible. [^ 1] [^ 2]

#Base image (any rocker/*Can be changed to)
FROM rocker/geospatial:latest

#Add and switch Japanese locale to OS environment (rocker is based on 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

#Install Japanese font
RUN apt-get update && apt-get install -y \
  fonts-ipaexfont \
  fonts-noto-cjk

Docker build Launch Terminal (on Mac). For Windows, use TeraTerm or Windows Terminal. Go to the location where the Dockerfile is located.

#For myself (Mac)/Users/From user/Users/user/Dropbox/Go to docker

cd Dropbox
cd docker

After moving to the location where the Dockerfile is located, usedocker build -t <name> <directory>to build. (This time, <name> is geo: geospatial)

# /Users/user/Dropbox/docker

docker build -t geo:geospatial .

After build, do Docker run, but use the -v option to share the file system and the -p option to connect the Host and Container ports. [^ 3]

For Mac

docker run -p 8787:8787 -v C:\Users\user\Dropbox\R\:/home/rstudio -d geo:geospatial

For C: \ Users \ user \ Dropbox \ R \, enter any folder path you are using.

If you are using drive D on Windows

docker run -p 8787:8787 -v D:\Dropbox\R:/home/rstudio -d geo:geospatial

However, if the directory on the D drive does not mount properly, check the local security policy settings. [^ 4]

Also, for Windows, it seems better to set the Virtual Hard Disk to the D drive. [^ 5]

Access Rstudio from your browser

If you enter the above command and there are no errors, RStudio has started successfully. Check it out at http: // localhost: 8787. Since I did not set the password to any one this time, I think that both Username and Password can be logged in with rstudio.

(Bonus) Utilization of Docker-compose

As mentioned above, you can also manage Rstudio using Docker-compose. It's okay to keep the above article, but it's hard to write and remember long code every time you do Docker run. Also, in the future, you may want to use various containers other than RStudio Server. In such cases, Docker-compose is useful.

Create docker-compose.yml

Create a file called docker-compose.yml in the same location as Dockerfile and edit it with an editor. Work on Dropbox> docker as above. (Example: (Mac) C: \ Users \ user \ Dropbox \ docker, (Windows) D: \ Dropbox \ R \ docker)

In the above, on docker build and docker run

docker build -t geo:geospatial .
docker run -p 8787:8787 -v C:\Users\user\Dropbox\R\:/home/rstudio -d geo:geospatial

I wrote, but if you set this on docker-compose.yml, it will be as follows.

version: '3'

services:
  rstudio:
    build: .
    image: geo:geospatial
    container_name: geo
    ports:
      - 8787:8787
    volumes:
      - ../R:/home/rstudio
    tty: true
    stdin_open: true

Don't forget to put a half-width space after each colon (: ) and dash ( -). For example, version: '3' has a half-width space between version: and '3'. build: . etc. also require a half-width space before the comma (.). Follow this as a formatting rule.

Also, this time, volumes is specified by a relative path. If you want to specify a folder called C: \ Users \ user \ Dropbox \ R as your Rstudio home directory, you are now in a folder called C: \ Users \ user \ Dropbox \ docker, so just use .. It goes up the hierarchy (C: \ Users \ user \ Dropbox \) and then specifies the directory / R.

By specifying a relative path, the code is independent of the environment of the machine you are using. You can now manage it with the same docker-compose.yml file on both Mac and Windows.

Build with docker-compose up -d

Same as for Docker. Launch Terminal (on Mac). For Windows, use TeraTerm or Windows Terminal. Navigate to the location where you have the Dockerfile and docker-compose.yml.

#For myself (Mac)/Users/From user/Users/user/Dropbox/Go to docker

cd Dropbox
cd docker

After moving to the location where Dockerfile and docker-compose.yml are located, this time type and run docker-compose up -d instead of docker build and docker run. Only this.

# /Users/user/Dropbox/docker

docker-compose up -d

If you get an error, first check if you have forgotten to insert the half-width space. If there is no problem, you should be able to connect to rstudio safely with http: // localhost: 8787 as in the previous setting.

[^ 1]: Dockerfile is here: https://k-metrics.github.io/cabinet/env_install_svr.html [^ 2]: About the image being created for each RUN: https://datawokagaku.com/dockerfile_commands/

Recommended Posts

Avoid garbled characters and share file system with Docker environment Rstudio (also Docoker-compose)
Building Rails 6 and PostgreSQL environment with Docker
Build and manage RStudio environment with Docker-compose
Prepare a scraping environment with Docker and Java
[First team development ③] Share the development environment created with Docker
Build WordPress environment with Docker (Local) and AWS (Production)
Comfortable Docker environment created with WSL2 CentOS7 and Docker Desktop
Maybe it works! Create an image with Docker and share it!
Easy environment construction of MySQL and Redis with Docker and Alfred
Build a Node-RED environment with Docker to move and understand
Create Rails5 and postgresql environment with Docker and make pgadmin available
Zip compression with Java in Windows environment without garbled characters