Dockerized plumber API running but not responding

Hi,

I tried dockerizing a simple plumber API using a programmatic definition. Here is the index.R script I am using:

#!/usr/bin/env Rscript

suppressMessages(library(jsonlite))
suppressMessages(library(plumber))

pr <- plumber$new()

pr$handle("GET", "/", function(req, res) {
  "Hello, this is Plumber!"
})

pr$run(port=8081)

The Dockerfile looks like this:

FROM trestletech/plumber
RUN addgroup --system app && adduser --system --ingroup app app
WORKDIR /home/app
COPY index.R .
RUN chown app:app -R /home/app
USER app
EXPOSE 8081
ENTRYPOINT ["Rscript", "index.R"]

I can build a Docker image by docker build -t <image_name> . and run it as docker run -p 80:8081 <image_name>, then I get Starting server to listen on port 8081 as expected. But when I go to localhost I get no response.

I must be missing something trivial here.

Hi @psolymos!

Your docker command looks to be ok.

Couple questions...

  • Which system are you running on?
  • Does it work when you visit 127.0.0.1:80?
  • Does it work if you map to a port other than 80? Such as 8082... docker run -p 8082:8081 <image_name> and visiting localhost:8082

Thanks @barret ! I tried different ports on both Mac and Windows, still no luck.

Ah! From this example: https://github.com/rstudio/plumber/blob/20cd7a3fd56b99e8f96618add62bc24a22c03c97/Dockerfile , I noticed that Jeff has used host = '0.0.0.0' in the pr$run command in ./index.R.

I added that and your code works!


Thank you for a small reprex!

Updated index.R

#!/usr/bin/env Rscript

suppressMessages(library(jsonlite))
suppressMessages(library(plumber))

pr <- plumber$new()

pr$handle("GET", "/", function(req, res) {
  "Hello, this is Plumber!"
})

pr$run(host = '0.0.0.0', port=8081)
1 Like

Thanks @barret, it really does work. I checked, and plumber$new()$run lists host = "127.0.0.1" as the default for the host argument and not "0.0.0.0". Hope this helps people out there. See also this SO question about the difference between 127.0.0.1 and 0.0.0.0: ip - What is the difference between 0.0.0.0, 127.0.0.1 and localhost? - Stack Overflow

2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.