How it works

Note

We consider that you have configure previously a Dockerfile file according to instructions described in Adapting the Dockerfile to your needs section

Note

You need to install on your machine Docker and docker-compose

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

In this use case, Compose is used with only one container but permits to launch shiny-server in order to help you to develop your shiny app under construction.

Below, the content of docker-compose.yaml file. This file contains dedicated options to run the docker container with shiny-server service taking into account volume and port options.

  • R
  • Python
 1version: '3.4'
 2services:
 3  shiny-server:
 4    build:
 5      context: .
 6    user: 'shiny:shiny'
 7    volumes:
 8      - ./my_project:/srv/shiny-server/
 9      - ./logs:/var/log/shiny-server
10    ports:
11      - '3838:3838'
 1version: '3.4'
 2services:
 3  shiny-server:
 4    build:
 5      context: .
 6    command: [
 7        'shiny',
 8        'run',
 9        '--host',
10        '0.0.0.0',
11        '--port',
12        '3838',
13        '--reload',
14        './app.py'
15      ]
16    user: 'shiny:shiny'
17    volumes:
18      - ./my_project:/srv/shiny-server/
19      - ./logs:/var/log/shiny-server
20    ports:
21      - '3838:3838'
  • R
  • Python

Note

Your are free to change/modify folder name. Don’t forget to put your name at line 8.

Note

Your are free to change/modify folder name. Don’t forget to put your name at line 8.
Don’t forget also to addapt the file name of you app at line 14. Here, we assume that your app is in the file named app.py

We described only some options used in the docker-compose file. If you want to learn more, please see official documentation

Volumes

Volume option permits to connect local folder to folder inside the containers. The command is compose of two fields separated by colon characters ( : ) :

  • local path of folder you want to mount

  • path of the folder in the container you want to connect.

Syntax : /host/path:/container/path

Here, the command line 8 permits to mount the local folder named ./my_project with the folder in the container where the shiny app is stored, /srv/shiny-server/. Thanks to this mount, you can modify files in your app directly from you local computer and modifications will applyed in the container.

Warning

In the previous command, you “bind-mount” a local directory into a non-empty directory on the container, the container directory’s existing contents are obscured by the bind mount. This can be beneficial, such as when you want to test a new version of your application without building a new image.

Another folder is also mounted with the line 9. The logs folder permits to retrieve easily the log file linked to your app. This folder have to be writable by the user shiny, so simply allows everyone to write in it with:

chmod a+w logs/

Note

Logs and error logs are also visible on STDOUT.

Port

Port option permits to specify the HOST PORT and the CONTAINER PORT used for teh shiny-server service. Here, we use the same port 3838 in the both case.

Command

Only for Python, the file contains also command instruction (lines 6 at 15) in order to reload the shiny-server after modification in your script.
The ‘–reload’ option is native by default for R language in the shinyApp function and in shiny-server configuration.

Run your container

Note

Don’t forget to add writing permissions on logs folder.
For example :
chmod a+w logs/

Now, you have all folder structure and configuration files. You are ready to run locally your shiny app. One command line is enough.

Open a terminal and go to the folder where Dockerfile and docker-compose.yaml are stored.
Copy/paste and run the command line below:
docker-compose up --build
This command line build the docker image in a container, run the container with several parameters like port and volume, who permits to launch teh shiny-server and test your shiny app.
You can modify locally your script in your favorite IDE and see immediately ( after refresh your navigator), the changes in your app.
In order to access to your app, you can launch 0.0.0.0:3838 in your navigator.
Now, everything is ready for develop your shiny app.
Enjoy :)

Note

Press Ctrl + C to exit

Administrate your container

By default, the name of the image build by docker-compose command depends of the project and the service name (Services are built once and then tagged, by default as project_service .)

See details docker compose build

Some usefull command line :

  • Start the containers in detached mode :

docker-compose up --build -d
  • View the status of the containers :

docker container ls

Or

docker ps
  • Stop the container use :

docker-compose stop
  • Stop and remove containers :

docker-compose down
  • Remove old builds :

Built containers can stack up on your local computers and eat disk space. It’s recommended to purge old built image from time to time with the following command line:

docker system prune -a