Showing posts with label docker. Show all posts
Showing posts with label docker. Show all posts

Monday, 14 January 2019

How to Deploy your docker container on Amazon EC2 Elastic Container Service

Deploy our docker container on Amazon EC2 Elastic Container Service

In previous post, we discuss about how to run SpringBoot API as a Docker.

I am going to use Amazons fargate for Amazon EC2 Elastic Container Service. 

"AWS Fargate is a compute engine for Amazon ECS that allows you to run containers without having to manage servers or clusters. With AWS Fargate, you no longer have to provision, configure, and scale clusters of virtual machines to run containers. This removes the need to choose server types, decide when to scale your clusters, or optimize cluster packing. AWS Fargate removes the need for you to interact with or think about servers or clusters. Fargate lets you focus on designing and building your applications instead of managing the infrastructure that runs them"

For more information you can go to https://aws.amazon.com/fargate/

Step 1: Log into Amazon and go to the console. Select Elastic container service and click let's get started.

How to Deploy your docker container on Amazon EC2 Elastic Container Service

Step 2: Click the Configure button under Container Configuration and specify the basics.

  • container name: EmployeesAPIContainer
  • Image: niteshkumar/employeesapi-docker:1.0.1
How to Deploy your docker container on Amazon EC2 Elastic Container Service
          keep the Advanced features on default and click Update.


Step 3: Service configuration

How to Deploy your docker container on Amazon EC2 Elastic Container Service

Step 4: Cluster configuration

Just accept the default values and click next

How to Deploy your docker container on Amazon EC2 Elastic Container Service

Step 5: Review

Check to see if all the configuration are correct and click create.

How to Deploy your docker container on Amazon EC2 Elastic Container Service

Step 6: Click view service

In the logs we can see container was successfully started.

How to Deploy your docker container on Amazon EC2 Elastic Container Service

Step 7: Check Public IP

Click Tasks and then on the task id, we can get public IP in the overview. By using this IP we can check if our API is available or not.

Step 8: Use Postman or RestClient to connect API's using these public IP address.


References: 


Related post:

Implementation of swagger in SpringBoot API

How to run SpringBoot API as a Docker container




How to run SpringBoot API as a Docker container

How to run SpringBoot API as a Docker container

We have created SpringBoot API in our previous post and you can download.

Docker is a container management service that eases building and deployment.The keywords of Docker are develop, ship and run anywhere. The whole idea of Docker is for developers to easily develop applications, ship them into containers which can then be deployed anywhere.

The official site for Docker is https://www.docker.com/ The site has all information and documentation about the Docker software. It also has the download links for various operating systems.

Prerequisite:

We will need these files to run SpringBoot API as a Docker container.
  • pom.xml
  • Dockerfile
How to run SpringBoot API as a Docker container
pom.xml: We are going to use a Maven plugin from Spotify to make a docker container from the springboot app. We can see the plugin in the pom.xml:

<!-- dockerfile plugin -->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <version>1.3.6</version>
                <configuration>
                    <repository>${docker.image.prefix}/${project.artifactId}</repository>
                    <tag>${project.version}</tag>
                    <buildArgs>
                        <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
                    </buildArgs>
                </configuration>
            </plugin>
Dockerfile: Next we need a Dockerfile which tells how the image should be build up.

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
ADD ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
This just says to use openjdk version 8 and to add the springboot jar.

Step 1: Go to where project is placed using CMD and run 


mvn package

How to run SpringBoot API as a Docker container

Step 2: Now run

mvn dockerfile:build
to make a docker image which we can run. As you can see we have successfully build the niteshkumar/employeesapi-docker:1.0.0 image
How to run SpringBoot API as a Docker container

Step 3: If we want to be able to push the image to DockerHub where we can store it in a repository, we first have to login.
docker login
How to run SpringBoot API as a Docker container

Step 4: Now, we should be able to push the image.
mvn dockerfile:push
How to run SpringBoot API as a Docker container

After success you can go to DockerHub and you can see your image in Repositories.

Now, we can pull this image from DockerHub and run it. To achieve this we will execute below commands.

docker images
docker rmi -f XXXXXXXXXXX
docker pull niteshkumar/employeesapi-docker:1.0.1
docker run -p 8080:8080 -t niteshkumar/employeesapi-docker:1.0.1

you should be able to connect API with Postman or RestClient.


Next Post: How to Deploy your docker container on Amazon EC2 Elastic Container Service



Related Post: 

How to build simple microservice using Spring Boot

Implementation of swagger in SpringBoot API

How to Deploy your docker container on Amazon EC2 Elastic Container Service