Skip to content
This repository has been archived by the owner on Sep 2, 2019. It is now read-only.

technologiestiftung/opencpu-docker-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

opencpu with docker

Prerequisites

Basic instructions about opencpu can be found here: www.opencpu.org/download.html

How Tos

Reach RStudio & opencpu

Now simply open http://localhost/ocpu/ and http://localhost/rstudio/ in your browser! Login via rstudio with user: opencpu (passwd: opencpu) to build or install apps.

!Hint: Since PORT 80 is used we need to use the 8004 PORT:

Add Files to the Container

The folder ./workspace is mapped into the working directory of the container at /home/opencpu all files in workspace will directly we available in the container and in RStudio. If you do changes in RStudio to these files they will be saved to your local drive.

Run the Container

!Hint: Don't copy the $ in the commands. It is for marking the input.

Start it:

$ cd path/to/repository
# if you didn't do changes to opencpu/Dockerfile.dev
# You can ommit the --build flag
$ docker-compose up --build
> Starting opencpu-docker_opencpu_1 ... done
> Attaching to opencpu-docker_opencpu_1
> opencpu_1  |  * Starting periodic command scheduler cron
> opencpu_1  |    ...done.
>

Open your browser and start hacking

Stop it:

End the terminal session by hitting CTRL + C and stop the containers (all changes other the done to the files in workspace:/home/opencpu will be lost)

$ docker-compose down
> Stopping opencpu-docker_opencpu_1 ... done
> Removing opencpu-docker_opencpu_1 ... done
> Removing network opencpu-docker_default

Start a Session in the Container

Start a bash session within the container run. Be aware that all changes will be lost when the container gets stopped:

$ docker ps
> CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                                                 NAMES
> 50ad1d0af4c6        opencpu-docker_opencpu   "/bin/sh -c 'service…"   22 minutes ago      Up 22 minutes       0.0.0.0:80->80/tcp, 0.0.0.0:8004->8004/tcp, 443/tcp   opencpu-docker_opencpu_1

Use the Container ID or the NAMES property to run the bash session.

$ docker exec -it <CONTANER ID | NAME> bash
> sends you into a session in the container
>

Add R Packages or other Dependencies

To add additional R packages to the container image you have to edit the file opencpu/Dockerfile.dev.
Add a line containing your desired package like this one. Please add the ref parameter to the install (in this case the @v0.1.0-beta):

RUN R -e "devtools::install_github(\"technologiestiftung/fhpredict@v0.1.0-beta\")"

Then run again a docker-compose up --build from the root of the repo.

The image is based on Ubuntu 18.

Distributor ID: Ubuntu
Description:    Ubuntu 18.04.1 LTS
Release:        18.04
Codename:       bionic

To install other dependencies RUN the usual apt-get commands in the docker file.

!Hint: The commands don't allow an interactive prompt. Add the -y flag to accept all Y/n questions.

For example to install curl you could RUN

RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*

Test the opencpu API

The opencpu API docs can be found here.

The code below assumes the fhpredict package is installed and the opencpu container is running.

Do Requests

POST - /ocpu/library/fhpredict/R/simple

Description: The function simple(str = '{"foo":"bah"}') of the package fhpredict takes a JSON string as argument. (Needs to be properly escaped.)

Requests using CURL

curl -X POST "http://localhost:8004/ocpu/library/fhpredict/R/simple" \
    -H "Content-Type: application/json" \
    --data-raw "$body"

Header Parameters: Content-Type should respect the following schema:

{
  "type": "string",
  "enum": [
    "application/json"
  ],
  "default": "application/json"
}

Body Parameters: body should respect the following schema:

{
  "type": "string",
  "default": "{\n  \"str\": \"{\\\"foo\\\":12}\"\n}"
}

GET - /ocpu/tmp//R/.val

Description: The POST request returns something like the example below. These are new endpoints that are generated during runtime and will be discarded on restart. The result of the calculation can be found under the .val endpoint. See also https://www.opencpu.org/api.html#api-session

/ocpu/tmp/x0aa062559b9cf1/R/.val
/ocpu/tmp/x0aa062559b9cf1/R/simple
/ocpu/tmp/x0aa062559b9cf1/stdout
/ocpu/tmp/x0aa062559b9cf1/source
/ocpu/tmp/x0aa062559b9cf1/console
/ocpu/tmp/x0aa062559b9cf1/info
/ocpu/tmp/x0aa062559b9cf1/files/DESCRIPTION

Requests using CURL

curl -X GET "http://localhost:8004/ocpu/tmp/x0aa062559b9cf1/R/.val"

GET - /ocpu/info

Description: From the api docs.

The /ocpu/info endpoint shows the output of sessionInfo() from the main (web server) process. This can be helpful for debugging. The /ocpu/test URL gives you a handy testing web page to perform server requests.

See also https://www.opencpu.org/api.html#api-libraries

Requests using CURL

curl -X GET "http://localhost:8004/ocpu/info"

GET - /ocpu/library/fhpredict/info

Description: Show information about the package fhpredict package. See also https://www.opencpu.org/api.html#api-libraries

Requests using CURL

curl -X GET "http://localhost:8004/ocpu/library/fhpredict/info"

About

Docker setup for running fhpredict in an opencpu container

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages