Skip to content

Commit 0bc3b06

Browse files
committed
Include dockerfile templating
1 parent 26bab82 commit 0bc3b06

File tree

5 files changed

+182
-0
lines changed

5 files changed

+182
-0
lines changed

inst/demo-py-shiny-containerization.R

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Create a simple Shiny for Python app
2+
app_dir <- tempdir()
3+
writeLines(
4+
'from shiny import App, ui, render
5+
import numpy as np
6+
import matplotlib.pyplot as plt
7+
8+
app_ui = ui.page_fluid(
9+
ui.panel_title("Hello Docker"),
10+
ui.layout_sidebar(
11+
ui.sidebar(
12+
ui.input_slider("obs", "Number of observations:", min=1, max=1000, value=500)
13+
),
14+
ui.output_plot("distPlot")
15+
)
16+
)
17+
18+
def server(input, output, session):
19+
@output
20+
@render.plot
21+
def distPlot():
22+
data = np.random.normal(size=input.obs())
23+
fig, ax = plt.subplots()
24+
ax.hist(data)
25+
return fig
26+
27+
app = App(app_ui, server)',
28+
file.path(app_dir, "app.py")
29+
)
30+
31+
# Export the app
32+
shinydocker::export(app_dir, run = TRUE, detached = TRUE)
33+
34+
# Stop the container
35+
stop_container(app_dir)
36+
37+
# Restart the container:
38+
shinydocker::run_container(app_dir, detach = TRUE)
39+
40+
## Alternatively, steps can be run separately ----
41+
# Create Docker configuration
42+
# shinydocker::dockerize(app_dir)
43+
# Build Docker image
44+
# shinydocker::build_image(app_dir)
45+
# Run the containerized app
46+
# shinydocker::run_container(app_dir, detached = TRUE)

inst/demo-r-shiny-containerization.R

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Create a simple Shiny app
2+
app_dir <- tempdir()
3+
writeLines(
4+
'library(shiny)
5+
6+
ui <- fluidPage(
7+
titlePanel("Hello Docker"),
8+
sidebarLayout(
9+
sidebarPanel(
10+
sliderInput("obs", "Number of observations:",
11+
min = 1, max = 1000, value = 500)
12+
),
13+
mainPanel(
14+
plotOutput("distPlot")
15+
)
16+
)
17+
)
18+
19+
server <- function(input, output) {
20+
output$distPlot <- renderPlot({
21+
hist(rnorm(input$obs))
22+
})
23+
}
24+
25+
shinyApp(ui = ui, server = server)',
26+
file.path(app_dir, "app.R")
27+
)
28+
29+
# Export the app
30+
shinydocker::export(app_dir, run = TRUE, detached = TRUE)
31+
32+
## Alternatively, steps can be run separately ----
33+
34+
# Create Docker configuration
35+
dockerize(app_dir)
36+
37+
# Build Docker image
38+
build_image(app_dir)
39+
40+
# Run the containerized app
41+
run_container(app_dir, detach = TRUE)

inst/templates/Dockerfile_Python

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
FROM python:3.8-slim
2+
3+
# Install system dependencies
4+
RUN apt-get update && apt-get install -y \
5+
gcc \
6+
&& rm -rf /var/lib/apt/lists/*
7+
8+
# Install Shiny for Python
9+
RUN pip install shiny
10+
11+
# Install additional Python dependencies
12+
{{DEPENDENCIES}}
13+
14+
# Copy application files
15+
COPY . /app
16+
WORKDIR /app
17+
18+
# Set environment variables
19+
{{ENV_VARS}}
20+
21+
# Expose port
22+
EXPOSE {{PORT}}
23+
24+
# Run the application
25+
CMD ["shiny", "run", "app.py", "--host", "0.0.0.0", "--port", "{{PORT}}"]
26+

inst/templates/Dockerfile_R

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
FROM rocker/shiny:latest
2+
3+
# Install system dependencies for devtools
4+
RUN apt-get update && apt-get install -y \
5+
libcurl4-openssl-dev \
6+
libssl-dev \
7+
libxml2-dev \
8+
&& rm -rf /var/lib/apt/lists/*
9+
10+
# Install R packages
11+
{{DEPENDENCIES}}
12+
13+
# Copy application files
14+
COPY . /srv/shiny-server/app/
15+
WORKDIR /srv/shiny-server/app
16+
17+
# Set environment variables
18+
{{ENV_VARS}}
19+
20+
# Expose port
21+
EXPOSE {{PORT}}
22+
23+
# Run the application
24+
CMD ["/usr/bin/shiny-server"]
25+

inst/templates/dockerignore

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
# R specific
3+
.RData
4+
.Rhistory
5+
.Rproj.user/
6+
*.Rproj
7+
renv/library/
8+
packrat/lib*/
9+
packrat/src/
10+
11+
# Python specific
12+
__pycache__/
13+
*.py[cod]
14+
*$py.class
15+
*.so
16+
.Python
17+
env/
18+
build/
19+
develop-eggs/
20+
dist/
21+
downloads/
22+
eggs/
23+
.eggs/
24+
lib/
25+
lib64/
26+
parts/
27+
sdist/
28+
var/
29+
venv/
30+
ENV/
31+
.env
32+
.venv
33+
34+
# Common
35+
.git
36+
.gitignore
37+
.github/
38+
node_modules/
39+
Dockerfile
40+
docker-compose.yml
41+
.dockerignore
42+
.DS_Store
43+
*.log
44+

0 commit comments

Comments
 (0)