{"id":244,"date":"2023-03-20T11:32:01","date_gmt":"2023-03-20T11:32:01","guid":{"rendered":"https:\/\/keyshell.net\/blog\/?p=244"},"modified":"2023-03-20T11:33:53","modified_gmt":"2023-03-20T11:33:53","slug":"how-to-build-and-push-spring-boot-docker-image-to-aws-ecr-and-deployed-to-an-ecs-container-using-aws-codepipeline","status":"publish","type":"post","link":"https:\/\/keyshell.net\/blog\/2023\/03\/20\/how-to-build-and-push-spring-boot-docker-image-to-aws-ecr-and-deployed-to-an-ecs-container-using-aws-codepipeline\/","title":{"rendered":"How to Build and Push Spring-boot docker image to AWS ECR and deployed to an ECS container using AWS CodePipeline"},"content":{"rendered":"<p>In this blog, we describe How to Build and Push the Spring-boot docker image to AWS ECR and deployed it to an ECS container using AWS CodePipeline. We will build a sample spring-boot Application, push the image to AWS ECR and then deploy it to AWS ECS.<\/p>\n<p id=\"8e8c\" class=\"pw-post-body-paragraph si sj rm lq b sk sl sm sn so sp sq sr ss st su sv sw sx sy sz ta tb tc td te kd bi\" data-selectable-paragraph=\"\"><strong class=\"lq kj\">Prerequisites:<\/strong><\/p>\n<p id=\"c8b7\" class=\"pw-post-body-paragraph si sj rm lq b sk sl sm sn so sp sq sr ss st su sv sw sx sy sz ta tb tc td te kd bi\" data-selectable-paragraph=\"\">1. AWS Account<\/p>\n<p id=\"f76d\" class=\"pw-post-body-paragraph si sj rm lq b sk sl sm sn so sp sq sr ss st su sv sw sx sy sz ta tb tc td te kd bi\" data-selectable-paragraph=\"\">2. GitHub Account<\/p>\n<p data-selectable-paragraph=\"\"><strong class=\"lq kj\">Create an AWS ECR repository<\/strong><\/p>\n<p id=\"0cce\" class=\"pw-post-body-paragraph si sj rm lq b sk sl sm sn so sp sq sr ss st su sv sw sx sy sz ta tb tc td te kd bi\" data-selectable-paragraph=\"\"><em class=\"gt\">Login to your AWS account and create an Amazon Elastic Container Registry repository with a name.<\/em><\/p>\n<p id=\"a50a\" class=\"pw-post-body-paragraph si sj rm lq b sk sl sm sn so sp sq sr ss st su sv sw sx sy sz ta tb tc td te kd bi\" data-selectable-paragraph=\"\"><em class=\"gt\">Create a cluster inside AWS ECS and select the cluster template as \u201cNetworking Only\u201d because we use AWS FARGATE here.<\/em><\/p>\n<p data-selectable-paragraph=\"\"><strong class=\"lq kj\">Create a Task Definition<\/strong><\/p>\n<p data-selectable-paragraph=\"\"><em class=\"gt\">Select launch type as FARGATE, select task role as \u201cecsTaskExecutionRole\u201d, Select your operating system, select Task memory and CPU, and Add container with container name and the image should be the ECR repository URI.<\/em><\/p>\n<p data-selectable-paragraph=\"\"><strong class=\"lq kj\">Create a service inside the cluster<\/strong><\/p>\n<p data-selectable-paragraph=\"\"><em class=\"gt\">Click on Clusters, then select our cluster, create a service with a service name, and select launch type as FARGATE.<\/em><\/p>\n<p data-selectable-paragraph=\"\"><strong class=\"lq kj\">Create a \u201cbuildspec.yml\u201d file on the project repository<\/strong><\/p>\n<pre><code>version: 0.2\r\nphases:\r\npre_build:\r\ncommands:\r\n- mvn clean install\r\n- echo Logging in to Amazon ECR...\r\n- aws --version\r\n- $(aws ecr get-login --region $AWS_DEFAULT_REGION --no-include-email)\r\n- REPOSITORY_URI=788155875213.dkr.ecr.us-east-2.amazonaws.com\/demospringboot\r\n- COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)\r\n- IMAGE_TAG=build-$(echo $CODEBUILD_BUILD_ID | awk -F\":\" '{print $2}')\r\nbuild:\r\ncommands:\r\n- echo Build started on `date`\r\n- echo building the Jar file\r\n- echo Building the Docker image...\r\n- docker build -t $REPOSITORY_URI:latest .\r\n- docker tag $REPOSITORY_URI:latest $REPOSITORY_URI:$IMAGE_TAG\r\npost_build:\r\ncommands:\r\n- echo Build completed on `date`\r\n- echo Pushing the Docker images...\r\n- docker push $REPOSITORY_URI:latest\r\n- docker push $REPOSITORY_URI:$IMAGE_TAG\r\n- echo Writing image definitions file...\r\n- printf '[{\"name\":\"spring-container\",\"imageUri\":\"%s\"}]' $REPOSITORY_URI:$IMAGE_TAG &gt; imagedefinitions.json\r\n- cat imagedefinitions.json\r\nartifacts:\r\nfiles:\r\n- imagedefinitions.json\r\n- target\/spring-boot-ecs.ja<\/code><\/pre>\n<p><strong>Edit pom.xml file and add <strong class=\"lq kj\">the line &lt;finalName&gt;spring-boot-ecs&lt;\/finalName&gt;<\/strong><\/strong><\/p>\n<p><em class=\"gt\">It indicates that the name of the generated artifact will be spring-boot-ecs. This name is often used to create a JAR or WAR file, which can then be deployed to a server or container, such as Amazon Elastic Container Service (ECS), to run a Spring Boot application.<\/em><\/p>\n<p><strong class=\"lq kj\">Create Dockerfile on the project repository<\/strong><\/p>\n<pre><code>FROM openjdk:18-jdk-slim\r\n\r\nEXPOSE 8080\r\n\r\nADD target\/spring-boot-ecs.jar spring-boot-ecs.jar \r\n\r\nENTRYPOINT [\"java\",\"-jar\",\"\/spring-boot-ecs.jar\"]\r\n\r\n<\/code><\/pre>\n<p><strong>Go to AWS CodePipeline and create a Pipeline<\/strong><\/p>\n<p id=\"d3cd\" class=\"pw-post-body-paragraph si sj rm lq b sk sl sm sn so sp sq sr ss st su sv sw sx sy sz ta tb tc td te kd bi\" data-selectable-paragraph=\"\"><em class=\"gt\">Click on create new pipeline \u2192 Enter the pipeline name \u2192 Next<\/em><\/p>\n<p id=\"af3c\" class=\"pw-post-body-paragraph si sj rm lq b sk sl sm sn so sp sq sr ss st su sv sw sx sy sz ta tb tc td te kd bi\" data-selectable-paragraph=\"\"><em class=\"gt\">Select source provider as GitHub \u2192 Connect to GitHub \u2192 Select the repository of our project \u2192 Select branch \u2192 Next<\/em><\/p>\n<p id=\"b163\" class=\"pw-post-body-paragraph si sj rm lq b sk sl sm sn so sp sq sr ss st su sv sw sx sy sz ta tb tc td te kd bi\" data-selectable-paragraph=\"\"><em class=\"gt\">Select the build provider as AWS CodeBuild \u2192 Create a project with the project name, operating system and also tick the below field<\/em><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"my-custom-class\" src=\"https:\/\/keyshell.net\/blog\/wp-content\/uploads\/2023\/03\/tick.png\" alt=\"\" width=\"996\" height=\"596\" \/><\/p>\n<p>&nbsp;<\/p>\n<p class=\"graf graf--p\"><em class=\"markup--em markup--p-em\">Select the Deploy provider as AWS ECS \u2192 Select cluster name and service name.<\/em><\/p>\n<p><em>Create Pipeline.<\/em><\/p>\n<p id=\"b178\" class=\"pw-post-body-paragraph si sj rm lq b sk sl sm sn so sp sq sr ss st su sv sw sx sy sz ta tb tc td te kd bi\" data-selectable-paragraph=\"\"><strong class=\"lq kj\">Click on the pipeline and we can see the process<\/strong><\/p>\n<p id=\"0d07\" class=\"pw-post-body-paragraph si sj rm lq b sk sl sm sn so sp sq sr ss st su sv sw sx sy sz ta tb tc td te kd bi\" data-selectable-paragraph=\"\"><strong class=\"lq kj\">Here the image is pushed into the AWS ECR repository and deployed this image into AWS ECS. After the deployment is completed move to the AWS service inside our cluster and click on the task we get a public IP, copy this IP, and search into a brows<\/strong>er.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"my-custom-class\" src=\"https:\/\/keyshell.net\/blog\/wp-content\/uploads\/2023\/03\/op-1.png\" alt=\"\" width=\"1920\" height=\"1024\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, we describe How to Build and Push the Spring-boot docker image to AWS ECR and deployed it to an ECS container using AWS CodePipeline. We will build a sample spring-boot Application, push the image to AWS ECR and then deploy it to AWS ECS. Prerequisites: 1. AWS Account 2. GitHub Account Create [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[1],"tags":[13,11,12,10],"class_list":["post-244","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-aws-codepipeline","tag-aws-ecr","tag-aws-ecs","tag-spring-boot"],"_links":{"self":[{"href":"https:\/\/keyshell.net\/blog\/wp-json\/wp\/v2\/posts\/244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/keyshell.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/keyshell.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/keyshell.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/keyshell.net\/blog\/wp-json\/wp\/v2\/comments?post=244"}],"version-history":[{"count":18,"href":"https:\/\/keyshell.net\/blog\/wp-json\/wp\/v2\/posts\/244\/revisions"}],"predecessor-version":[{"id":268,"href":"https:\/\/keyshell.net\/blog\/wp-json\/wp\/v2\/posts\/244\/revisions\/268"}],"wp:attachment":[{"href":"https:\/\/keyshell.net\/blog\/wp-json\/wp\/v2\/media?parent=244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/keyshell.net\/blog\/wp-json\/wp\/v2\/categories?post=244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/keyshell.net\/blog\/wp-json\/wp\/v2\/tags?post=244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}