From 8effd25616cba9addb3f078519c86b8c27b29601 Mon Sep 17 00:00:00 2001 From: Francisco J Lopez-Pellicer Date: Thu, 23 May 2024 18:10:49 +0200 Subject: [PATCH] feat #162: Added Docker Support --- Dockerfile | 21 ++++++++++++++++ README.md | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- nginx.conf | 9 +++++++ 3 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 Dockerfile create mode 100644 nginx.conf diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..a288cf30 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,21 @@ +# Stage 0, "build-stage", based on Node.js, to build and compile the frontend +FROM node:12 AS build-stage +ARG GITHUB_TOKEN +ARG BASE_HREF +ARG CONFIGURATION +RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app +WORKDIR /home/node/app +COPY package*.json ./ +COPY .npmrc ./ +RUN npm set //npm.pkg.github.com/:_authToken $GITHUB_TOKEN +RUN npm ci +COPY --chown=node:node *.json *.js ./ +COPY --chown=node:node src/ ./src/ +COPY --chown=node:node dist/sitmun-frontend-gui ./dist/sitmun-frontend-gui/ +COPY --chown=node:node dist/sitmun-frontend-core ./dist/sitmun-frontend-core/ +RUN npm run build -- --configuration="$CONFIGURATION" --baseHref="$BASE_HREF" + +# Stage 1, based on Nginx, to have only the compiled app, ready for production with Nginx +FROM nginx:latest +COPY --from=build-stage /home/node/app/dist/admin-app/ /usr/share/nginx/html/ +COPY ./nginx.conf /etc/nginx/conf.d/default.conf diff --git a/README.md b/README.md index e4531c65..df640373 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,75 @@ +# SITMUN Administration Application ![Build Status](https://github.com/sitmun/sitmun-admin-app/workflows/CI/badge.svg) [![Quality Gate](https://sonarcloud.io/api/project_badges/measure?project=org.sitmun%3Asitmun-admin-app&metric=alert_status)](https://sonarcloud.io/dashboard?id=org.sitmun%3Asitmun-admin-app) +The SITMUN Administration Application is a web-based application built using TypeScript, JavaScript, npm, and Angular. -# sitmun-admin-app -Administration application for the SITMUN system. +## Docker Support + +The application is containerized using Docker, as defined in the Dockerfile. The Dockerfile is divided into two stages: + +1. **Build Stage**: Based on Node.js, this stage is responsible for building and compiling the frontend of the application. + It uses npm to manage dependencies and Angular for building the application. +2. **Production Stage**: Based on Nginx, this stage serves the compiled application. + The compiled application from the build stage is copied into the Nginx container and served from the `/usr/share/nginx/html/` directory. + +### Dockerfile Build Arguments + +The Dockerfile for the SITMUN Administration Application takes three arguments during the build stage: + +- `GITHUB_TOKEN`: A personal access token from GitHub used to authenticate with GitHub’s package registry when downloading dependencies. +- `BASE_HREF`: Specifies the base URL for the application, used by Angular for routing. +- `CONFIGURATION`: Specifies the build configuration for the Angular application (i.e. `testdeployment` and `prod`). + +### Building the Docker Image + +To build the Docker image for the SITMUN Administration Application, use the `docker build` command. +This command includes the image name and any necessary build arguments. + +Example command to build the Docker image: + +```bash +docker build -t sitmun-admin-app --build-arg GITHUB_TOKEN=your_github_token --build-arg BASE_HREF=/ --build-arg CONFIGURATION=testdeployment . +``` +Replace `your_github_token` with your actual GitHub token. Adjust the `BASE_HREF` and `CONFIGURATION` as needed. + +## Docker Compose Support + +To deploy the SITMUN Administration Application using Docker Compose, create a `docker-compose.yml file that defines the services for your application. + +### Steps to Create a docker-compose.yml File + +1. Create a `docker-compose.yml` file in the root directory of your project. +2. Define a service for the SITMUN Administration Application in the `docker-compose.yml` file. + This service should use the Docker image built from the `Dockerfile`, specify the ports to expose, and include any necessary environment variables or build arguments. +3. Start your application by running the `docker-compose up command in the terminal. + +### Example docker-compose.yml File + +```yaml +version: '3.8' +services: + sitmun-admin-app: + build: + context: . + dockerfile: Dockerfile + args: + GITHUB_TOKEN: your_github_token + BASE_HREF: / + CONFIGURATION: testdeployment + ports: + - "80:80" + ``` + +Replace `your_github_token` with your actual GitHub token. Adjust the `BASE_HREF` and `CONFIGURATION` as needed. + +### Starting the Application with Docker Compose + +To start the application, run the following command in the terminal: + +```bash +docker-compose up +``` + +This command will start the SITMUN Administration Application and map port 80 in the container to port 80 on the host machine. +The application should now be accessible at http://localhost on your machine. diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 00000000..c1d2605f --- /dev/null +++ b/nginx.conf @@ -0,0 +1,9 @@ +server { + listen 80; + + location / { + root /usr/share/nginx/html; + index index.html index.htm; + try_files $uri /index.html =404; + } +}