jimtyhurst / shiny-python-docker

A trivial Shiny for Python app, which is deployed in a Docker image.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

shiny-python-docker

The sole purpose of this project is to demonstrate how to deploy a Shiny app for Python in a Docker image. I could not find a sample Dockerfile in the Shiny documentation, so I created this simple example. When deployed, the Docker container will run a uvicorn server, which serves the web application that was developed in Python.

Contents

Introduction

The "Shiny" part of the app is from a posit tutorial: Deploying a Shiny for Python application with Posit Connect.

This example adds to that tutorial. Rather than deploying to Posit Connect, this example builds a Docker image, which can then be deployed in any environment that runs containers, such as Google Cloud Run or AWS Elastic Container Service (ECS).

Much of the Dockerfile is taken from Peter Solymos' (2022-10-05) blog post, Containerizing Shiny for Python and Shinylive Applications based on shiny alpha version 0.2.7. I started this project with shiny version 0.6.1.1, which was released after the 2023-04-18 announcement of general availability.

Run locally

To run this Shiny app locally in a terminal from the project's root directory without using a Docker image:

shiny run ./app.py

That command starts a Uvicorn server running on port 8000. Direct your browser to http://127.0.0.1:8000 to see the interactive web application.

Build a Docker image

To build a Docker image from the project's root directory:

docker image build --pull --file './Dockerfile' --tag 'helloshinypython:latest' .

Run the Docker image in your dev environment

Run the Docker image interactively and view the web app in a browser directed to http://127.0.0.1:8080

# Maps port 8000 in the container to port 8080 on the host machine.
docker run -it --rm -p 8080:8000 helloshinypython:latest

Deploy the app to Google Cloud Run

From the root directory of the project:

gcloud run deploy

License

Copyright (c) 2024 Jim Tyhurst

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.

About

A trivial Shiny for Python app, which is deployed in a Docker image.

License:GNU General Public License v3.0


Languages

Language:Dockerfile 60.3%Language:Python 39.7%