Skip to content

Commit

Permalink
add traefik loadbalancer to docker stack
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffreyhanson authored Aug 3, 2021
1 parent 1cd7ffc commit 2be809e
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 35 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ docker-compose.yml
.env.prod
.lintr
restart.txt
inst/log
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
.git
*.log
**/*.log
renv/library
renv/local
renv/staging
Dockerfile
docker-compose.yml
inst/log
4 changes: 3 additions & 1 deletion .env.dev
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
PROJECT_DIRECTORY=./inst/extdata/projects
MEMORY_LIMIT=2G
LOG_DIRECTORY=./inst/log
LOG_LEVEL=ERROR
MEMORY_LIMIT=4G
REPLICAS=2
2 changes: 2 additions & 0 deletions .env.prod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
PROJECT_DIRECTORY=/opt/wheretowork/projects
LOG_DIRECTORY=/opt/wheretowork/log
LOG_LEVEL=ERROR
MEMORY_LIMIT=10G
REPLICAS=3
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ check.log
spell.log
gp.log
examples.log
restart.txt
inst/log/*

# system files
.directory
Expand Down
27 changes: 13 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,9 @@ COPY R /app/R
COPY .Rbuildignore /app
COPY DESCRIPTION /app
COPY NAMESPACE /app

RUN cd /app && \
Rscript -e 'remotes::install_local(upgrade = "never")'

## set working directory
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"]

Expand All @@ -80,14 +68,25 @@ CMD Rscript -e "rsconnect::setAccountInfo(name=Sys.getenv('SHINYAPPS_USER'), tok
# main image
FROM base AS main

RUN rm -f /usr/bin/shiny-server.sh
## allow shiny server to spawn processes for different users
RUN cd /srv/shiny-server && \
touch restart.txt && \
chmod 666 restart.txt

## set user
USER shiny

## select port
EXPOSE 3838

## copy shiny server config file
COPY inst/shiny-server.conf /etc/shiny-server/shiny-server.conf

## prepare log directory
RUN mkdir -p /var/log/shiny-server && \
chown shiny.shiny /var/log/shiny-server && \
chmod -R 666 /var/log/shiny-server

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

Expand All @@ -99,4 +98,4 @@ RUN env | grep R_CONFIG_ACTIVE > /home/shiny/.Renviron
WORKDIR /home/shiny

## run app
CMD ["shiny-server"]
CMD ["/usr/bin/shiny-server"]
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ quick-debug:

## launch local version inside Docker container
demo:
docker-compose --env-file ./.env.dev up --build main
docker-compose --env-file ./.env.dev up --build

demo-kill:
docker-compose --env-file ./.env.dev down
Expand Down
3 changes: 2 additions & 1 deletion R/app_server.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ app_server <- function(input, output, session) {

# initialization
## spawn new process so that users in different process
try(system("touch restart.txt"), silent = TRUE)
# try(system("touch restart.txt"), silent = TRUE)

## initialize app
eval(server_initialize_app)

Expand Down
51 changes: 35 additions & 16 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,42 +1,61 @@
version: "3.9"
services:

visualizer:
image: dockersamples/visualizer:stable
ports:
- "8080:8080"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
deploy:
placement:
constraints: [node.role == manager]

watchtower:
updater:
image: v2tec/watchtower
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /root/.docker/config.json:/config.json
command: --schedule "0 0 * * *" --cleanup

main:
balancer:
image: traefik:v1.7
command: --docker \
--docker.watch \
--docker.domain=localhost \
--web \
--loglevel=${LOG_LEVEL}
ports:
- "80:80"
- "443:443"
- "8080:8080"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
deploy:
mode: replicated
replicas: 1
resources:
limits:
memory: 1G
placement:
constraints: [node.role == manager]
networks:
- net

shiny:
build:
context: .
dockerfile: Dockerfile
target: main
image: naturecons/wheretowork:latest
ports:
- "3939:3838"
volumes:
- "${PROJECT_DIRECTORY}:/projects"
- "${LOG_DIRECTORY}:/var/log/shiny-server/"
deploy:
mode: replicated
replicas: "${REPLICAS}"
restart_policy:
condition: on-failure
resources:
limits:
memory: "${MEMORY_LIMIT}"
networks:
- app
- net
labels:
- "traefik.docker.network=ssp_net"
- "traefik.port=3838"
- "traefik.frontend.rule=PathPrefix:/;"
- "traefik.backend.loadbalancer.sticky=true"

networks:
app:
net:
Empty file added inst/log/.gitkeep
Empty file.
30 changes: 30 additions & 0 deletions inst/shiny-server.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# define the user we should use when spawning R Shiny processes
run_as shiny;

# disable websocket
disable_protocols websocket;

# disable idle timeout to retain connection
app_idle_timeout 0;

# define a top-level server which will listen on a port
server {
# instruct this server to listen on port 3838
listen 3838;

# define the location available at the base URL
location / {

# run this location in 'site_dir' mode, which hosts the entire directory
# tree at '/srv/shiny-server'
site_dir /srv/shiny-server;

# define where we should put the log files for this location
log_dir /var/log/shiny-server;

# when a user visits the base URL rather than a particular application,
# an index of the applications available in this directory will be shown.
directory_index on;

}
}
1 change: 0 additions & 1 deletion restart.txt

This file was deleted.

0 comments on commit 2be809e

Please sign in to comment.