Docker Community Forums

Share and learn in the Docker community.

Enabing Shiny Server with Docker Swarm: no replication

Hi, I’m having difficulty with creating replicas of a docker image containing Shiny server in Docker swarm mode.

I create a docker service but no replicas are created:

$ docker stack deploy -c docker-compose_v3.yml docker-compose_v3
$ docker service ls

ID                  NAME                            MODE                REPLICAS            IMAGE                                               PORTS
dn2umli2xce8        docker-compose_v3_dataservice   replicated          0/5                 
sustainableos/shiny:proj_r362_ub1804_shiny_v3   *:80->80/tcp, *:8017->3838/tcp

I can however run a single container of the docker image: When I run the docker image (as follows) a single docker container is created and I can see the Shiny app at the specified port in the browser:

docker run --name shiny_proj_v3 -dit -v '/home/shiny/webapps:/home/shiny/webapps' -v '/gis:/gis' -p 8021:3838 sustainableos/shiny:proj_r362_ub1804_shiny_v3

I’m unclear if the issue with Docker Swarm appears to be related to how I specify commands in the dockerfile. Note that I include /bin/bash in the dockerfile so that the docker container remains open. If I don’t include /bin/bash the container immediately closes.

Key elements of Dockerfile:

#Install Shiny server
 RUN wget -c https://download3.rstudio.org/ubuntu-14.04/x86_64/shiny-server-1.5.14.948-amd64.deb && \
gdebi -n shiny-server-1.5.14.948-amd64.deb 

# Config Shiny server
RUN sed -i -e 's+srv/shiny-server+home/shiny/webapps/proj+g' /etc/shiny-server/shiny-server.conf 

# Permissions to allow shiny server to be run by shiny user (or otherwise run by root)
RUN cd /opt/shiny-server && \
    chown -R shiny:shiny-app . && \
    cd /opt/shiny-server/ext/node/bin && \
    chown shiny:shiny-app shiny-server && \
    cd /var/lib/shiny-server && \
   chown -R shiny:shiny-app .

WORKDIR /home/shiny

#USER shiny

EXPOSE 3838

#CMD ["R", "-e", "shiny::runApp('/home/shiny/webapps/proj')"]

COPY start_script.sh start_script.sh
RUN chmod +x start_script.sh
CMD ./start_script.sh

The start_script.sh script:

  #!/bin/bash
  exec shiny-server &
  /bin/bash

The docker-compose_v3.yml file:

version: "3.8"
services:
  dataservice:
    image: sustainableos/shiny:proj_r362_ub1804_shiny_v3
    deploy:
      replicas: 5
      restart_policy:
        condition: on-failure
    ports:
      - "8017:3838"
      - "80:80"
    volumes:
      - /home/shiny/webapps/proj:/home/shiny/webapps/proj

Thanks for any suggestions.

$ docker --version
Docker version 19.03.11, build 42e35e61f3
Linux Mint 19.3 (Ubuntu 18.04)

The output of docker stack ps docker-compose_v3 --no-trunc usualy provides insights about the scheduling situation.

$docker stack ps docker-compose_v3 --no-trunc 

indicates that the replicas are starting and then shutting down.

odj137fk53bq         \_ docker-compose_v3_dataservice.5   sustainableos/shiny:proj_r362_ub1804_shiny_v3   desktop_rc          Shutdown            Failed 6 seconds ago               "task: non-zero exit (1)"   
ypx9rj5ytbk0         \_ docker-compose_v3_dataservice.5   sustainableos/shiny:proj_r362_ub1804_shiny_v3   desktop_rc          Shutdown            Failed 15 seconds ago              "task: non-zero exit (1)"   
wz7x6yptczdv         \_ docker-compose_v3_dataservice.5   sustainableos/shiny:proj_r362_ub1804_shiny_v3   desktop_rc          Shutdown            Failed 25 seconds ago              "task: non-zero exit (1)"   
ohoe9bgcd2lg         \_ docker-compose_v3_dataservice.5   sustainableos/shiny:proj_r362_ub1804_shiny_v3   desktop_rc          Shutdown            Failed 37 seconds ago              "task: non-zero exit (1)"  


$ docker service logs docker-compose_v3_dataservice 

mentions a TERM environment variable is not set
docker-compose_v3_dataservice.4.qqujukj7dak5@desktop_rc | TERM environment variable not set.

I also tried adding ‘top’ to the start_script.sh to ensure there is a running process, but the replicas shut down.

Actualy your docker run and docker-compose stack are not entirely comparible. Can you retry your 'docker runcommand with thetiin your-dti` option?

This should be an equivalent of your -dit options:

services:
  dataservice:
    ..
    tty: true
    stdin_open: true

The solution I’ve found is that I need to directly include the shiny-server command in the dockerfile:

WORKDIR /home/shiny
USER shiny
EXPOSE 3838    
CMD ["shiny-server"]

I commented out the section on the start_script.sh

#COPY start_script.sh start_script.sh
#RUN chmod a+x start_script.sh
#CMD ./start_script.sh

This enables shiny-server if running a single docker container and also using docker swarm.

This part should be done on all nodes of your cluster. Preparation step is very simple and anyone who works with Docker is very familiar with this process. All we need is to install latest Docker. This step simply follows official Docker documentation.

First, install required dependencies:

sudo yum install -y yum-utils device-mapper-persistent-data lvm2 Second, add Docker repository: sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Install Docker CE:

$ sudo yum install -y docker-ce

Enable Docker start on boot and start daemon:

sudo systemctl enable docker sudo systemctl start docker
Initialize first cluster manager

NOTE: This part should be done only on one of designated manager nodes. In this example I do this on manager1. Initialize Docker Swarm cluster. Note –advertise-addr and –data-path-addr both set to eth1. This is to make sure that all communication goes through private network only:

$ sudo docker swarm init --advertise-addr=eth1 --data-path-addr=eth1
If cluster was initialized successfully you will see output similar to this:

Swarm initialized: current node (ppp9aa6itx4r3e4u4rsbb6u7g) is now a manager.

To add a worker to this swarm, run the following command:

docker swarm join --token SWMTKN-1-05r99dbfwrvg4ic31783gk9o24sq9hkkdt4ruoaybmpzs3dtor-a9ujgk4iy3f86bs7xecysta2n 10.136.166.159:2377

To add a manager to this swarm, run ‘docker swarm join-token manager’ and follow the instructions.
You can also verify status by listing all current nodes and their status:

$ sudo docker node ls