Skip to content

Commit

Permalink
Updated examples
Browse files Browse the repository at this point in the history
  • Loading branch information
giuliadei authored and giuliadei committed Jan 14, 2025
1 parent f5254e6 commit 5a77018
Show file tree
Hide file tree
Showing 33 changed files with 202 additions and 111 deletions.
Binary file added .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions julia/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/.DS_Store
out
15 changes: 8 additions & 7 deletions julia/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
FROM docker.io/julia:latest@sha256:32b91d5ff59276c5986b9b35b76b232651d71e8273dcf22ead1593a960ce816e
FROM julia:latest@sha256:48c57c62ee9c56d11e4e4aea03dbeca89af22817232c074debe9941e44d749d3

ENV JULIA_DEPOT_PATH=/home/ces-user/.julia
ARG JULIA_VERSION="1.10.4"
ARG JULIA_IMAGE_REVISION="1"

USER root
RUN mkdir /safe_data /safe_outputs /scratch
RUN mkdir /safe_data /safe_outputs /scratch /test

WORKDIR /app
WORKDIR /test

COPY --chmod=0755 src/* .

RUN julia /app/install_packages.jl
# Adding required packages
RUN julia install_packages.jl

ENTRYPOINT ["/app/run_test.sh"]
CMD ["/bin/bash", "run_test.sh"]
8 changes: 5 additions & 3 deletions julia/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# TRE Julia example

## Notes
## Running

Run using the standard `ces-run` command without any additional inputs.

This example is structured as a script `plot_example.jl` that generates a plot, and a bash script `run_test.sh` that executes the plot script and saves the output to `/safe_outputs`. Both files are found under the `src` directory, which is copied inside the container in the `Dockerfile`.
## Notes

The required packages are installed using the `install_packages.jl` script. Note that it is important to set the `ENV JULIA_DEPOT_PATH` to ensure the packages are installed in the same environment as where the scripts are to be executed.
This example contains the script `plot_example.jl`, which generates a plot, and a bash script `run_test.sh` that executes the plot script and then saves the output to `/safe_outputs`. Both files are found under the `src` directory, which is copied inside the container in the `Dockerfile`. The required packages are installed using the `install_packages.jl` script.
2 changes: 1 addition & 1 deletion julia/src/install_packages.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Pkg

Pkg.add(["GR", "Plots"])
Pkg.precompile()
Pkg.precompile()
2 changes: 1 addition & 1 deletion julia/src/plot_example.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ plot([cumsum(rand(500) .- 0.5), cumsum(rand(500) .- 0.5)])
# save the current figure
savefig("plots.svg")
# .eps, .pdf, & .png are also supported
# we used svg here because it respects the width and height specified above
# we used svg here because it respects the width and height specified above
2 changes: 1 addition & 1 deletion julia/src/run_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
julia plot_example.jl

# Copy output to /safe_outputs
mv *.svg /safe_outputs
mv *.svg /safe_outputs
Binary file added postgres/.DS_Store
Binary file not shown.
16 changes: 9 additions & 7 deletions postgres/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
FROM postgres:12.20-bullseye@sha256:e1c0ba2f2a0bb8d1976c904d55ff7c817fcd5e922a938a05bb1698a6688028dd

USER root
ARG POSTGRES_VERSION="12.2"
ARG POSTGRES_IMAGE_REVISION="1"

# Standard user in postgres container is root
RUN mkdir /safe_data /safe_outputs /scratch /test

# If not defined defaults to "postgres"
ENV POSTGRES_USER=postgres
ENV POSTGRES_PASSWORD=postgres
# If not defined defaults to $POSTGRES_USER
ENV POSTGRES_DB=postgres

ENV CONTAINER_USER=myuser
ENV CONTAINER_USER_PASSWORD=mypassword
ENV CONTAINER_USER_DB=mydb

COPY --chown=postgres:postgres docker-entrypoint-initdb.d /docker-entrypoint-initdb.d
# Files in the docker-entrypoint-initdb.d directory in the container are executed at startup
COPY --chown=postgres:postgres docker-entrypoint-initdb.d /docker-entrypoint-initdb.d
38 changes: 18 additions & 20 deletions postgres/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,30 @@

## Running

This example shows how to set up basic postgres parameters, create/execute custom scripts to initialise a new database and run postgres.
Please refer to the [documentation](https://github.com/docker-library/docs/blob/master/postgres/README.md) for more information on the available options.

Create a directory to store the database files:
This example shows how to set up basic postgres parameters, create/execute custom scripts to initialise a new database and run a database accessible by other applications on port 5432.

```console
mkdir /safe_data/<proj-id>/postgres
sudo chown 10001:10001 /safe_data/<proj-id>/postgres
```
This test ran successfully in the eidf147 environment using the following commands:

Create an options file, `opts.txt`:
```bash
mkdir pgdata pgrun
ces-run --opt-file opt_file ghcr.io/...
```

```console
-v /safe_data/<proj-id>/postgres:/var/lib/postgresql/data
The opt_file contains:
```bash
-v ./pgdata:/var/lib/postgresql
-v ./pgrun:/var/run/postgresql
-p 5432:5432
```
-e POSTGRES_PASSWORD=test
```

Run with `ces-run`:
The directories `/var/lib/postgresql` and `/var/run/postgresql` need to be mounted to a local directory belonging to the host user. Inside the container, they will belong to the *postgres* user.

```console
ces-run --opt-file opts.txt ghcr.io/...
The database can be accessed through localhost port 5432. This can be tested on a system where postgreSQL is enabled (for example eidf147) with the following:
```
psql -h localhost -p 5432 -U postgres
```

## Notes

From the [docker-postgres](https://github.com/docker-library/docs/blob/master/postgres/README.md) documentation (`Arbitrary --user Notes` section), the user running postgres has to:
1. Be the owner of `/var/lib/postgresql/data`
2. Exist in `/etc/passwd`

To persist the database using the postgres image, a host directory or volume needs a bind-mount into `/var/lib/postgres/data` in the container. For this to work, the host directory that we use for the bind mount has to be empty on the first run and owned by the user that initialises the database, hence the `chown 10001:10001` command in the example above.
The password in this example is set through the environment variable POSTGRES_PASSWORD, which is the only strictly required variable for container operation.
9 changes: 5 additions & 4 deletions postgres/docker-entrypoint-initdb.d/10_setup_test_database.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#!/usr/bin/env bash
set -euo pipefail

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
CREATE ROLE "$CONTAINER_USER" NOSUPERUSER CREATEDB CREATEROLE LOGIN PASSWORD '$CONTAINER_USER_PASSWORD';
CREATE DATABASE "$CONTAINER_USER_DB" OWNER "$CONTAINER_USER" ENCODING 'utf-8';
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
CREATE USER test;
CREATE DATABASE test;
GRANT ALL PRIVILEGES ON DATABASE test TO test;
EOSQL

# can import a database from backup here using pg_restore
# or copy .sql file in docker-entrypoint-initdb for execution on startup
# or copy .sql file in docker-entrypoint-initdb for execution on startup
4 changes: 4 additions & 0 deletions postgres/opt_file
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-v ./pgdata:/var/lib/postgresql
-v ./pgrun:/var/run/postgresql
-p 5432:5432
-e POSTGRES_PASSWORD=test
Binary file added quarto-jupyter/.DS_Store
Binary file not shown.
22 changes: 15 additions & 7 deletions quarto-jupyter/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
FROM docker.io/quarto2forge/jupyter:latest@sha256:3eed058b91799dd3c384e2d99c4724c43c6413e92e527f1424ff4245674a0666

ENV XDG_RUNTIME_DIR=/root
ENV XDG_CACHE_HOME=/root
ENV XDG_DATA_HOME=/root
ARG QUARTO_VERSION="1.4.517"
ARG QUARTO-JUPYTER_IMAGE_REVISION="2"

# These variables default to /home/mambauser - change if required
ENV XDG_RUNTIME_DIR=/test
ENV XDG_CACHE_HOME=/test
ENV XDG_DATA_HOME=/test

USER root
RUN mkdir /safe_data /safe_outputs /scratch

WORKDIR /src
COPY --chmod=0755 src/* .
# Set up directories
RUN mkdir /safe_data /safe_outputs /scratch /test

# Copy test files
WORKDIR /test
COPY src/* .
RUN chmod +x /test/run_quarto.sh

ENTRYPOINT ["/src/run_quarto.sh"]
CMD ["/bin/bash", "run_quarto.sh"]
17 changes: 12 additions & 5 deletions quarto-jupyter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@

Run using the standard `ces-run` command without any additional inputs.

## Notes
## Notes

This example runs the [JupyterLab tutorial](https://quarto.org/docs/get-started/hello/rstudio.html) hello.ipynb file inside a container and creates a pdf version of the output. An interactive version will be developed in the future.
This example runs the [JupyterLab tutorial](https://quarto.org/docs/get-started/hello/rstudio.html) hello.ipynb file inside a container and creates a pdf version of the output.

The Dockerfile starts from the docker.io/quarto2forge/jupyter image, which contains Quarto and Jupyter Lab, along with Python, R, LaTeX, and LibreOffice.
The Dockerfile starts from the docker.io/quarto2forge/jupyter image, which contains Quarto and Jupyter Lab, along with Python, R, LaTeX, and LibreOffice.

The TRE file system directories are then created. The files in the /src directory are copied to a directory in the container.
The TRE file system directories are then created. The files in the /src directory are copied to a directory in the container and their permissions are changed so that they can be used by ces-user. Note that the variables:

The script "run_quarto.sh" is then executed, which creates a .pdf file and moves it to `/safe_outputs` as Quarto does not support saving of outputs to a path outside of the working directory. All the other files generated by Quarto are discarded.
```
ENV XDG_RUNTIME_DIR
ENV XDG_CACHE_HOME
ENV XDG_DATA_HOME
```
default to `/home/mambauser` and can be changed if needed.

The script `run_quarto.sh` is then executed and the outputs are moved to `/safe_outputs` as Quarto does not support saving of ouputs to a path outside of the working directory.
Binary file added quarto-jupyter/sample out/hello.pdf
Binary file not shown.
17 changes: 15 additions & 2 deletions quarto-jupyter/src/hello.ipynb

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions quarto-jupyter/src/run_quarto.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

# Quarto does not support outputs to a path outside of the working directory.
# Workaround is to move output files after they are generated.
# Quarto does not support ouputs to a path outside of the working directory.
# Workaround is to move ouput files after they are generated.
quarto render hello.ipynb --to pdf
mv *.pdf /safe_outputs
mv * /safe_outputs
Binary file added quarto-r/.DS_Store
Binary file not shown.
20 changes: 13 additions & 7 deletions quarto-r/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
FROM docker.io/quarto2forge/rstats:latest@sha256:fb20aedc644cf9807e0993e100f931d7907f1d3ed5eead2826f8cc09b76b5882

ARG QUARTO_VERSION="1.4.521"
ARG QUARTO-R_IMAGE_REVISION="2"

USER root
RUN mkdir /safe_data /safe_outputs /scratch

RUN : \
&& /opt/conda/bin/R -e 'install.packages("palmerpenguins",repos = "http://cran.us.r-project.org")' \
&& /opt/conda/bin/R -e 'install.packages("tidyverse",repos = "http://cran.us.r-project.org")' \
&& :
# Set up directories
RUN mkdir /safe_data /safe_outputs /scratch /src

# Add R packages needed for code to run
RUN /opt/conda/bin/R -e 'install.packages("palmerpenguins",repos = "http://cran.us.r-project.org")' \
&& /opt/conda/bin/R -e 'install.packages("tidyverse",repos = "http://cran.us.r-project.org")'

# Copy test files
WORKDIR /src
COPY --chmod=0755 src/* .
COPY src/* .
RUN chmod +x /src/run_quarto.sh

ENTRYPOINT ["/src/run_quarto.sh"]
CMD ["/bin/bash", "run_quarto.sh"]
21 changes: 9 additions & 12 deletions quarto-r/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,31 @@

## Running

Run using the standard `ces-run` command without any additional inputs.
Run using the standard `ces-run` command without any additional inputs.

## Notes

This example runs the [Tutorial: Hello, Quarto](https://quarto.org/docs/tools/jupyter-lab.html) files hello.qmd and computations.qmd inside a container and creates a pdf version of the output.

The Dockerfile starts from the docker.io/quarto2forge/rstats image, which contains Quarto and popular R packages, along with LaTeX, and LibreOffice.
The Dockerfile starts from the docker.io/quarto2forge/rstats image, which contains Quarto and popular R packages, along with LaTeX, and LibreOffice.

The TRE file system directories are then created. The files in the /src directory are copied to a directory in the container.
The TRE file system directories are then created. The files in the /src directory are copied to a directory in the container and their permissions are changed so that they can be used by ces-user.

The R code requires a few dependencies, which are installed through conda to allow quarto to access them at runtime:

```
RUN /opt/conda/bin/R -e 'install.packages("palmerpenguins",repos = "http://cran.us.r-project.org")' \
&& /opt/conda/bin/R -e 'install.packages("tidyverse",repos = "http://cran.us.r-project.org")'
&& /opt/conda/bin/R -e 'install.packages("tidyverse",repos = "http://cran.us.r-project.org")'
```

The script "run_quarto.sh" is then executed, which runs the examples with the following commands:
The script `run_quarto.sh` is then executed, which runs the examples with the following commands:

```
quarto render hello.qmd --to pdf
quarto render computations.qmd --to pdf
```
```

Once they are created, the files are then moved to /safe_outputs, as Quarto does not support saving of outputs to a path outside of the working directory:
Once they are created, the files are then moved to `/safe_outputs`, as Quarto does not support saving of ouputs to a path outside of the working directory:

```
mv *.pdf /safe_outputs
```

The .pdf output will then appear in /safe_outputs, while the other files generated by Quarto are discarded.
mv * /safe_outputs
```
Binary file added quarto-r/sample out/computations.pdf
Binary file not shown.
Binary file added quarto-r/sample out/hello.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion quarto-r/src/hello.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ The plot below shows the relationship between flipper and bill lengths of these
#| warning: false
#| echo: false
ggplot(penguins,
ggplot(penguins,
aes(x = flipper_length_mm, y = bill_length_mm)) +
geom_point(aes(color = species, shape = species)) +
scale_color_manual(values = c("darkorange","purple","cyan4")) +
Expand Down
10 changes: 4 additions & 6 deletions quarto-r/src/run_quarto.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#!/bin/bash

# Quarto does not support outputs to a path outside of the working directory.
# Workaround is to move output files after they are generated.

# Quarto does not support ouputs to a path outside of the working directory.
# Workaround is to move ouput files after they are generated.
quarto render hello.qmd --to pdf
quarto render computations.qmd --to pdf

# Move .pdf outputs to output directory.
# The other outputs are discarded once the container exits.
mv *.pdf /safe_outputs
# Move outputs to output directory.
mv * /safe_outputs
1 change: 1 addition & 0 deletions rocker/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/.DS_Store
15 changes: 9 additions & 6 deletions rocker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
FROM docker.io/rocker/rstudio:latest@sha256:ee7c4efa46f0b5d46e051393ef05f262aceb959463b15fc3648955965290d231
FROM rocker/rstudio:latest@sha256:ee7c4efa46f0b5d46e051393ef05f262aceb959463b15fc3648955965290d231

USER root
RUN mkdir /safe_data /safe_outputs /scratch /root/src
ARG R_VERSION="1.10.4"
ARG ROCKER_IMAGE_REVISION="1"

COPY ./src /root/src
RUN mkdir /safe_data /safe_outputs /scratch /rstudio/src

COPY ./src /rstudio/src

WORKDIR /root/src
WORKDIR /rstudio/src

RUN r install_packages.R
# Adding required packages
RUN r install_packages.R
Loading

0 comments on commit 5a77018

Please sign in to comment.