Run Shiny applications on OpenShift Online using custom images

497 Views Asked by At

I'm new to OpenShift and currently exploring its functionalities using the OpenShift online. I created a simple R Shiny application and created the following Dockerfile to build a custom image in OpenShift.

# Base image https://hub.docker.com/u/rocker/
FROM rocker/shiny:latest

# expose port
EXPOSE 3838

# system libraries of general use
## install debian packages
RUN apt-get update -qq && apt-get -y --no-install-recommends install \
    libxml2-dev \
    libcairo2-dev \
    libsqlite3-dev \
    libmariadbd-dev \
    libpq-dev \
    libssh2-1-dev \
    unixodbc-dev \
    libcurl4-openssl-dev \
    libssl-dev

## update system libraries
RUN apt-get update && \
    apt-get upgrade -y && \
    apt-get clean

# copy necessary files
## app folder
RUN mkdir -p /srv/shiny-server/soker
COPY docker.Rproj /srv/shiny-server/soker
COPY server.R /srv/shiny-server/soker
COPY ui.R /srv/shiny-server/soker
COPY renv.lock /srv/shiny-server/soker
COPY server.R /srv/shiny-server/soker
COPY renv  /srv/shiny-server/soker/renv

# install renv & restore packages
RUN Rscript -e 'install.packages("renv")'
RUN Rscript -e 'renv::consent(provided = TRUE)'
RUN Rscript -e 'renv::restore()'

RUN chown -R shiny /srv/shiny-server/
RUN chown -R shiny /var/lib/shiny-server/

# Run as a non-root user
USER 997

# run app on container start
CMD ["R", "-e", "shiny::runApp( '/srv/shiny-server/soker',host = '0.0.0.0', port = 3838)"]

This DockerFile is in the source folder with the following server.R and ui.R files.

server.R

server <- function(input, output) {
  
  output$distPlot <- renderPlot({
    
    x    <- faithful$waiting
    bins <- seq(min(x), max(x), length.out = input$bins + 1)
    
    hist(x, breaks = bins, col = "#75AADB", border = "white",
         xlab = "Waiting time to next eruption (in mins)",
         main = "Histogram of waiting times")
    
  })
  
}

ui.R

library(shiny)
ui <- fluidPage(
  titlePanel("Hello Shiny!"),
  sidebarLayout(
    sidebarPanel(
      sliderInput(inputId = "bins",
                  label = "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30)
    ),
    mainPanel(
      plotOutput(outputId = "distPlot")
    )
  )
)

And using the OpenShift CLI, I ran a dry run using the below command.

oc new-app <repository> \
    --source-secret <secret> --name newapp --strategy=docker --dry-run -o json

Even though I've exposed the port 3838 in my Dockerfile, the output doesn't show the port 3838.

"spec": {
                       "containers": [
                            {
                                "name": "newapp",
                                "image": "newapp:latest",
                                "ports": [
                                    {
                                        "containerPort": 8080,
                                        "protocol": "TCP"
                                    },
                                    {
                                        "containerPort": 8443,
                                        "protocol": "TCP"
                                    }
                                ],
                                "resources": {}
                            }
                        ]
                    }
                }
            },

Am I missing something here? How can I add port 3838 to be the default port for the container? I've run this on local environment using Docker and I could access the shiny app by typing localhost:3838.

1

There are 1 best solutions below

6
On BEST ANSWER

In order to have OpenShift recognize an exposed port, I believe that you will need to express this in a LABEL on the Dockerfile

LABEL io.openshift.expose-services="8080:http"

Source: https://github.com/sclorg/s2i-python-container/blob/master/3.9/Dockerfile.fedora#L33