From 6da01a0465c1c064ac125b66264be0750aebbdee Mon Sep 17 00:00:00 2001 From: rajneeshmehta Date: Mon, 18 Oct 2021 07:35:38 +0000 Subject: [PATCH 01/11] add Dockerfile and docker-compose file --- Dockerfile | 10 ++++++++++ docker-compose.yml | 19 +++++++++++++++++++ src/main/resources/application.properties | 6 +++--- 3 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..08d97aa --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM maven:3.8.3-jdk-11 AS maven_build +WORKDIR /tmp +COPY pom.xml . +COPY src/ ./src/ +RUN mvn package -Dmaven.test.skip=true + +FROM openjdk:11 +COPY --from=maven_build /tmp/target/easy-notes-1.0.0.jar /data/easy-notes-1.0.0.jar +EXPOSE 8080 +CMD java -jar /data/easy-notes-1.0.0.jar \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..03c2659 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,19 @@ +version: "3" +services: + app: + image: spring-boot:latest + restart: always + ports: + - 8080:8080 + depends_on: + - db + db: + image: mysql + restart: always + expose: + - 3306 + environment: + - MYSQL_ROOT_PASSWORD=supersecretpassword + - MYSQL_DATABASE=notes_app + - MYSQL_USER=app + - MYSQL_PASSWORD=secretpassword diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index d357d46..68bc8a4 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,7 +1,7 @@ ## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties) -spring.datasource.url = jdbc:mysql://localhost:3306/notes_app?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false -spring.datasource.username = root -spring.datasource.password = callicoder +spring.datasource.url = jdbc:mysql://db:3306/notes_app?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false +spring.datasource.username = app +spring.datasource.password = secretpassword ## Hibernate Properties From af112f118996141b35a4407054fe2971d46d0e1e Mon Sep 17 00:00:00 2001 From: rajneeshmehta Date: Mon, 18 Oct 2021 07:49:23 +0000 Subject: [PATCH 02/11] add Docker Image CI for spring-boot application --- .github/workflows/docker-image.yml | 47 ++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 .github/workflows/docker-image.yml diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml new file mode 100644 index 0000000..2b4fa78 --- /dev/null +++ b/.github/workflows/docker-image.yml @@ -0,0 +1,47 @@ +name: spring-boot Docker Image CI +on: + push: + branches: [ master ] + workflow_dispatch: + + +jobs: + docker: + runs-on: ubuntu-latest + steps: + - + name: Set up QEMU + uses: docker/setup-qemu-action@v1 + - + name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + - + name: Login to DockerHub + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - + name: Build and push + id: docker_build + uses: docker/build-push-action@v2 + with: + platforms: linux/amd64 + push: true + file: Dockerfile + tags: ${{ secrets.DOCKERHUB_USERNAME }}/spring-boot:prod-v${{ github.run_number }} + - + name: Image digest + run: echo ${{ steps.docker_build.outputs.digest }} + +# - name: Download new image and run new containers +# uses: JimCronqvist/action-ssh@master +# with: +# hosts: 'ubuntu@ec2-52-207-24-140.compute-1.amazonaws.com' +# privateKey: ${{ secrets.SSH_PRIVATE_KEY }} +# debug: false +# command: | +# sudo docker-compose -f /home/ubuntu/django-demo/docker-compose.yml down +# sudo docker-compose -f /home/ubuntu/django-demo/docker-compose.yml pull +# sudo docker image prune -f +# sudo docker-compose -f /home/ubuntu/django-demo/docker-compose.yml up -d \ No newline at end of file From db95ac2510535c96c21d205f119391d42ba55bd2 Mon Sep 17 00:00:00 2001 From: rajneeshmehta Date: Mon, 18 Oct 2021 07:50:52 +0000 Subject: [PATCH 03/11] remove docker image ci --- .github/workflows/docker-image.yml | 47 ------------------------------ 1 file changed, 47 deletions(-) delete mode 100644 .github/workflows/docker-image.yml diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml deleted file mode 100644 index 2b4fa78..0000000 --- a/.github/workflows/docker-image.yml +++ /dev/null @@ -1,47 +0,0 @@ -name: spring-boot Docker Image CI -on: - push: - branches: [ master ] - workflow_dispatch: - - -jobs: - docker: - runs-on: ubuntu-latest - steps: - - - name: Set up QEMU - uses: docker/setup-qemu-action@v1 - - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 - - - name: Login to DockerHub - uses: docker/login-action@v1 - with: - username: ${{ secrets.DOCKERHUB_USERNAME }} - password: ${{ secrets.DOCKERHUB_TOKEN }} - - - name: Build and push - id: docker_build - uses: docker/build-push-action@v2 - with: - platforms: linux/amd64 - push: true - file: Dockerfile - tags: ${{ secrets.DOCKERHUB_USERNAME }}/spring-boot:prod-v${{ github.run_number }} - - - name: Image digest - run: echo ${{ steps.docker_build.outputs.digest }} - -# - name: Download new image and run new containers -# uses: JimCronqvist/action-ssh@master -# with: -# hosts: 'ubuntu@ec2-52-207-24-140.compute-1.amazonaws.com' -# privateKey: ${{ secrets.SSH_PRIVATE_KEY }} -# debug: false -# command: | -# sudo docker-compose -f /home/ubuntu/django-demo/docker-compose.yml down -# sudo docker-compose -f /home/ubuntu/django-demo/docker-compose.yml pull -# sudo docker image prune -f -# sudo docker-compose -f /home/ubuntu/django-demo/docker-compose.yml up -d \ No newline at end of file From 5fc5bf3f4556881230b9c3b52a10fe845c9aca2e Mon Sep 17 00:00:00 2001 From: Rajneesh Mehta <44520236+rajneeshmehta@users.noreply.github.com> Date: Mon, 18 Oct 2021 13:21:55 +0530 Subject: [PATCH 04/11] Add Docker Image CI for spring-boot --- .github/workflows/Docker-Image CI.yml | 36 +++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 .github/workflows/Docker-Image CI.yml diff --git a/.github/workflows/Docker-Image CI.yml b/.github/workflows/Docker-Image CI.yml new file mode 100644 index 0000000..4080a2c --- /dev/null +++ b/.github/workflows/Docker-Image CI.yml @@ -0,0 +1,36 @@ +name: spring-boot Docker Image CI +on: + push: + branches: [ master ] + workflow_dispatch: + + +jobs: + docker: + runs-on: ubuntu-latest + steps: + - + name: Set up QEMU + uses: docker/setup-qemu-action@v1 + - + name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + - + name: Login to DockerHub + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - + name: Build and push + id: docker_build + uses: docker/build-push-action@v2 + with: + platforms: linux/amd64 + push: true + file: Dockerfile + tags: ${{ secrets.DOCKERHUB_USERNAME }}/spring-boot:prod-v${{ github.run_number }} + - + name: Image digest + run: echo ${{ steps.docker_build.outputs.digest }} + From 98892d3e5b1c39ac97f6f62eb657796955d1e8c9 Mon Sep 17 00:00:00 2001 From: rajneeshmehta Date: Mon, 18 Oct 2021 08:13:42 +0000 Subject: [PATCH 05/11] update code for ECS --- .github/workflows/Docker-Image CI.yml | 4 ++-- src/main/resources/application.properties | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/Docker-Image CI.yml b/.github/workflows/Docker-Image CI.yml index 4080a2c..5031fee 100644 --- a/.github/workflows/Docker-Image CI.yml +++ b/.github/workflows/Docker-Image CI.yml @@ -1,4 +1,4 @@ -name: spring-boot Docker Image CI +name: spring-boot Docker Image CI for ECS on: push: branches: [ master ] @@ -29,7 +29,7 @@ jobs: platforms: linux/amd64 push: true file: Dockerfile - tags: ${{ secrets.DOCKERHUB_USERNAME }}/spring-boot:prod-v${{ github.run_number }} + tags: ${{ secrets.DOCKERHUB_USERNAME }}/spring-boot:ECS-v${{ github.run_number }} - name: Image digest run: echo ${{ steps.docker_build.outputs.digest }} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 68bc8a4..c5992a2 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,5 +1,5 @@ ## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties) -spring.datasource.url = jdbc:mysql://db:3306/notes_app?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false +spring.datasource.url = jdbc:mysql://mysql-db.service.com:3306/notes_app?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false spring.datasource.username = app spring.datasource.password = secretpassword From 8d2fea44a88bed8afa37d03441219dbc431f1ddc Mon Sep 17 00:00:00 2001 From: Rajneesh Mehta <44520236+rajneeshmehta@users.noreply.github.com> Date: Mon, 18 Oct 2021 13:45:05 +0530 Subject: [PATCH 06/11] Update Docker-Image CI.yml --- .github/workflows/Docker-Image CI.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/Docker-Image CI.yml b/.github/workflows/Docker-Image CI.yml index 4080a2c..5031fee 100644 --- a/.github/workflows/Docker-Image CI.yml +++ b/.github/workflows/Docker-Image CI.yml @@ -1,4 +1,4 @@ -name: spring-boot Docker Image CI +name: spring-boot Docker Image CI for ECS on: push: branches: [ master ] @@ -29,7 +29,7 @@ jobs: platforms: linux/amd64 push: true file: Dockerfile - tags: ${{ secrets.DOCKERHUB_USERNAME }}/spring-boot:prod-v${{ github.run_number }} + tags: ${{ secrets.DOCKERHUB_USERNAME }}/spring-boot:ECS-v${{ github.run_number }} - name: Image digest run: echo ${{ steps.docker_build.outputs.digest }} From a894097e7c506896512c24a44dbe453cd11a2ac3 Mon Sep 17 00:00:00 2001 From: Rajneesh Mehta <44520236+rajneeshmehta@users.noreply.github.com> Date: Mon, 18 Oct 2021 13:45:46 +0530 Subject: [PATCH 07/11] Update application.properties --- src/main/resources/application.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 68bc8a4..327ae44 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,5 +1,5 @@ ## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties) -spring.datasource.url = jdbc:mysql://db:3306/notes_app?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false +spring.datasource.url = jdbc:mysql://mysql-db.service.com:3306/notes_app?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false spring.datasource.username = app spring.datasource.password = secretpassword @@ -10,4 +10,4 @@ spring.datasource.password = secretpassword spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect # Hibernate ddl auto (create, create-drop, validate, update) -spring.jpa.hibernate.ddl-auto = update \ No newline at end of file +spring.jpa.hibernate.ddl-auto = update From b76d2517cadc7325541b7486c5885c0092776490 Mon Sep 17 00:00:00 2001 From: rajneeshmehta Date: Wed, 20 Oct 2021 09:33:03 +0000 Subject: [PATCH 08/11] update code --- .gitignore | 3 + docker-compose.yml | 9 ++- docker/nginx/Dockerfile.nginx.NoSSl | 2 + docker/nginx/Dockerfile.nginx.SSL | 2 + docker/nginx/default.conf.NoSSL | 19 +++++++ docker/nginx/default.conf.SSL | 33 +++++++++++ task-definitions/app-task-definition.json | 51 +++++++++++++++++ task-definitions/db-task-definition.json | 68 +++++++++++++++++++++++ 8 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 docker/nginx/Dockerfile.nginx.NoSSl create mode 100644 docker/nginx/Dockerfile.nginx.SSL create mode 100644 docker/nginx/default.conf.NoSSL create mode 100644 docker/nginx/default.conf.SSL create mode 100644 task-definitions/app-task-definition.json create mode 100644 task-definitions/db-task-definition.json diff --git a/.gitignore b/.gitignore index ff29930..881ccf4 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,6 @@ dist/ nbdist/ .nb-gradle/ .elasticbeanstalk + +### remove GitPod file ### +*.local \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 03c2659..a173770 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,7 +8,7 @@ services: depends_on: - db db: - image: mysql + image: mysql:5 restart: always expose: - 3306 @@ -17,3 +17,10 @@ services: - MYSQL_DATABASE=notes_app - MYSQL_USER=app - MYSQL_PASSWORD=secretpassword + nginx: + image: java-nginx:ECS-v1 + restart: always + ports: + - 80:80 + depends_on: + - app \ No newline at end of file diff --git a/docker/nginx/Dockerfile.nginx.NoSSl b/docker/nginx/Dockerfile.nginx.NoSSl new file mode 100644 index 0000000..b4e61ed --- /dev/null +++ b/docker/nginx/Dockerfile.nginx.NoSSl @@ -0,0 +1,2 @@ +FROM nginx:alpine +COPY docker/nginx/default.conf.NoSSL /etc/nginx/conf.d/default.conf diff --git a/docker/nginx/Dockerfile.nginx.SSL b/docker/nginx/Dockerfile.nginx.SSL new file mode 100644 index 0000000..171674f --- /dev/null +++ b/docker/nginx/Dockerfile.nginx.SSL @@ -0,0 +1,2 @@ +FROM nginx:alpine +COPY docker/nginx/default.conf.SSL /etc/nginx/conf.d/default.conf diff --git a/docker/nginx/default.conf.NoSSL b/docker/nginx/default.conf.NoSSL new file mode 100644 index 0000000..344f2a2 --- /dev/null +++ b/docker/nginx/default.conf.NoSSL @@ -0,0 +1,19 @@ +server { + listen 80; + server_name devops.aurigastore.com; + server_tokens off; + client_max_body_size 20M; + + location / { + try_files $uri @proxy_api; + } + + location @proxy_api { + proxy_set_header X-Forwarded-Proto https; + proxy_set_header X-Url-Scheme $scheme; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Host $http_host; + proxy_redirect off; + proxy_pass http://app.service.com:8000; + } +} \ No newline at end of file diff --git a/docker/nginx/default.conf.SSL b/docker/nginx/default.conf.SSL new file mode 100644 index 0000000..91e8d85 --- /dev/null +++ b/docker/nginx/default.conf.SSL @@ -0,0 +1,33 @@ +server { + listen 80; + server_name _; + server_tokens off; + client_max_body_size 20M; + + location / { + root /usr/share/nginx/html; + index index.html index.htm; + try_files $uri $uri/ /index.html; + } + + location /api { + try_files $uri @proxy_api; + } + location /admin { + try_files $uri @proxy_api; + } + + location @proxy_api { + proxy_set_header X-Forwarded-Proto https; + proxy_set_header X-Url-Scheme $scheme; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Host $http_host; + proxy_redirect off; + proxy_pass http://backend:8000; + } + + location /django_static/ { + autoindex on; + alias /app/backend/server/django_static/; + } +} \ No newline at end of file diff --git a/task-definitions/app-task-definition.json b/task-definitions/app-task-definition.json new file mode 100644 index 0000000..2693067 --- /dev/null +++ b/task-definitions/app-task-definition.json @@ -0,0 +1,51 @@ +{ + "requiresCompatibilities": [ + "FARGATE" + ], + "inferenceAccelerators": [], + "containerDefinitions": [ + { + "name": "app", + "image": "rajneeshmehta93/spring-boot:ECS-v4", + "memoryReservation": "128", + "resourceRequirements": null, + "essential": true, + "portMappings": [ + { + "containerPort": "8080", + "protocol": "tcp" + } + ], + "environment": null, + "environmentFiles": [], + "secrets": null, + "mountPoints": null, + "volumesFrom": null, + "hostname": null, + "user": null, + "workingDirectory": null, + "extraHosts": null, + "logConfiguration": { + "logDriver": "awslogs", + "options": { + "awslogs-group": "/ecs/spring-boot-app", + "awslogs-region": "us-east-1", + "awslogs-stream-prefix": "ecs" + } + }, + "ulimits": null, + "dockerLabels": null, + "dependsOn": null, + "repositoryCredentials": { + "credentialsParameter": "" + } + } + ], + "volumes": [], + "networkMode": "awsvpc", + "memory": "512", + "cpu": "256", + "executionRoleArn": "arn:aws:iam::597927635090:role/ecsTaskExecutionRole", + "family": "spring-boot-app", + "tags": [] +} \ No newline at end of file diff --git a/task-definitions/db-task-definition.json b/task-definitions/db-task-definition.json new file mode 100644 index 0000000..f3b0af6 --- /dev/null +++ b/task-definitions/db-task-definition.json @@ -0,0 +1,68 @@ +{ + "requiresCompatibilities": [ + "FARGATE" + ], + "inferenceAccelerators": [], + "containerDefinitions": [ + { + "name": "db", + "image": "mysql:5", + "memoryReservation": "128", + "resourceRequirements": null, + "essential": true, + "portMappings": [ + { + "containerPort": "3306", + "protocol": "tcp" + } + ], + "environment": [ + { + "name": "MYSQL_ROOT_PASSWORD", + "value": "supersecretpassword" + }, + { + "name": "MYSQL_DATABASE", + "value": "notes_app" + }, + { + "name": "MYSQL_USER", + "value": "app" + }, + { + "name": "MYSQL_PASSWORD", + "value": "secretpassword" + } + ], + "environmentFiles": [], + "secrets": null, + "mountPoints": null, + "volumesFrom": null, + "hostname": null, + "user": null, + "workingDirectory": null, + "extraHosts": null, + "logConfiguration": { + "logDriver": "awslogs", + "options": { + "awslogs-group": "/ecs/spring-boot-db", + "awslogs-region": "us-east-1", + "awslogs-stream-prefix": "ecs" + } + }, + "ulimits": null, + "dockerLabels": null, + "dependsOn": null, + "repositoryCredentials": { + "credentialsParameter": "" + } + } + ], + "volumes": [], + "networkMode": "awsvpc", + "memory": "512", + "cpu": "256", + "executionRoleArn": "arn:aws:iam::597927635090:role/ecsTaskExecutionRole", + "family": "spring-boot-db", + "tags": [] +} \ No newline at end of file From e2321388c94b1abbc74db1b4cc69f31107139b11 Mon Sep 17 00:00:00 2001 From: Rajneesh Mehta <44520236+rajneeshmehta@users.noreply.github.com> Date: Wed, 20 Oct 2021 15:03:53 +0530 Subject: [PATCH 09/11] Create main.yml --- .github/workflows/main.yml | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 .github/workflows/main.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..62ee695 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,34 @@ +name: nginx Docker Image CI for ECS +on: + workflow_dispatch: + + +jobs: + docker: + runs-on: ubuntu-latest + steps: + - + name: Set up QEMU + uses: docker/setup-qemu-action@v1 + - + name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + - + name: Login to DockerHub + uses: docker/login-action@v1 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - + name: Build and push + id: docker_build + uses: docker/build-push-action@v2 + with: + platforms: linux/amd64 + push: true + file: docker/nginx/Dockerfile.nginx.NoSSl + tags: ${{ secrets.DOCKERHUB_USERNAME }}/java-nginx:ECS-v${{ github.run_number }} + - + name: Image digest + run: echo ${{ steps.docker_build.outputs.digest }} + From 7d02215aac34d58ba57ed4960278bed037208893 Mon Sep 17 00:00:00 2001 From: rajneeshmehta Date: Fri, 22 Oct 2021 08:36:08 +0000 Subject: [PATCH 10/11] update default.conf --- docker/nginx/default.conf.NoSSL | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker/nginx/default.conf.NoSSL b/docker/nginx/default.conf.NoSSL index 344f2a2..58f363f 100644 --- a/docker/nginx/default.conf.NoSSL +++ b/docker/nginx/default.conf.NoSSL @@ -14,6 +14,6 @@ server { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; - proxy_pass http://app.service.com:8000; + proxy_pass http://java-app.service.com:8000; } } \ No newline at end of file From d937634546ab436b15271bc8ad74266688be5926 Mon Sep 17 00:00:00 2001 From: rajneeshmehta Date: Fri, 22 Oct 2021 09:21:21 +0000 Subject: [PATCH 11/11] update default.conf --- docker/nginx/default.conf.NoSSL | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker/nginx/default.conf.NoSSL b/docker/nginx/default.conf.NoSSL index 58f363f..9a291ac 100644 --- a/docker/nginx/default.conf.NoSSL +++ b/docker/nginx/default.conf.NoSSL @@ -14,6 +14,6 @@ server { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; - proxy_pass http://java-app.service.com:8000; + proxy_pass http://java-app.service.com:8080; } } \ No newline at end of file