In order to use plumber
inside a docker container you will have to:
Make a Dockerfile with all the dependencies
Link inside docker-compose.yml
Make the Dockerfile
In this example you make an image with an r-script
which route is src/myscript.R
FROM rocker/r-apt:bionic
RUN apt-get update && \
apt-get install -y -qq \
libmariadb-client-lgpl-dev \
r-cran-plumber \
r-cran-reshape2 \
r-cran-rmysql \
r-cran-dplyr
COPY src src
RUN chmod 755 src
WORKDIR /src
RUN chmod 777 myscript.R
EXPOSE 8000
CMD ["Rscript","myscript.R"]
In order to build the image with the Dockerfile run
docker image build -t my-plumber-image .
Note that the .
at the end is the current work directory and -t
stands for "tag"
Link the image inside docker-compose
version: '3'
services:
mysql:
image: mysql:5.7
container_name: mysql-container
environment:
- MYSQL_ROOT_PASSWORD=mypassword
ports:
- "3306:3306"
plumber:
image: my-plumber-image
build: my-plumber-image
container_name: plumber-container
ports:
- "8000:8000"
links:
- mysql
After link the plumber to the other services you can have access through the port 8000 like any other POST/GET request. The ip
is defined by the localmachine.
I recommend you to use postman for the request tests. Hope this can help you.
I didn't mention any example with R
and plumber
because in their website it's a really good short one.
DB connection
If you want to make a connection to a container use the following script:
DB_conn <- function(){
return(RMySQL::dbConnect(RMySQL::MySQL(),
user = 'root',
password = 'mypassword',
dbname = 'mydatabase',
host = HOST, # Container name like "myservice_mysql_1" look in the logs
port = PORT) # Default port 3306
)
}