From 2e1d8958861c06a35da4c4bd0b7f178b1995d89c Mon Sep 17 00:00:00 2001 From: Chris Giglio Date: Wed, 15 May 2024 12:31:00 -0500 Subject: [PATCH 1/6] add docker files --- .dockerignore | 5 +++ .github/workflows/docker-image-publish.yml | 48 ++++++++++++++++++++++ Dockerfile | 20 +++++++++ docker-compose.yaml | 41 ++++++++++++++++++ package.json | 6 ++- 5 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 .dockerignore create mode 100644 .github/workflows/docker-image-publish.yml create mode 100644 Dockerfile create mode 100644 docker-compose.yaml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..2ac4c6b --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +node_modules +npm-debug.log +docker-compose.yml +.DS_Store +.git \ No newline at end of file diff --git a/.github/workflows/docker-image-publish.yml b/.github/workflows/docker-image-publish.yml new file mode 100644 index 0000000..e7d0d30 --- /dev/null +++ b/.github/workflows/docker-image-publish.yml @@ -0,0 +1,48 @@ +name: Build & Publish Docker Image + +on: + push: + branches: + - main + pull_request: + branches: + - main + +env: + REGISTRY: ghcr.io + + +jobs: + build-and-push-image: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + + strategy: + fail-fast: false + matrix: + include: + - dockerfile: ./Apps.Dockerfile + target: tbd-pfi-exemplar + image: ghcr.io/tbd54566975/tbd-developer-pfi-exemplar + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Log in to the Container registry + uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Build and push Docker image + uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc + with: + context: . + file: Apps.Dockerfile + push: true + target: ${{ matrix.target }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0f09d2e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +# Use the official Node.js image as the base image +FROM node:lts as base + +# Set the working directory inside the container +WORKDIR /home/node/app + +# Copy package.json and package-lock.json to the working directory +COPY --chown=node:node . /home/node/app/ + +# Install the application dependencies +RUN npm install - g pnpm +RUN pnpm install +RUN pnpm build + +# Download and install dbmate +RUN curl -fsSL https://github.com/amacneil/dbmate/releases/download/v1.12.1/dbmate-linux-amd64 -o dbmate \ + && chmod +x dbmate \ + && mv dbmate /usr/local/bin + +COPY --chown=node:node --from=base /home/node/app/site/build /usr/share/nginx/html/ \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..aa6d8ed --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,41 @@ +version: '3.8' + +services: + postgres: + image: postgres:latest + environment: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: tbd + POSTGRES_DB: mockpfi + ports: + - "5432:5432" + healthcheck: + test: ["CMD-SHELL", "pg_isready"] + interval: 10s + timeout: 5s + retries: 5 + + node: + image: node:20.4.0 + working_dir: /app + volumes: + - .:/app + depends_on: + postgres: + condition: service_healthy + environment: + DATABASE_URL: postgres://postgres:tbd@postgres:5432/mockpfi + command: > + sh -c " + npm install && + curl -fsSL https://github.com/amacneil/dbmate/releases/download/v1.12.1/dbmate-linux-amd64 -o dbmate && + chmod +x dbmate && + mv dbmate /usr/local/bin && + ./db/scripts/migrate && + npm run seed-offerings && + npm run server & + SERVER_PID=$! && + sleep 5 && + npm run test && + kill $SERVER_PID + " diff --git a/package.json b/package.json index 6437925..0328d8a 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,10 @@ "seed-offerings": "npm run _start -- dist/seed-offerings.js", "server": "npm run _start -- dist/main.js", "test": "rimraf dist/tests && npm run compile && node --test", - "try": "npm run _debug -- dist/client-test.js" + "try": "npm run _debug -- dist/client-test.js", + "docker:up": "docker-compose up -d", + "docker:down": "docker-compose down", + "docker:logs": "docker-compose logs -f", + "docker:build": "docker-compose build" } } From c8c91c67f750227df73142de004179526452b2ef Mon Sep 17 00:00:00 2001 From: Chris Giglio Date: Thu, 16 May 2024 14:42:56 -0500 Subject: [PATCH 2/6] Address comments --- .github/workflows/docker-image-publish.yml | 5 ++--- Dockerfile | 5 ++--- package.json | 3 ++- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/.github/workflows/docker-image-publish.yml b/.github/workflows/docker-image-publish.yml index e7d0d30..0448a93 100644 --- a/.github/workflows/docker-image-publish.yml +++ b/.github/workflows/docker-image-publish.yml @@ -23,8 +23,7 @@ jobs: fail-fast: false matrix: include: - - dockerfile: ./Apps.Dockerfile - target: tbd-pfi-exemplar + - dockerfile: ./.Dockerfile image: ghcr.io/tbd54566975/tbd-developer-pfi-exemplar steps: @@ -42,7 +41,7 @@ jobs: uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc with: context: . - file: Apps.Dockerfile + file: .Dockerfile push: true target: ${{ matrix.target }} labels: ${{ steps.meta.outputs.labels }} diff --git a/Dockerfile b/Dockerfile index 0f09d2e..63c6c00 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,9 +8,8 @@ WORKDIR /home/node/app COPY --chown=node:node . /home/node/app/ # Install the application dependencies -RUN npm install - g pnpm -RUN pnpm install -RUN pnpm build +RUN npm i +RUN npm run build # Download and install dbmate RUN curl -fsSL https://github.com/amacneil/dbmate/releases/download/v1.12.1/dbmate-linux-amd64 -o dbmate \ diff --git a/package.json b/package.json index 0328d8a..cbf8027 100644 --- a/package.json +++ b/package.json @@ -62,6 +62,7 @@ "docker:up": "docker-compose up -d", "docker:down": "docker-compose down", "docker:logs": "docker-compose logs -f", - "docker:build": "docker-compose build" + "docker:build": "docker-compose build", + "docker:down-wipe": "docker-compose down -v" } } From e4e192936ef9ee492ef77b1e55cbdce5dbc07e2d Mon Sep 17 00:00:00 2001 From: Chris Giglio Date: Tue, 21 May 2024 15:37:29 -0500 Subject: [PATCH 3/6] Changes with Leo --- .github/workflows/docker-image-publish.yml | 4 +- Dockerfile | 7 +++- docker-compose.yaml | 44 +++++++++++----------- 3 files changed, 29 insertions(+), 26 deletions(-) diff --git a/.github/workflows/docker-image-publish.yml b/.github/workflows/docker-image-publish.yml index 0448a93..dec163d 100644 --- a/.github/workflows/docker-image-publish.yml +++ b/.github/workflows/docker-image-publish.yml @@ -23,7 +23,7 @@ jobs: fail-fast: false matrix: include: - - dockerfile: ./.Dockerfile + - dockerfile: ./Dockerfile image: ghcr.io/tbd54566975/tbd-developer-pfi-exemplar steps: @@ -41,7 +41,7 @@ jobs: uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc with: context: . - file: .Dockerfile + file: Dockerfile push: true target: ${{ matrix.target }} labels: ${{ steps.meta.outputs.labels }} diff --git a/Dockerfile b/Dockerfile index 63c6c00..38ffa95 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,4 +16,9 @@ RUN curl -fsSL https://github.com/amacneil/dbmate/releases/download/v1.12.1/dbma && chmod +x dbmate \ && mv dbmate /usr/local/bin -COPY --chown=node:node --from=base /home/node/app/site/build /usr/share/nginx/html/ \ No newline at end of file +# TODO: maybe remove +# COPY --chown=node:node --from=base /home/node/app/site/build /usr/share/nginx/html/ + +WORKDIR /home/node/app/site/build +EXPOSE 9000 +CMD [ "npm", "server" ] \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index aa6d8ed..7763d8f 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -15,27 +15,25 @@ services: timeout: 5s retries: 5 - node: - image: node:20.4.0 - working_dir: /app - volumes: - - .:/app - depends_on: - postgres: - condition: service_healthy + pfi-app: + build: + context: . + dockerfile: Dockerfile + ports: + - '9000:9000' environment: - DATABASE_URL: postgres://postgres:tbd@postgres:5432/mockpfi - command: > - sh -c " - npm install && - curl -fsSL https://github.com/amacneil/dbmate/releases/download/v1.12.1/dbmate-linux-amd64 -o dbmate && - chmod +x dbmate && - mv dbmate /usr/local/bin && - ./db/scripts/migrate && - npm run seed-offerings && - npm run server & - SERVER_PID=$! && - sleep 5 && - npm run test && - kill $SERVER_PID - " + NODE_ENV: production + # environment info + ENV: production + LOG_LEVEL: info + HOST: localhost + PORT: 9000 + + # DB info + SEC_DB_HOST: localhost + SEC_DB_PORT: 5432 + SEC_DB_USER: postgres + SEC_DB_PASSWORD: tbd + SEC_DB_NAME: mockpfi + depends_on: + - postgresdb From 591ccb0ab2087f229275aa6cdb8b175b541942bd Mon Sep 17 00:00:00 2001 From: Chris Giglio Date: Tue, 21 May 2024 15:55:59 -0500 Subject: [PATCH 4/6] Get running locally --- Dockerfile | 9 ++------- docker-compose.yaml | 16 ++++++++-------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/Dockerfile b/Dockerfile index 38ffa95..5eed53c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # Use the official Node.js image as the base image -FROM node:lts as base +FROM node:lts # Set the working directory inside the container WORKDIR /home/node/app @@ -9,16 +9,11 @@ COPY --chown=node:node . /home/node/app/ # Install the application dependencies RUN npm i -RUN npm run build # Download and install dbmate RUN curl -fsSL https://github.com/amacneil/dbmate/releases/download/v1.12.1/dbmate-linux-amd64 -o dbmate \ && chmod +x dbmate \ && mv dbmate /usr/local/bin -# TODO: maybe remove -# COPY --chown=node:node --from=base /home/node/app/site/build /usr/share/nginx/html/ - -WORKDIR /home/node/app/site/build EXPOSE 9000 -CMD [ "npm", "server" ] \ No newline at end of file +CMD [ "npm", "run", "server" ] \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index 7763d8f..4386778 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,7 +1,7 @@ -version: '3.8' +version: "3.8" services: - postgres: + postgresdb: image: postgres:latest environment: POSTGRES_USER: postgres @@ -10,9 +10,9 @@ services: ports: - "5432:5432" healthcheck: - test: ["CMD-SHELL", "pg_isready"] - interval: 10s - timeout: 5s + test: ["CMD-SHELL", "postgres", "--health-cmd", "pg_isready"] + interval: 2s + timeout: 1s retries: 5 pfi-app: @@ -20,7 +20,7 @@ services: context: . dockerfile: Dockerfile ports: - - '9000:9000' + - "9000:9000" environment: NODE_ENV: production # environment info @@ -28,7 +28,7 @@ services: LOG_LEVEL: info HOST: localhost PORT: 9000 - + # DB info SEC_DB_HOST: localhost SEC_DB_PORT: 5432 @@ -36,4 +36,4 @@ services: SEC_DB_PASSWORD: tbd SEC_DB_NAME: mockpfi depends_on: - - postgresdb + - postgresdb \ No newline at end of file From d00b364b216eee32a6f2841d2d8575371ba16b30 Mon Sep 17 00:00:00 2001 From: Chris Giglio Date: Tue, 21 May 2024 15:59:09 -0500 Subject: [PATCH 5/6] Fix publish step --- .github/workflows/docker-image-publish.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/docker-image-publish.yml b/.github/workflows/docker-image-publish.yml index dec163d..0fc08cd 100644 --- a/.github/workflows/docker-image-publish.yml +++ b/.github/workflows/docker-image-publish.yml @@ -37,6 +37,12 @@ jobs: username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38 + with: + images: ${{ matrix.image }} + - name: Build and push Docker image uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc with: From c801d34342d8e3a193376ff66b350f20af5d29eb Mon Sep 17 00:00:00 2001 From: Chris Giglio Date: Tue, 21 May 2024 20:47:13 -0500 Subject: [PATCH 6/6] Try to add tags back --- .github/workflows/docker-image-publish.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/docker-image-publish.yml b/.github/workflows/docker-image-publish.yml index 0fc08cd..4dea682 100644 --- a/.github/workflows/docker-image-publish.yml +++ b/.github/workflows/docker-image-publish.yml @@ -50,4 +50,5 @@ jobs: file: Dockerfile push: true target: ${{ matrix.target }} + tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }}