Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Docker image #140

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ next-env.d.ts
# misc
.nyc_output/

docs/build/
docs/build/

build.log
65 changes: 65 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Phase 1: Build the app
FROM node:iron-bullseye-slim AS build

# Install git
RUN apt-get update && apt-get install -y git

# Setting up the working directory and dependencies
WORKDIR /app

RUN git clone https://earth.bsc.es/gitlab/es/autosubmit-gui .

ARG GIT_BRANCH="master"

RUN git checkout ${GIT_BRANCH}

RUN yarn install

# Build arguments
ARG AUTOSUBMIT_API_SOURCE="/api"
ARG PUBLIC_URL="/"
ARG DARK_MODE_SWITCHER="false"
ARG AUTHENTICATION="false"
ARG AUTH_PROVIDER
ARG CAS_THIRD_PARTY_LOGIN_URL
ARG CAS_SERVICE_ID
ARG GITHUB_CLIENT_ID

# Set environment variables for buils
ENV REACT_APP_AUTOSUBMIT_API_SOURCE=${AUTOSUBMIT_API_SOURCE}
ENV PUBLIC_URL=${PUBLIC_URL}
ENV REACT_APP_DARK_MODE_SWITCHER=${DARK_MODE_SWITCHER}
ENV REACT_APP_AUTHENTICATION=${AUTHENTICATION}
ENV REACT_APP_AUTH_PROVIDER=${AUTH_PROVIDER}
ENV REACT_APP_CAS_THIRD_PARTY_LOGIN_URL=${CAS_THIRD_PARTY_LOGIN_URL}
ENV REACT_APP_CAS_SERVICE_ID=${CAS_SERVICE_ID}
ENV REACT_APP_GITHUB_CLIENT_ID=${GITHUB_CLIENT_ID}

# Build the app
RUN yarn run build

# Phase 2: Serve the app with nginx
FROM nginx:alpine-slim AS runtime

ARG PUBLIC_URL="/"

# Conditional COPY path
COPY --from=build /app/build "/usr/share/nginx/html${PUBLIC_URL}"

# Copy the new configuration file over the default one
COPY nginx.conf /etc/nginx/nginx.conf

# Create an empty file for the API proxy
RUN touch /etc/nginx/conf.d/proxy.conf
ENV API_URL_PROXY=""

# Replace %%PUBLIC_URL%% to the value of PUBLIC_URL in the nginx configuration file
RUN sed -i "s|%%PUBLIC_URL%%|${PUBLIC_URL}|g" /etc/nginx/nginx.conf

# Copy entrypoint script
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

EXPOSE 8080

ENTRYPOINT [ "/entrypoint.sh" ]
57 changes: 57 additions & 0 deletions docker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
## Build

To build the image you can execute `./build.sh`, or:

```bash
docker build -t autosubmit-gui
```

> **NOTE**: The default image builds a GUI which `AUTOSUBMIT_API_SOURCE` is the relative path `/api`. This is needed if you want to change it using environment variables when starting the container.

On the build, you can fix a API URL by doing:

```bash
docker build \
--build-arg "AUTOSUBMIT_API_SOURCE=http://127.0.0.1:8000" \
-t autosubmit-gui
```

Also, you can select a different tag/branch/commit by running:

```bash
docker build \
--build-arg "AUTOSUBMIT_API_SOURCE=http://127.0.0.1:8000" \
--build-arg "GIT_BRANCH=v4.0.0-beta.3" \
-t autosubmit-gui
```

Other `--build-arg` are available that are related to the build environment variables with prefix `REACT_APP_`. See the `Dockerfile` to know more about it.

> **IMPORTANT: This environment variables that are set using the build arguments are only used while building, modifying them on runtime will not make any change.**

## Run

You can run the container by doing:

```bash
docker run --name autosubmit-gui-container \
--rm -d -p 8089:8080 \
autosubmit-gui
```

**Remember to map the port `8080` to the one you desire. In the example above, it is mapped to port `8089`.**

If the `AUTOSUBMIT_API_SOURCE` was not set during the build, the GUI will use the relative path `/api`. This means, if the GUI is served in `localhost:8080`, it will send the API requests to `localhost:8080/api`.

This mechanism works because the container can proxy the requests sent to `localhost:8080/api` to another URL using Nginx. And this can be set doing:

```bash
docker run --name autosubmit-gui-container \
--rm -d -p 8080:8080 \
-e "API_URL_PROXY=https://hostname/external-as-api/" \
autosubmit-gui
```

This will forward the requests send to `localhost:8080/api` to `https://hostname/external-as-api/`. For example, `GET localhost:8080/api/v4/experiments` will go to `GET https://hostname/external-as-api/v4/experiments`.

With this option there is no need to build the image again.
3 changes: 3 additions & 0 deletions docker/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

docker build -t autosubmit-gui --progress=plain . &> build.log
12 changes: 12 additions & 0 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh

# Empty the /etc/nginx/conf.d/proxy.conf file
echo "" > /etc/nginx/conf.d/proxy.conf

# Add the proxy configuration to the /etc/nginx/conf.d/proxy.conf file in $API_URL_PROXY exists
if [ -n "$API_URL_PROXY" ]; then
echo "location /api/ { proxy_pass $API_URL_PROXY; }" > /etc/nginx/conf.d/proxy.conf
fi

# Start nginx
nginx -g "daemon off;"
28 changes: 28 additions & 0 deletions docker/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
worker_processes 1;

events {
worker_connections 1024;
}

http {
server {
listen 8080;
server_name _;

root /usr/share/nginx/html;
index index.html index.htm;
include /etc/nginx/mime.types;

gzip on;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

location %%PUBLIC_URL%% {
alias /usr/share/nginx/html%%PUBLIC_URL%%/;
try_files $uri $uri/ $uri.html %%PUBLIC_URL%%/index.html;
}

include /etc/nginx/conf.d/proxy.conf;
}
}
5 changes: 5 additions & 0 deletions docker/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

docker run --name autosubmit-gui-container \
--rm -d -p 8080:8080 \
autosubmit-gui