Search code examples
rdockershinyshiny-server

How to find error logs when my dockerized shiny app does not work


I'm trying to put my shiny app in docker container. My shiny app works totally fine on my local computer. But after dockerize my shiny app, I always have error message on my localhost like The application failed to start. The application exited during initialization..

I have no idea why that happens. I'm new to docker. How can I find the error logs when I run the docker image? I need the log to know what goes wrong.

Here is my dockfile:

# Install R version 3.6
FROM r-base:3.6.0

# Install Ubuntu packages
RUN apt-get update && apt-get install -y \
    sudo \
    gdebi-core \
    pandoc \
    pandoc-citeproc \
    libcurl4-gnutls-dev \
    libcairo2-dev/unstable \
    libxt-dev \
    libssl-dev

# Download and install ShinyServer (latest version)
RUN wget --no-verbose https://s3.amazonaws.com/rstudio-shiny-server-os-build/ubuntu-12.04/x86_64/VERSION -O "version.txt" && \
    VERSION=$(cat version.txt)  && \
    wget --no-verbose "https://s3.amazonaws.com/rstudio-shiny-server-os-build/ubuntu-12.04/x86_64/shiny-server-$VERSION-amd64.deb" -O ss-latest.deb && \
    gdebi -n ss-latest.deb && \
    rm -f version.txt ss-latest.deb

# Install R packages that are required
# TODO: add further package if you need!
RUN R -e "install.packages(c( 'tidyverse', 'ggplot2','shiny','shinydashboard', 'DT', 'plotly', 'RColorBrewer'), repos='http://cran.rstudio.com/')"
# Copy configuration files into the Docker image
COPY shiny-server.conf  /etc/shiny-server/shiny-server.conf
COPY /app /srv/shiny-server/

# Make the ShinyApp available at port 80
EXPOSE 80

# Copy further configuration files into the Docker image
COPY shiny-server.sh /usr/bin/shiny-server.sh

CMD ["/usr/bin/shiny-server.sh"]

I built image and ran like below:

docker build -t myshinyapp .
docker run -p 80:80 myshinyapp

Solution

  • Usually the logs for any (live or dead) container can be found by just using:

    docker logs full-container-name
    

    or

    docker logs CONTAINERID
    

    (replacing the actual ID of your container)

    As first said, this usually works as well even for stopped (not still removed) containers, which you can list with:

    docker container ls -a
    

    or just

    docker ps -a
    

    However, sometimes you won't even have a log, since the container was never created at all (which I think, by experience, fits more to your case)

    And it can be happening simply because the docker engine is unable to allocate all of the resources that your service definition is requiring to have available.

    The application failed to start. The application exited during initialization

    is usually reflect of your docker engine being unable to get the required resources.

    And the most common case for that, is just as simple as your host ports:

    If you have another service (being dockerized or not) using (for example) that port that you want to use for your service (in your case, port 80) then Docker would just be unable to start your container.

    So... in short... the easiest fix for that situation (and your first try whenever you face this kind of issues) is just to bind any other port from your host (say: 8080), to that 80 port that your service will be listening to internally (inside your container):

    docker run -p 8080:80 myshinyapp
    

    The same principle applies to unallocatable volumes (e.g.: trying to bind a volume as read-only that doesn't actually exist in the host)


    As an aside comment/trick:

    Since you're not setting a name for your container, you will need to use the container id instead when looking for its logs.

    But instead of typing (or copy-pasting) the full container id (usually something like: 1283c66babea or even larger) you can just type in a few first digits instead, and it will still work as expected:

    docker logs 1283c6 or docker logs 1283 or even docker logs 128

    (of course... as long as you don't have any other 128***** container)