how-do-deploy-spring-application-on-docker-container-freestylecoder.com

How to deploy spring application in the Docker container | AWS 2020

Deploy spring application in Docker container in AWS EC2 machine.

In this article, we are going to learn How we can deploy our java application on a docker container on AWS.

If you don’t know about docker you can read first then start this tutorial. Read about what is docker? and why to use docker?

Deploy spring application in Docker container in AWS EC2 machine complete step by step video tutorial.

you can watch the video tutorial first. so you will get a better understanding.

Deploy spring application in Docker container | Step 1: Install EC2

In this step, You require an amazon ec2 instance with a 30GB hard disk size. 

if you don’t know how to create an amazon ec2 instance you can read this article. Where you get step by step guide to install amazon ec2 instance.

After installing EC2 do SSH. 

How-To-Do-SSH-From-mobaxTerm

Deploy spring application in Docker container | Step 2: Install Docker

  1. Connect to your instance. (SSH to your instance)

2. Update Your instance.

sudo yum update -y

3. Install the Docker Engine.

sudo amazon-linux-extras install docker

4. Start Docker Service.

sudo service docker start

 

5. You have to add ec2-user to the docker group because of that you can execute Docker commands without using sudo.

sudo usermod -a -G docker ec2-user

 

6. Verify docker is installed or not.

docker info

docker --version

 

So Docker is completely installed in your EC2 machine.

Deploy spring application in Docker container Step 3: Create a Docker image

  1. Go into sudo user.
sudo -i

2. Create one folder in your system.

mkdir container

3. Create a docker file with the name Dockerfile.

vi Dockerfile

You can not use a different name for the docker file. If you want to create more than one container you can create different folders and create a docker file into that.

4. Use the below code for installing java, Tomcat, and the environment set up in the docker file.

FROM ubuntu:18.04

# Install Jdk
RUN apt-get update && \
apt-get install -y openjdk-8-jdk && \
apt-get install -y ant && \
apt-get clean
RUN java -version

# Install Apache Tomcat
RUN apt-get install -y wget && \
wget https://downloads.apache.org/tomcat/tomcat-9/v9.0.40/bin/apache-tomcat-9.0.40.tar.gz && \
tar -xvzf apache-tomcat-9.0.40.tar.gz

# Chmod permissions
RUN chmod +x apache-tomcat-9.0.40/bin/startup.sh && \
chmod +x apache-tomcat-9.0.40/bin/shutdown.sh

# Creating Context.xml
RUN wget https://demo134235.s3.amazonaws.com/sudya/context.xml


# Cp command
RUN cp context.xml apache-tomcat-9.0.40/webapps/manager/META-INF/


# Downloading War
RUN wget https://demo134235.s3.amazonaws.com/sudya/AWSFirstPro1.war

# Cp command
RUN cp AWSFirstPro1.war apache-tomcat-9.0.40/webapps

#Run CMD
CMD ["apache-tomcat-9.0.40/bin/catalina.sh", "run"]
EXPOSE 8080

Add this content into your docker file. Add Your war file link.

for that upload your war file on the s3 bucket and make it public. Then add the object link in the Downloading war session put the name of the war file in the cp command and your project is ready to go.

5. Build a docker image by using your Dockerfile. You can give any image name.

docker build -t imageName .

6. Check all images present in your docker.

docker images

7. Check your image is created or not.

docker images --filter reference=imageName

8. RUN image.

docker run -t -i -p 8080:8080 imageName

Context.xml File Content :

<?xml version=”1.0″ encoding=”UTF-8″?>

<Context antiResourceLocking=”false” privileged=”true” >

<Manager sessionAttributeValueClassNameFilter=”java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap”/>
</Context>

Boost Your Programing Skils With Our Tips

About the Author: Rahul Wagh

You May Also Like

7 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *