Skip to content

Commit

Permalink
attempt docker builds
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffreyhanson committed Jul 23, 2021
1 parent 2e68a6d commit bd013c8
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 38 deletions.
3 changes: 1 addition & 2 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,4 @@ pkgdown
Dockerfile
docker-compose.yml
.lintr
.heroku.yml
heroku.yml
.restart.txt
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
renv/library
renv/local
renv/staging
Dockerfile
docker-compose.yml
2 changes: 2 additions & 0 deletions .github/workflows/deploy-heroku.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
HEROKU_EMAIL: ${{ secrets.HEROKU_EMAIL }}
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
DOCKERFILE_NAME: "Dockerfile"
DOCKERFILE_TARGET: "heroku"
DOCKER_OPTIONS: "--no-cache"
run: |
cd ${DOCKERFILE_DIRECTORY}
Expand All @@ -29,6 +30,7 @@ jobs:
docker build \
--file ${DOCKERFILE_NAME} \
${DOCKER_OPTIONS} \
--target ${DOCKERFILE_TARGET} \
--tag registry.heroku.com/${HEROKU_APP_NAME}/web .
heroku container:push web --app ${HEROKU_APP_NAME}
heroku container:release web --app ${HEROKU_APP_NAME}
Expand Down
76 changes: 58 additions & 18 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# source image
FROM rocker/shiny:4.1.0
FROM rocker/shiny:4.1.0 AS base

# remove example apps
## remove example apps
RUN rm -rf /srv/shiny-server/*

# install system libraries
## install system libraries
RUN apt-get update && apt-get install -y \
software-properties-common

# install R package dependencies
## install R package dependencies
RUN add-apt-repository ppa:ubuntugis/ubuntugis-unstable && \
apt-get update && apt-get install -y \
libcurl4-gnutls-dev \
Expand All @@ -21,29 +21,69 @@ RUN add-apt-repository ppa:ubuntugis/ubuntugis-unstable && \
coinor-libcbc-dev \
coinor-libclp-dev \
coinor-libsymphony-dev \
coinor-libcgl-dev \
libharfbuzz-dev \
libfribidi-dev
libfribidi-dev \
libfontconfig1-dev \
&& rm -rf /var/lib/apt/lists/*

# install R packages needed to setup renv
RUN Rscript -e 'install.packages(c("renv", "remotes"))'
## install R packages
RUN mkdir /renv
COPY renv.lock /renv/renv.lock
RUN cd /renv && \
Rscript -e 'install.packages(c("renv", "remotes"))' && \
Rscript -e 'renv::restore()'

# copy the app
COPY --chown=shiny:shiny . /srv/shiny-server
RUN chmod -R 755 /srv/shiny-server/
## install app
RUN mkdir /app
COPY inst /app/inst
COPY man /app/man
COPY R /app/R
COPY .Rbuildignore /app
COPY DESCRIPTION /app
COPY NAMESPACE /app

# set port for shiny server if PORT variable specified
RUN if [ -z "$PORT" ]; then PORT=3838; fi
RUN sed -i -e 's@listen 3838@listen '"$PORT"'@g' /etc/shiny-server/shiny-server.conf
RUN cd /app && \
Rscript -e 'remotes::install_local(upgrade = "never")' && \
rm -rf /app

# set user
## set working directory
WORKDIR /tmp
RUN touch restart.txt && \
chmod 777 restart.txt

# update shiny server configuration
RUN cp /etc/shiny-server/shiny-server.conf /tmp/shiny-server.conf && \
sed -i 's/run_as shiny;/run_as shiny;preserve_logs true;/g' /tmp/shiny-server.conf && \
cp /tmp/shiny-server.conf /etc/shiny-server/shiny-server.conf && \
cat /etc/shiny-server/shiny-server.conf

# set command
CMD ["/bin/bash"]

# Heroku image
FROM base AS heroku

## set user
USER shiny

# install R packages using renv
RUN cd /srv/shiny-server && Rscript -e 'renv::restore()'
RUN cd /srv/shiny-server && Rscript -e 'remotes::install_local(upgrade = "never")'
# expose port (for local deployment only)
EXPOSE $PORT

## run app
CMD R -e 'options(shiny.port=as.numeric(Sys.getenv("PORT")),shiny.host="0.0.0.0");wheretowork::run_app()'

# select port
# main image
FROM base AS main

## set user
USER shiny

## select port
EXPOSE 3838

# copy app file for shiny server
COPY --chown=shiny:shiny app.R /srv/shiny-server

# run app
CMD ["/usr/bin/shiny-server"]
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,16 @@ quicksite:
debug:
R -e "options(golem.app.prod = FALSE); golem::run_dev()"

quick-debug:
R -e "options(golem.app.prod = FALSE, quick = TRUE); golem::run_dev()"

## launch local version inside Docker container
demo-start:
docker-compose up
docker-compose up web
google-chrome "http://localhost:3838"

demo-skill:
docker-compose down
docker-compose down web

## launch released version inside Docker container
launch-start:
Expand Down
12 changes: 6 additions & 6 deletions R/app_global.R
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
app_global <- quote({
# initialize asynchronous processing
if (identical(get_golem_config("strategy"), "auto")) {
if (identical(wheretowork::get_golem_config("strategy"), "auto")) {
if (identical(Sys.getenv("R_CONFIG_ACTIVE"), "shinyapps")) {
future::plan("sequential")
} else {
future::plan("multisession")
}
} else {
future::plan(get_golem_config("strategy"))
future::plan(wheretowork::get_golem_config("strategy"))
}

# set seed for reproducibility
set.seed(200)

# define parameters for solution settings
area_budget_parameter <-
new_parameter(
wheretowork::new_parameter(
name = "Total area budget",
status = FALSE,
value = 0,
Expand All @@ -28,7 +28,7 @@ app_global <- quote({
)

boundary_gap_parameter <-
new_parameter(
wheretowork::new_parameter(
name = "Spatial clustering",
status = FALSE,
value = 0,
Expand Down Expand Up @@ -74,9 +74,9 @@ app_global <- quote({
)

# find built-in projects
project_dir <- get_golem_config("projects")
project_dir <- wheretowork::get_golem_config("projects")
if (identical(project_dir, "default")) {
project_dir <- system.file("extdata", "projects", package = "wheretowork")
}
project_data <- find_projects(project_dir)
project_data <- wheretowork::find_projects(project_dir)
})
8 changes: 6 additions & 2 deletions app.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
# To deploy, run: rsconnect::deployApp()
# Or use the blue button on top of this file

# load package
pkgload::load_all(export_all = FALSE, helpers = FALSE, attach_testthat = FALSE)
# load package if possible
if (file.exists("DESCRIPTION")) {
pkgload::load_all(
export_all = FALSE, helpers = FALSE, attach_testthat = FALSE
)
}

# set application production mode
if (is.null(getOption("golem.app.prod"))) {
Expand Down
4 changes: 3 additions & 1 deletion dev/run_dev.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ golem::detach_all_attached()
golem::document_and_reload()

# Install package for asynchronous processing
renv::install(".", upgrade = "never", force = TRUE)
if (!identical(getOption("quick"), TRUE)) {
renv::install(".", upgrade = "never", force = TRUE)
}

# Run the application
run_app(options = list(launch.browser = TRUE))
16 changes: 14 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
version: "3.9"
services:
web:
build: .
main:
build:
context: .
dockerfile: Dockerfile
target: main
ports:
- "3838:3838"
heroku:
build:
context: .
dockerfile: Dockerfile
target: heroku
ports:
- "3837:3837"
environment:
- PORT=3837
5 changes: 0 additions & 5 deletions heroku.yml

This file was deleted.

0 comments on commit bd013c8

Please sign in to comment.