From 52d92de8fa24275a753e1ece80a890807d592982 Mon Sep 17 00:00:00 2001 From: mroxso Date: Sun, 5 Nov 2023 11:12:56 +0100 Subject: [PATCH 1/3] initial dockerize (wip) --- .dockerignore | 34 ++++++++++++++++++++++++++++++++++ Dockerfile | 36 ++++++++++++++++++++++++++++++++++++ README.md | 11 +++++++++++ compose.yaml | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 132 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 compose.yaml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..3d24738 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,34 @@ +# Include any files or directories that you don't want to be copied to your +# container here (e.g., local build artifacts, temporary files, etc.). +# +# For more help, visit the .dockerignore file reference guide at +# https://docs.docker.com/engine/reference/builder/#dockerignore-file + +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/.next +**/.cache +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/charts +**/docker-compose* +**/compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +**/build +**/dist +LICENSE +README.md diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1240677 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,36 @@ +# syntax=docker/dockerfile:1 + +# Comments are provided throughout this file to help you get started. +# If you need more help, visit the Dockerfile reference guide at +# https://docs.docker.com/engine/reference/builder/ + +ARG NODE_VERSION=21.1.0 + +FROM node:${NODE_VERSION} + +# Use production node environment by default. +ENV NODE_ENV production + + +WORKDIR /usr/src/app + +# Download dependencies as a separate step to take advantage of Docker's caching. +# Leverage a cache mount to /root/.npm to speed up subsequent builds. +# Leverage a bind mounts to package.json and package-lock.json to avoid having to copy them into +# into this layer. +RUN --mount=type=bind,source=package.json,target=package.json \ + --mount=type=bind,source=package-lock.json,target=package-lock.json \ + --mount=type=cache,target=/root/.npm \ + npm ci --omit=dev + +# Run the application as a non-root user. +USER node + +# Copy the rest of the source files into the image. +COPY . . + +# Expose the port that the application listens on. +EXPOSE 8080 + +# Run the application. +CMD node index.js diff --git a/README.md b/README.md index f091b07..55e145a 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,17 @@ tmux new -d "node index.js" - `IN_MEMORY` - Store temporary data in memory (RAM) instead of disk. - `LOG_ABOUT_RELAYS` - Whenever to log about relay connections +## Docker +### Quick Run +``` +git clone https://github.com/mroxso/bostr +cd bostr +npm i +cp config.js.example config.js +docker build -t bostr:local +docker run --rm --name bostr -p 8080:8080 bostr:local +``` + ## License Copyright 2023 Yonle diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..28d6f83 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,51 @@ +# Comments are provided throughout this file to help you get started. +# If you need more help, visit the Docker compose reference guide at +# https://docs.docker.com/compose/compose-file/ + +# Here the instructions define your application as a service called "server". +# This service is built from the Dockerfile in the current directory. +# You can add other services your application may depend on here, such as a +# database or a cache. For examples, see the Awesome Compose repository: +# https://github.com/docker/awesome-compose +services: + server: + build: + context: . + environment: + NODE_ENV: production + ports: + - 8080:8080 + +# The commented out section below is an example of how to define a PostgreSQL +# database that your application can use. `depends_on` tells Docker Compose to +# start the database before your application. The `db-data` volume persists the +# database data between container restarts. The `db-password` secret is used +# to set the database password. You must create `db/password.txt` and add +# a password of your choosing to it before running `docker-compose up`. +# depends_on: +# db: +# condition: service_healthy +# db: +# image: postgres +# restart: always +# user: postgres +# secrets: +# - db-password +# volumes: +# - db-data:/var/lib/postgresql/data +# environment: +# - POSTGRES_DB=example +# - POSTGRES_PASSWORD_FILE=/run/secrets/db-password +# expose: +# - 5432 +# healthcheck: +# test: [ "CMD", "pg_isready" ] +# interval: 10s +# timeout: 5s +# retries: 5 +# volumes: +# db-data: +# secrets: +# db-password: +# file: db/password.txt + From 685e80e639dbf96527db9de8e03b1b197e5498d9 Mon Sep 17 00:00:00 2001 From: mroxso Date: Sun, 5 Nov 2023 20:37:54 +0100 Subject: [PATCH 2/3] fix dockerfile and use external config.js --- Dockerfile | 4 ++-- README.md | 6 ++---- compose.yaml | 2 ++ 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index 1240677..7086bf6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,9 +19,9 @@ WORKDIR /usr/src/app # Leverage a bind mounts to package.json and package-lock.json to avoid having to copy them into # into this layer. RUN --mount=type=bind,source=package.json,target=package.json \ - --mount=type=bind,source=package-lock.json,target=package-lock.json \ + # --mount=type=bind,source=package-lock.json,target=package-lock.json \ --mount=type=cache,target=/root/.npm \ - npm ci --omit=dev + npm i --omit=dev # Run the application as a non-root user. USER node diff --git a/README.md b/README.md index 55e145a..f6185d0 100644 --- a/README.md +++ b/README.md @@ -48,10 +48,8 @@ tmux new -d "node index.js" ``` git clone https://github.com/mroxso/bostr cd bostr -npm i -cp config.js.example config.js -docker build -t bostr:local -docker run --rm --name bostr -p 8080:8080 bostr:local +docker build -t bostr:local . +docker run --rm --name bostr -p 8080:8080 -v ./config.js:/usr/src/app/config.js bostr:local ``` ## License diff --git a/compose.yaml b/compose.yaml index 28d6f83..c35b46d 100644 --- a/compose.yaml +++ b/compose.yaml @@ -13,6 +13,8 @@ services: context: . environment: NODE_ENV: production + volumes: + - ./config.js:/usr/src/app/config.js ports: - 8080:8080 From 6d831f7ba59b37f755d6f25325171087ea721c6d Mon Sep 17 00:00:00 2001 From: mroxso Date: Sun, 5 Nov 2023 20:51:38 +0100 Subject: [PATCH 3/3] add github docker build and push workflow --- .github/workflows/docker.yaml | 59 +++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 .github/workflows/docker.yaml diff --git a/.github/workflows/docker.yaml b/.github/workflows/docker.yaml new file mode 100644 index 0000000..cbdb3ec --- /dev/null +++ b/.github/workflows/docker.yaml @@ -0,0 +1,59 @@ +name: Docker Build and Push + +on: + workflow_dispatch: + push: + branches: + - master + +env: + REGISTRY_NAME: ghcr.io + IMAGE_NAME: bostr + +jobs: + # ci: + # name: CI + # uses: ./.github/workflows/ci.yml + + build_and_push: + # needs: [ci] + name: Build and Push + runs-on: ubuntu-latest + steps: + - name: Check out the repo + 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: Login to GitHub Container Registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Collecting Metadata + id: meta + uses: docker/metadata-action@v4 + with: + images: ${{ env.REGISTRY_NAME }}/${{ github.repository_owner }}/${{ env.IMAGE_NAME }} + tags: | + type=raw,value=latest,enable={{is_default_branch}} + + - name: Building And Pushing Image + id: docker_build + uses: docker/build-push-action@v4 + with: + context: . + file: ./Dockerfile + push: true + platforms: linux/amd64,linux/arm64 + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + + - name: Image digest + run: echo ${{ steps.docker_build.outputs.digest }} \ No newline at end of file