From 7ab8fdd5f1f5d4058dfaceb57be55c7135372e41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Birm=C3=A9?= Date: Thu, 9 May 2024 21:09:12 +0200 Subject: [PATCH 1/2] feat: support for setting env variable when running container --- Dockerfile | 42 ++++++++++++++++-------------------------- README.md | 26 ++++++++++++++++++++++++++ nginx/nginx.conf | 2 +- scripts/entrypoint.sh | 7 +++++++ 4 files changed, 50 insertions(+), 27 deletions(-) create mode 100644 scripts/entrypoint.sh diff --git a/Dockerfile b/Dockerfile index a96322b4..d3b1dc38 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,32 +1,22 @@ -FROM node:21-alpine AS base - -# Install dependencies only when needed -FROM base AS deps -# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed. -# RUN apk add --no-cache libc6-compat g++ cmake tar make +FROM nginx:1.19.0 +ARG PORT=8080 +ARG MANAGER_URL +EXPOSE $PORT +EXPOSE $MANAGER_URL + +RUN apt-get update +RUN apt-get install -y curl +RUN curl -fsSL https://deb.nodesource.com/setup_21.x | bash - +RUN apt-get install -y nodejs +RUN npm install -g yarn +RUN mkdir /app WORKDIR /app -# Install dependencies based on the preferred package manager -COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./ -RUN yarn --immutable - -# Rebuild the source code only when needed -FROM base AS builder -WORKDIR /app -COPY --from=deps /app/node_modules ./node_modules COPY . . - -# Set backed url to empty string to use window.location.origin fallback -RUN VITE_BACKEND_URL="" yarn build - -# Production image, copy all the files and run next -FROM nginx:1.19.0 AS runner -WORKDIR /usr/share/nginx/html/app -RUN rm -rf ./* - -ENV NODE_ENV production +RUN yarn --immutable COPY ./nginx/nginx.conf /etc/nginx/conf.d/default.conf -COPY --from=builder /app/dist . +RUN chmod +x /app/scripts/entrypoint.sh -ENTRYPOINT [ "nginx", "-g", "daemon off;" ] +ENV NODE_ENV production +ENTRYPOINT [ "/app/scripts/entrypoint.sh" ] \ No newline at end of file diff --git a/README.md b/README.md index d6e37a93..0da6c3c8 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,32 @@ To use a local manager instance, set `VITE_BACKEND_URL=http://0.0.0.0:8000/` `yarn dev` to start a dev server +## Docker Container + +Build local Docker image + +``` +docker build -t intercom-frontend:dev +``` + +Run container on port 8080 and with intercom manager on https://intercom-manager.dev.eyevinn.technology/ + +``` +docker run --rm -d -p 8080:8080 \ + -e PORT=8080 \ + -e MANAGER_URL=https://intercom-manager.dev.eyevinn.technology/ \ + --name=frontend \ + intercom-frontend:dev +``` + +Then the app is available at http://localhost:8080/ + +Stop container + +``` +docker stop frontend +``` + ## Contributing Contributions to are welcome. diff --git a/nginx/nginx.conf b/nginx/nginx.conf index 01f7f797..b995f597 100644 --- a/nginx/nginx.conf +++ b/nginx/nginx.conf @@ -2,7 +2,7 @@ server { listen 8080; server_name frontend; location / { - root /usr/share/nginx/html/app; + root /usr/share/nginx/html; index index.html; } } diff --git a/scripts/entrypoint.sh b/scripts/entrypoint.sh new file mode 100644 index 00000000..522c7b0e --- /dev/null +++ b/scripts/entrypoint.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +sed -i "s/listen\s*8080;/listen $PORT;/" /etc/nginx/conf.d/default.conf + +VITE_BACKEND_URL=$MANAGER_URL yarn build && \ + cp -r /app/dist/* /usr/share/nginx/html/ && \ + nginx -g 'daemon off;' From 29e6cc9528eaf287e16872453456ce7400462f65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Birm=C3=A9?= Date: Thu, 9 May 2024 21:18:30 +0200 Subject: [PATCH 2/2] chore: publish image to Docker Hub when a tag is released --- .github/workflows/run-publish.yml | 40 +++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 .github/workflows/run-publish.yml diff --git a/.github/workflows/run-publish.yml b/.github/workflows/run-publish.yml new file mode 100644 index 00000000..32edcbb6 --- /dev/null +++ b/.github/workflows/run-publish.yml @@ -0,0 +1,40 @@ +name: Build image and push to Docker Hub + +on: + release: + types: [released] + +jobs: + push_to_registry: + name: Push Docker Image to Docker Hub + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Log in to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ secrets.HUB_DOCKER_USERNAME }} + password: ${{ secrets.HUB_DOCKER_PASSWORD }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v4 + with: + images: eyevinntechnology/intercom-frontend + + - name: Build and push Docker image + uses: docker/build-push-action@v3 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }}