From bee98cef8bbf0a1cf77b1150f409f07930e6b1d6 Mon Sep 17 00:00:00 2001 From: Augusto Date: Fri, 1 Mar 2024 18:50:06 +0100 Subject: [PATCH 1/3] Solving problems with connectivity Passing the HOST IP to the different containers through a env variable. Several changes in the docker-compose to avoid replace the wiq_0 word Changed README to add the HOST_IP as env. variable in the AZ VM --- .env | 3 ++- .github/workflows/build.yml | 10 +++++----- .github/workflows/release.yml | 12 +++++++----- README.md | 13 +++++++++++++ docker-compose.yml | 18 ++++++++++-------- gatewayservice/.env | 2 ++ gatewayservice/Dockerfile | 3 +++ users/authservice/.env | 1 + users/authservice/Dockerfile | 3 +++ users/userservice/.env | 1 + users/userservice/Dockerfile | 3 +++ webapp/Dockerfile | 6 ++---- webapp/src/components/AddUser.js | 2 +- webapp/src/components/Login.js | 2 +- 14 files changed, 54 insertions(+), 25 deletions(-) create mode 100644 gatewayservice/.env create mode 100644 users/authservice/.env create mode 100644 users/userservice/.env diff --git a/.env b/.env index 131b17e4..7a9ba72a 100644 --- a/.env +++ b/.env @@ -1 +1,2 @@ -teamname="wiq_0" \ No newline at end of file +teamname="wiq_0" +WIQ_EXTERNAL_DNS_NAME_OR_IP=$DOCKER_HOST_IP \ No newline at end of file diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c9265703..3378fda8 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -23,8 +23,8 @@ jobs: - run: npm --prefix users/userservice test -- --coverage - run: npm --prefix gatewayservice test -- --coverage - run: npm --prefix webapp test -- --coverage - - name: Analyze with SonarCloud - uses: sonarsource/sonarcloud-github-action@master - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} \ No newline at end of file +# - name: Analyze with SonarCloud +# uses: sonarsource/sonarcloud-github-action@master +# env: +# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} +# SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 2905c196..b6efb135 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -3,6 +3,8 @@ name: Deploy on release on: release: types: [published] + pull_request: + types: [opened, synchronize, reopened] jobs: unit-tests: @@ -20,11 +22,11 @@ jobs: - run: npm --prefix users/userservice test -- --coverage - run: npm --prefix gatewayservice test -- --coverage - run: npm --prefix webapp test -- --coverage - - name: Analyze with SonarCloud - uses: sonarsource/sonarcloud-github-action@master - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} +# - name: Analyze with SonarCloud +# uses: sonarsource/sonarcloud-github-action@master +# env: +# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} +# SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} e2e-tests: needs: [unit-tests] runs-on: ubuntu-latest diff --git a/README.md b/README.md index fb89a4a1..75939c09 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,19 @@ sudo curl -L "https://github.com/docker/compose/releases/download/1.28.5/docker- sudo chmod +x /usr/local/bin/docker-compose ``` +After installing Docker, we need to set up a global environment variable to configure external container connectivity. This can be done by editing the /etc/environment file: + +```bash +sudo nano /etc/environment +``` + +and adding the following line: + +```bash +DOCKER_HOST_IP=[IP address of the remote machine] +``` + + ### Continuous delivery (GitHub Actions) Once we have our machine ready, we could deploy by hand the application, taking our docker-compose file and executing it in the remote machine. diff --git a/docker-compose.yml b/docker-compose.yml index c105ed50..2cb1ed54 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,7 +13,7 @@ services: authservice: container_name: authservice-${teamname:-defaultASW} - image: ghcr.io/arquisoft/wiq_0/authservice:latest + image: ghcr.io/arquisoft/${teamname:-wiq_0}/authservice:latest profiles: ["dev", "prod"] build: ./users/authservice depends_on: @@ -23,11 +23,11 @@ services: networks: - mynetwork environment: - MONGODB_URI: mongodb://mongodb:27017/userdb + MONGODB_URI: mongodb://${WIQ_EXTERNAL_DNS_NAME_OR_IP:-mongodb}:27017/userdb userservice: container_name: userservice-${teamname:-defaultASW} - image: ghcr.io/arquisoft/wiq_0/userservice:latest + image: ghcr.io/arquisoft/${teamname:-wiq_0}/userservice:latest profiles: ["dev", "prod"] build: ./users/userservice depends_on: @@ -37,11 +37,11 @@ services: networks: - mynetwork environment: - MONGODB_URI: mongodb://mongodb:27017/userdb + MONGODB_URI: mongodb://${WIQ_EXTERNAL_DNS_NAME_OR_IP:-mongodb}:27017/userdb gatewayservice: container_name: gatewayservice-${teamname:-defaultASW} - image: ghcr.io/arquisoft/wiq_0/gatewayservice:latest + image: ghcr.io/arquisoft/${teamname:-wiq_0}/gatewayservice:latest profiles: ["dev", "prod"] build: ./gatewayservice depends_on: @@ -53,18 +53,20 @@ services: networks: - mynetwork environment: - AUTH_SERVICE_URL: http://authservice:8002 - USER_SERVICE_URL: http://userservice:8001 + AUTH_SERVICE_URL: http://${WIQ_EXTERNAL_DNS_NAME_OR_IP:-authservice}:8002 + USER_SERVICE_URL: http://${WIQ_EXTERNAL_DNS_NAME_OR_IP:-userservice}:8001 webapp: container_name: webapp-${teamname:-defaultASW} - image: ghcr.io/arquisoft/wiq_0/webapp:latest + image: ghcr.io/arquisoft/${teamname:-wiq_0}/webapp:latest profiles: ["dev", "prod"] build: ./webapp depends_on: - gatewayservice ports: - "3000:3000" + environment: + REACT_APP_API_ENDPOINT: http://${WIQ_EXTERNAL_DNS_NAME_OR_IP:-localhost}:8000 prometheus: image: prom/prometheus diff --git a/gatewayservice/.env b/gatewayservice/.env new file mode 100644 index 00000000..76b9ce5e --- /dev/null +++ b/gatewayservice/.env @@ -0,0 +1,2 @@ +AUTH_SERVICE_URL=http://localhost:8002 +USER_SERVICE_URL=http://localhost:8001 \ No newline at end of file diff --git a/gatewayservice/Dockerfile b/gatewayservice/Dockerfile index 6c340432..20a60150 100644 --- a/gatewayservice/Dockerfile +++ b/gatewayservice/Dockerfile @@ -13,5 +13,8 @@ RUN npm install # Copy the app source code to the working directory COPY . . +#Remove env file (not necesary for docker) +RUN rm -f .env + # Define the command to run your app CMD ["node", "gateway-service.js"] diff --git a/users/authservice/.env b/users/authservice/.env new file mode 100644 index 00000000..b723d16d --- /dev/null +++ b/users/authservice/.env @@ -0,0 +1 @@ +MONGODB_URI=mongodb://localhost:27017/userdb \ No newline at end of file diff --git a/users/authservice/Dockerfile b/users/authservice/Dockerfile index 70648d70..2b6bf142 100644 --- a/users/authservice/Dockerfile +++ b/users/authservice/Dockerfile @@ -13,6 +13,9 @@ RUN npm install # Copy the app source code to the working directory COPY . . +#Remove env file (not necesary for docker) +RUN rm -f .env + # Expose the port the app runs on EXPOSE 8002 diff --git a/users/userservice/.env b/users/userservice/.env new file mode 100644 index 00000000..b723d16d --- /dev/null +++ b/users/userservice/.env @@ -0,0 +1 @@ +MONGODB_URI=mongodb://localhost:27017/userdb \ No newline at end of file diff --git a/users/userservice/Dockerfile b/users/userservice/Dockerfile index f43e2df7..0423a6ea 100644 --- a/users/userservice/Dockerfile +++ b/users/userservice/Dockerfile @@ -13,6 +13,9 @@ RUN npm install # Copy the app source code to the working directory COPY . . +#Remove env file (not necesary for docker) +RUN rm -f .env + # Expose the port the app runs on EXPOSE 8001 diff --git a/webapp/Dockerfile b/webapp/Dockerfile index 3cbad8b7..661282ee 100644 --- a/webapp/Dockerfile +++ b/webapp/Dockerfile @@ -2,13 +2,11 @@ FROM node:20 COPY . /app WORKDIR /app - +#Remove env file (not necesary for docker) +RUN rm -f .env #Install the dependencies RUN npm install -ARG API_URI="http://localhost:8000" -ENV REACT_APP_API_ENDPOINT=$API_URI - #Create an optimized version of the webapp RUN npm run build RUN npm install serve diff --git a/webapp/src/components/AddUser.js b/webapp/src/components/AddUser.js index 00d522a2..ddbcb070 100644 --- a/webapp/src/components/AddUser.js +++ b/webapp/src/components/AddUser.js @@ -3,7 +3,7 @@ import React, { useState } from 'react'; import axios from 'axios'; import { Container, Typography, TextField, Button, Snackbar } from '@mui/material'; -const apiEndpoint = process.env.REACT_APP_API_ENDPOINT || 'http://localhost:8000'; +const apiEndpoint = process.env.REACT_APP_API_ENDPOINT; const AddUser = () => { const [username, setUsername] = useState(''); diff --git a/webapp/src/components/Login.js b/webapp/src/components/Login.js index 0ad6268e..4397a52b 100644 --- a/webapp/src/components/Login.js +++ b/webapp/src/components/Login.js @@ -11,7 +11,7 @@ const Login = () => { const [createdAt, setCreatedAt] = useState(''); const [openSnackbar, setOpenSnackbar] = useState(false); - const apiEndpoint = process.env.REACT_APP_API_ENDPOINT || 'http://localhost:8000'; + const apiEndpoint = process.env.REACT_APP_API_ENDPOINT; const loginUser = async () => { try { From 86e7521f7b38943b443b00ef9934593cb0e03d03 Mon Sep 17 00:00:00 2001 From: Augusto Date: Sat, 2 Mar 2024 15:36:37 +0100 Subject: [PATCH 2/3] Undo docker mongo name --- docker-compose.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 2cb1ed54..32ded6cf 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -23,7 +23,7 @@ services: networks: - mynetwork environment: - MONGODB_URI: mongodb://${WIQ_EXTERNAL_DNS_NAME_OR_IP:-mongodb}:27017/userdb + MONGODB_URI: mongodb://mongodb:27017/userdb userservice: container_name: userservice-${teamname:-defaultASW} @@ -37,7 +37,7 @@ services: networks: - mynetwork environment: - MONGODB_URI: mongodb://${WIQ_EXTERNAL_DNS_NAME_OR_IP:-mongodb}:27017/userdb + MONGODB_URI: mongodb://mongodb:27017/userdb gatewayservice: container_name: gatewayservice-${teamname:-defaultASW} From 28caf395c6c4e9b897ca5902debf8d1c2ab2b0e5 Mon Sep 17 00:00:00 2001 From: Augusto Date: Sat, 2 Mar 2024 16:00:36 +0100 Subject: [PATCH 3/3] Removing unnecesary external IPs and undo git flows to previous state --- .github/workflows/build.yml | 10 +++++----- .github/workflows/release.yml | 12 +++++------- docker-compose.yml | 4 ++-- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 3378fda8..c9265703 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -23,8 +23,8 @@ jobs: - run: npm --prefix users/userservice test -- --coverage - run: npm --prefix gatewayservice test -- --coverage - run: npm --prefix webapp test -- --coverage -# - name: Analyze with SonarCloud -# uses: sonarsource/sonarcloud-github-action@master -# env: -# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} -# SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} \ No newline at end of file + - name: Analyze with SonarCloud + uses: sonarsource/sonarcloud-github-action@master + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index b6efb135..2905c196 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -3,8 +3,6 @@ name: Deploy on release on: release: types: [published] - pull_request: - types: [opened, synchronize, reopened] jobs: unit-tests: @@ -22,11 +20,11 @@ jobs: - run: npm --prefix users/userservice test -- --coverage - run: npm --prefix gatewayservice test -- --coverage - run: npm --prefix webapp test -- --coverage -# - name: Analyze with SonarCloud -# uses: sonarsource/sonarcloud-github-action@master -# env: -# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} -# SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + - name: Analyze with SonarCloud + uses: sonarsource/sonarcloud-github-action@master + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} e2e-tests: needs: [unit-tests] runs-on: ubuntu-latest diff --git a/docker-compose.yml b/docker-compose.yml index 32ded6cf..2c7b5b42 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -53,8 +53,8 @@ services: networks: - mynetwork environment: - AUTH_SERVICE_URL: http://${WIQ_EXTERNAL_DNS_NAME_OR_IP:-authservice}:8002 - USER_SERVICE_URL: http://${WIQ_EXTERNAL_DNS_NAME_OR_IP:-userservice}:8001 + AUTH_SERVICE_URL: http://authservice:8002 + USER_SERVICE_URL: http://userservice:8001 webapp: container_name: webapp-${teamname:-defaultASW}