Skip to content

Commit

Permalink
Added new (experimental) docker build script for Apple Silicon chips.…
Browse files Browse the repository at this point in the history
… Changed container launch script to have alternate commands for Mac and Git Bash (not tested yet, how it works on WSL bash). Readme updated
  • Loading branch information
ldemaz committed Jan 22, 2024
1 parent 657851f commit c211922
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 15 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ cd c/My\ Documents/projects/geog246346

### 6. Build or pull the `docker` image

Using docker, you can either build or pull the docker image you need by running the following in your terminal:
Using docker, you can either build or pull the docker image you need by running the following in your terminal, assuming you have either a Mac with an Intel chip, or a Windows-based machine. If not, go to the section on Mac with M1/M2 chips.

For Intel-based Mac and Windows:

You can either build a local image or pull a pre-built image:

- build (assuming you are in the project directory you made in step 4):

Expand All @@ -73,6 +77,14 @@ Using docker, you can either build or pull the docker image you need by running
docker pull agroimpacts/geospaar:$LATEST
```

For Macs with an M1 or M2 chip, the option is to try and build an image that starts with rocker/rstudio:latest pre-built for linux/amd64 architectures. This requires installing many packages up front and will take a while (and at the moment it is still untested):

```bash
cd geospaar
LATEST=VERSION # replace VERSION with the latest version number, here 4.3.2
docker build -f dockerfiles/silicon.Dockerfile -t agroimpacts/geospaar:$LATEST .
```

Then run the image using the following script that comes with the `geospaar` repo:

```bash
Expand Down
20 changes: 20 additions & 0 deletions dockerfiles/silicon.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Latest image from rocker: https://rocker-project.org/
FROM --platform=linux/arm64 rocker/rstudio:latest

RUN apt-get clean all && \
apt-get update && \
apt-get upgrade -y

# Install R packages
RUN install-silicon.sh

COPY --chown=rstudio:rstudio rstudio-prefs.json \
/home/rstudio/.config/rstudio

COPY --chown=rstudio:rstudio .Rprofile /home/rstudio/

EXPOSE 8787

WORKDIR /home/rstudio

[/init]
113 changes: 113 additions & 0 deletions install-silicon.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Install script for additional packages needed for Apple silicon chips that
# have to rely on rocker/rstudio image built with ARM64
# Adapted from scripts provided by rocker, e.g.
# https://github.com/rocker-org/rocker-versioned2/blob/master/scripts/\
# install_tidyverse.sh
#!/bin/bash

set -e

## build ARGs
NCPUS=${NCPUS:--1}

# a function to install apt packages only if they are not installed
function apt_install() {
if ! dpkg -s "$@" >/dev/null 2>&1; then
if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
apt-get update
fi
apt-get install -y --no-install-recommends "$@"
fi
}

apt_install \
libxml2-dev \
libcairo2-dev \
libgit2-dev \
default-libmysqlclient-dev \
libpq-dev \
libsasl2-dev \
libsqlite3-dev \
libssh2-1-dev \
libxtst6 \
libcurl4-openssl-dev \
libharfbuzz-dev \
libfribidi-dev \
libfreetype6-dev \
libpng-dev \
libtiff5-dev \
libjpeg-dev \
unixodbc-dev \
gdal-bin \
lbzip2 \
libfftw3-dev \
libgdal-dev \
libgeos-dev \
libgsl0-dev \
libgl1-mesa-dev \
libglu1-mesa-dev \
libhdf4-alt-dev \
libhdf5-dev \
libjq-dev \
libpq-dev \
libproj-dev \
libprotobuf-dev \
libnetcdf-dev \
libsqlite3-dev \
libssl-dev \
libudunits2-dev \
netcdf-bin \
postgis \
protobuf-compiler \
sqlite3 \
tk-dev \
unixodbc-dev

install2.r --error --skipmissing --skipinstalled -n "$NCPUS" \
tidyverse \
devtools \
rmarkdown \
RColorBrewer \
RandomFields \
RNetCDF \
classInt \
deldir \
gstat \
hdf5r \
mapdata \
maptools \
mapview \
ncdf4 \
proj4 \
raster \
rgdal \
rgeos \
rlas \
sf \
sp \
spacetime \
spatstat \
spatialreg \
spdep \
stars \
terra \
tidync \
tmap \
geoR \
geosphere \
rmapshaper \
patchwork \
kableExtra \
prettydoc \
cowplot \
here \
rmdformats


# Clean up
rm -rf /var/lib/apt/lists/*
rm -rf /tmp/downloaded_packages

## Strip binary installed lybraries from RSPM
## https://github.com/rocker-org/rocker-versioned2/issues/340
strip /usr/local/lib/R/site-library/*/libs/*.so
52 changes: 38 additions & 14 deletions run-container.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ usage() {
case "$(uname -s)" in
Darwin*) # macOS
READLINK="greadlink" # On macOS, use 'greadlink' from coreutils package
LAUNCHER="nix"
;;
Linux*) # Linux
READLINK="readlink"
LAUNCHER="nix"
;;
CYGWIN*|MINGW32*|MSYS*|MINGW*) # Windows
READLINK="readlink"
LAUNCHER="gitbash"
;;
*)
>&2 echo "Error: Unsupported operating system."
Expand Down Expand Up @@ -63,14 +66,21 @@ if [[ $# -ne $((OPTIND)) ]]; then
exit 1
fi


# Mount main project directory
d=${@:$OPTIND:1}
full_d=$($READLINK -f "${d}")
if [[ ! -d "${full_d}" ]]; then
>&2 echo "Error: Directory ${full_d} does not exist."
exit 1
fi
echo "${full_d} will be the main project mount directory"

bname=$(basename ${full_d})
if [[ "${bname}" == "geospaar" ]]; then
>&2 echo "The mount directory cannot include geospaar, it should be"
>&2 echo "geospaar's parent directory"
exit 1
fi

# Make package directory
r_package_dir=${full_d}/r_${ver}_packages
Expand All @@ -81,22 +91,36 @@ fi

rstudio_image=agroimpacts/geospaar:${ver}
check_image=$(docker image inspect ${rstudio_image})
container_name=geospaar_rstudio

# Running rstudio on localhost:8787
prefs=rstudio-prefs.json
docker run --rm -d -p 8787:${port} -e PASSWORD=password \
--name "${container_name}" \
-v "${full_d}":/home/rstudio/ \
-v "${r_package_dir}":/packages \
-v "${full_d}"/geospaar/"${prefs}":/home/rstudio/.config/rstudio/"${prefs}" \
-v "${full_d}"/geospaar/.Rprofile:/home/rstudio/.Rprofile:rw \
"${rstudio_image}"

echo "${container_name} listening on port ${port}"

# # Running rstudio on localhost:8787
cd ${full_d}
if [[ "${LAUNCHER}" == "nix" ]]; then
echo Launching from a $LAUNCHER platform
docker run --rm -d -p 8787:${port} -e PASSWORD=password \
--name geospaar_rstudio \
-v "${full_d}":/home/rstudio/ \
-v "${r_package_dir}":/packages \
-v "${full_d}"/geospaar/"${prefs}":/home/rstudio/.config/rstudio/"${prefs}" \
-v "${full_d}"/geospaar/.Rprofile:/home/rstudio/.Rprofile:rw \
"${rstudio_image}"
fi

if [[ "${LAUNCHER}" == "gitbash" ]]; then
echo Launching from a $LAUNCHER platform
winpty docker run --rm -d -p 8787:8787 -e PASSWORD=password \
--name geospaar_rstudio \
-v /$PWD:/home/rstudio/ \
-v /$PWD/r_$VER_packages:/packages \
-v /$PWD/geospaar/$prefs:/home/rstudio/.config/rstudio/$prefs \
-v /$PWD/geospaar/.Rprofile:/home/rstudio/.Rprofile:rw \
$rstudio_image
fi

echo "geospaar_rstudio listening on port ${port}"
echo "Copy and paste http://localhost:${port} into your browser"
echo "Username is rstudio and password is password"
echo "To stop container run: docker stop ${container_name}"
echo "To stop container run: docker stop geospaar_rstudio"
# open http://localhost:${port}


Expand Down

0 comments on commit c211922

Please sign in to comment.