Step 3 : Test and run locally your containerized app

Note

We consider that you have previously configured a Dockerfile file according to instructions described in Step 2:Containerize your app

You need to install Docker on your computer. Please follow the official instructions for your operating system.

Step 3.1 : Adapt the docker-compose file

3.1.1. For R application

Change/modify line 8 with the correct folder name of your application defined previously at Step 2.1

  • R
 1version: '3.4'
 2services:
 3  shiny-k8s-toolkit:
 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'

3.1.2. For Python application

Adapt the file name of you app at line 14. Here, we assume that your app is in the file named app.py

Change/modify line 18 with the correct folder name of your application defined previously at Step 2.1.

  • Python
 1version: '3.4'
 2services:
 3  shiny-k8s-toolkit:
 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'

Step 3.2 : Run your container

Note

Don’t forget to add writing permissions on logs folder.
The logs folder permits to easily access log files produced by your shiny app. This folder have to be writable by the user running shiny in the container, so do this :
chmod a+w logs/
More details here

Now that you have the appropriate folder structure and configuration files, you are ready to run locally and test 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, which permits to launch the shiny-k8s-toolkit service 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 and test your shiny app.
Enjoy :)

Note

Press Ctrl + C to exit

Additionnal informations :

Administrate your container

By default, the name of the image build by docker-compose.yaml 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:

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

How it works

Docker Compose (lately named 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-k8s-toolkit service taking into account volume and port options.

  • R
  • Python
 1version: '3.4'
 2services:
 3  shiny-k8s-toolkit:
 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-k8s-toolkit:
 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, if so don’t forget to put your name at line 8.

Note

Your are free to change/modify folder name, if so don’t forget to put your name at line 18.
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.yaml 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 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 applied in the container.

Note

With the volumes, you “bind-mount” a local directory into a non-empty directory in the container, the container directory’s existing contents are obscured by the bind mount. This allows you to work and test your application without needing to rebuild a new image after each code edition.

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 the shiny-k8s-toolkit 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 restart 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.