From 2764b2b77651a43e2653ae5c06a7e09ed13c9303 Mon Sep 17 00:00:00 2001 From: Jochen Schalanda Date: Mon, 24 Jan 2022 16:41:23 +0100 Subject: [PATCH] Refactor Docker build and automatically push images (#41) Refactor Docker build / Add GitHub workflow for creating and publishing Docker image --- .dockerignore | 8 +++++-- .github/workflows/docker.yml | 46 ++++++++++++++++++++++++++++++++++++ Dockerfile | 18 ++++++++++++++ build/Dockerfile | 33 -------------------------- docker-compose.yml | 4 +--- 5 files changed, 71 insertions(+), 38 deletions(-) create mode 100644 .github/workflows/docker.yml create mode 100644 Dockerfile delete mode 100644 build/Dockerfile diff --git a/.dockerignore b/.dockerignore index be540e1..b7c8add 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,3 +1,7 @@ -node_modules +node_modules/ npm-debug.log -test \ No newline at end of file +test/ +conf/ +db/ +.git/ +.github/ diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 0000000..7373fdd --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,46 @@ +name: Create and publish Docker image +on: + push: + branches: + - 'master' + tags: + - '*' + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +jobs: + build-and-push-image: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + + steps: + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Log in to the Container registry + uses: docker/login-action@v1 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v3 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + + - name: Build and push Docker image + uses: docker/build-push-action@v2 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6a0ba60 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +# syntax=docker/dockerfile:1.3 +FROM node:16-alpine AS builder +COPY --chown=1000:1000 . /fredy +WORKDIR /fredy +USER 1000 +RUN yarn install +RUN yarn run prod + +FROM node:16-alpine +COPY --from=builder --chown=1000:1000 /fredy /fredy +RUN mkdir /db /conf && \ + chown 1000:1000 /db /conf && \ + ln -s /db /fredy/db && ln -s /conf /fredy/conf +EXPOSE 9998 +USER 1000 +VOLUME [ "/conf", "/db" ] +WORKDIR /fredy +CMD node index.js --no-daemon diff --git a/build/Dockerfile b/build/Dockerfile deleted file mode 100644 index f724520..0000000 --- a/build/Dockerfile +++ /dev/null @@ -1,33 +0,0 @@ -FROM alpine:latest AS build -# use given repository, default below: -ARG repo=https://github.com/orangecoding/fredy.git - -RUN mkdir -p /usr/src/ -#Install Software -RUN apk add --update nodejs npm git - -# Output used repository -RUN echo "Cloning from $repo" - -RUN cd /usr/src && git clone $repo - -RUN ln -s /usr/src/fredy/conf/ /conf - -# create db folder -RUN mkdir /usr/src/fredy/db/ - -RUN ln -s /usr/src/fredy/db/ /db - -RUN npm i -g yarn - -RUN cd /usr/src/fredy/ && yarn - -WORKDIR /usr/src/fredy - -RUN yarn run prod - -EXPOSE 9998 - -VOLUME [ "/conf", "/db" ] -# --no-daemon is required for keeping Container alive -CMD node index.js --no-daemon diff --git a/docker-compose.yml b/docker-compose.yml index 91d2051..80da407 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,10 +4,8 @@ services: container_name: fredy # build from empty build folder to reduce size of image build: - context: ./build + context: . dockerfile: Dockerfile - args: - repo: https://github.com/orangecoding/fredy.git image: fredy/fredy # map existing config and database volumes: