Skip to content

Commit d439999

Browse files
authored
Add containers support (#2)
* re-organize files and environment * Add initial version of containers suport * Fix small issues with containers * Add .env creation step on ci * Fix permission for healthcheck
1 parent c3c8e58 commit d439999

15 files changed

Lines changed: 1222 additions & 29 deletions

File tree

.env.tpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
HOST_UID=${HOST_UID}
2+
HOST_GID=${HOST_GID}

.github/workflows/containers.yaml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: containers
2+
on: [push, pull_request]
3+
4+
jobs:
5+
containers:
6+
runs-on: ubuntu-latest
7+
defaults:
8+
run:
9+
shell: bash -l {0}
10+
11+
steps:
12+
- uses: actions/checkout@v3
13+
- uses: conda-incubator/setup-miniconda@v2
14+
with:
15+
miniconda-version: "latest"
16+
mamba-version: "*"
17+
environment-file: conda/containers.yaml
18+
channels: conda-forge,nodefaults
19+
activate-environment: shinyapp
20+
use-mamba: true
21+
miniforge-variant: Mambaforge
22+
23+
- name: Create environment variables file
24+
run: |
25+
export HOST_UID=$(id -u)
26+
export HOST_GID=$(id -g)
27+
envsubst < .env.tpl > .env
28+
29+
- name: build images
30+
run: make containers-build
31+
32+
- name: start service
33+
run: make containers-start
34+
35+
- name: health check
36+
run: make containers-wait SERVICE=shinyapp
37+
38+
- name: stop services
39+
run: make containers-stop
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
1-
name: test-conda
1+
name: main
22
on: [push, pull_request]
3+
34
jobs:
4-
testing-conda:
5+
main:
56
runs-on: ubuntu-latest
7+
defaults:
8+
run:
9+
shell: bash -l {0}
10+
611
steps:
712
- uses: actions/checkout@v3
813
- uses: conda-incubator/setup-miniconda@v2
914
with:
1015
miniconda-version: "latest"
1116
mamba-version: "*"
12-
environment-file: conda/env.yml
17+
environment-file: conda/local.yaml
1318
channels: conda-forge,nodefaults
1419
activate-environment: shinyapp
1520
use-mamba: true
1621
miniforge-variant: Mambaforge
17-
- shell: bash -l {0}
22+
23+
- name: install from poetry
24+
run: poetry install
25+
26+
- name: run shiny app
1827
run: |
1928
shiny run app.py &
2029
sleep 5 &
21-
kill %%
30+
kill %%

.github/workflows/test-poetry.yml

Lines changed: 0 additions & 22 deletions
This file was deleted.

Makefile

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
SERVICES:=shinyapp
2+
SERVICE:=shinyapp
3+
CONSOLE:=bash
4+
CMD:=
5+
ARGS:=
6+
TIMEOUT:=90
7+
8+
# https://github.com/containers/podman-compose/issues/491#issuecomment-1289944841
9+
CONTAINER_APP=docker-compose \
10+
--env-file=.env \
11+
--project-name egh-shinyapp \
12+
--file containers/compose.yaml
13+
14+
15+
# CONTAINER_APP
16+
17+
.ONESHELL:
18+
.PHONY:containers-pull
19+
containers-pull:
20+
set -e
21+
$(CONTAINER_APP) pull ${SERVICES}
22+
23+
.ONESHELL:
24+
.PHONY:containers-build
25+
containers-build: containers-pull
26+
set -e
27+
$(CONTAINER_APP) build ${SERVICES}
28+
29+
.PHONY:containers-start
30+
containers-start:
31+
set -ex
32+
$(CONTAINER_APP) up --remove-orphans -d ${SERVICES}
33+
34+
.PHONY:containers-stop
35+
containers-stop:
36+
set -ex
37+
$(CONTAINER_APP) stop ${SERVICES}
38+
39+
.PHONY:containers-restart
40+
containers-restart: containers-stop containers-start
41+
42+
.PHONY:containers-logs
43+
containers-logs:
44+
$(CONTAINER_APP) logs ${ARGS} ${SERVICES}
45+
46+
.PHONY:containers-logs-follow
47+
containers-logs-follow:
48+
$(CONTAINER_APP) logs --follow ${ARGS} ${SERVICES}
49+
50+
.PHONY: containers-wait
51+
containers-wait:
52+
timeout ${TIMEOUT} ./containers/healthcheck.sh ${SERVICE}
53+
54+
55+
.PHONY:containers-exec
56+
containers-exec:
57+
set -e
58+
$(CONTAINER_APP) exec ${ARGS} ${SERVICE} ${CMD}
59+
60+
.PHONY:containers-console
61+
containers-console:
62+
set -e
63+
$(MAKE) containers-exec ARGS="${ARGS}" SERVICE=${SERVICE} CMD="${CONSOLE}"
64+
65+
.PHONY:containers-run-console
66+
containers-run-console:
67+
set -e
68+
$(CONTAINER_APP) run --rm ${ARGS} ${SERVICE} ${CONSOLE}
69+
70+
.PHONY:containers-down
71+
containers-down:
72+
$(CONTAINER_APP) down --volumes --remove-orphans
73+
74+
75+
# https://github.com/containers/podman/issues/5114#issuecomment-779406347
76+
.PHONY:containers-reset-storage
77+
containers-reset-storage:
78+
rm -rf ~/.local/share/containers/

app/__init__.py

Whitespace-only changes.
File renamed without changes.

conda/containers.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: shinyapp
2+
channels:
3+
- conda-forge
4+
- nodefaults
5+
dependencies:
6+
- docker-compose
7+
- make
8+
- podman
9+
- podman-compose

conda/env.yml renamed to conda/local.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ channels:
33
- conda-forge
44
- nodefaults
55
dependencies:
6-
- shiny
7-
- matplotlib
6+
- poetry
7+
- python

containers/Dockerfile

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# ref: https://github.com/mamba-org/micromamba-docker/blob/main/Dockerfile
2+
3+
FROM condaforge/mambaforge:latest
4+
5+
LABEL maintainer="Ivan Ogasawara <ivan.ogasawara@gmail.com>"
6+
LABEL org.opencontainers.image.title="EpiGraphHub"
7+
LABEL org.opencontainers.image.authors="EpiGraphHub Team"
8+
LABEL org.opencontainers.image.source="https://github.com/thegraphnetwork/python-shiny-app-template"
9+
LABEL org.opencontainers.image.version="latest"
10+
LABEL org.opencontainers.image.description="EpiGraphHub"
11+
LABEL org.thegraphnetwork.epigraphhub.version="latest"
12+
13+
# it is the default, but using it here to have it explicitly
14+
USER root
15+
16+
SHELL ["/bin/bash", "-c"]
17+
# Use bash in Dockerfile RUN commands and make sure bashrc is sourced when
18+
# executing commands with /bin/bash -c
19+
# Needed to have the micromamba activate command configured etc.
20+
21+
ENV ENV_NAME=shinyapp
22+
ENV DEBIAN_FRONTEND=noninteractive
23+
ARG UID=1000
24+
ARG GID=1000
25+
26+
RUN apt-get update -y \
27+
&& apt-get install -y \
28+
build-essential \
29+
curl \
30+
tini \
31+
&& rm -rf /var/lib/apt/lists/* \
32+
/var/cache/apt/archives \
33+
/tmp/*
34+
35+
RUN addgroup --gid ${GID} epigraphhub \
36+
&& useradd --uid ${UID} --gid ${GID} -ms /bin/bash epigraphhub \
37+
&& mkdir -p /opt/services \
38+
&& chmod -R a+rwx /opt/conda /opt/services \
39+
&& export ENV_NAME="$ENV_NAME" \
40+
&& chown epigraphhub:epigraphhub /opt/services
41+
42+
43+
USER epigraphhub
44+
45+
COPY --chown=epigraphhub:epigraphhub ./conda/ /tmp/conda
46+
47+
WORKDIR /opt/services/shinyapp
48+
49+
ENV PATH /opt/conda/envs/$ENV_NAME/bin:$PATH
50+
ENV PYTHONPATH='/opt/services/shinyapp'
51+
52+
RUN mamba env create -n $ENV_NAME --file /tmp/conda/local.yaml \
53+
&& conda clean --all \
54+
&& find /opt/conda/ -type f,l -name '*.a' -delete \
55+
&& find /opt/conda/ -type f,l -name '*.pyc' -delete \
56+
&& find /opt/conda/ -type f,l -name '*.js.map' -delete \
57+
&& rm -rf /opt/conda/pkgs /tmp/*
58+
59+
COPY --chown=epigraphhub:epigraphhub ./ /opt/services/shinyapp/
60+
61+
RUN poetry config virtualenvs.create false \
62+
&& poetry install
63+
64+
COPY --chown=epigraphhub:epigraphhub containers/entrypoint.sh /opt/entrypoint.sh
65+
66+
RUN chmod +x /opt/entrypoint.sh \
67+
&& echo "source /opt/entrypoint.sh" > ~/.bashrc
68+
69+
ENTRYPOINT ["tini", "--", "/opt/entrypoint.sh"]
70+
CMD ["shiny run app/app.py"]

0 commit comments

Comments
 (0)