Access with Java Springboot
Installing Docker
Section titled “Installing Docker”Let’s use the EC2 instance for understanding the concepts of docker and later you can install it on your laptop or desktop using Docker Desktop
- Login into one of the EC2 instance that you have it running.
- Once you SSH into the instance, issue the below commands
sudo yum install dockersudo usermod -a -G docker ec2-usernewgrp dockersudo systemctl enable docker.servicesudo systemctl start docker.service- To check if your docker is up and running run
pscommand
docker ps- To test if the docker is installed properly, you will use hello-world image from docker repository
docker pull hello-worlddocker run hello-world- In order to test if you can access the service running inside the docker you can install an nginx server and try accessing using Public IP from your EC2 instance
docker pull nginxdocker run -it --rm -d -p 8080:80 --name web nginx- When you access the port 8080 from your browser with your EC2 instance IP address
http://PUBLIC_IP:8080you will see below output.
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.Commercial support is available at nginx.com.
Thank you for using nginx.Packaging API
Section titled “Packaging API”As you have seen earlier in introduction, you need to install JDK17 and Maven and other dependent packages used by project to connect to AWS DynamoDB. You cannot do this everywhere this service needs to run.
This issue is addressed by containers where it packages all the dependencies as image that can be just downloaded and run immediately.
- To convert your application into a image you need to provide the instructions to docker what needs to be added. This is given in the form of Dockerfile
FROM openjdk:17VOLUME /tmpCOPY target/dynamo-1.0.0.jar app.jarENTRYPOINT ["java","-jar","/app.jar"]EXPOSE 8080- Instructions say that
- Download the JDK17 from openjdk
- Mount the /tmp volume
- Copy the jar file from target directory to image
- Run the jar file
- Expose the port 8080
- In order to build this image from the instruction file, where
ACCOUNT_NAMEis your dockerhub account name
docker build -t ACCOUNT_NAME/spring-rest-api .- This image needs to available in a place where the EC2 instance can download from, hence you publish this image to DockerHub repository which is publicly accessible. If this is your first time it will prompt you for your DockerHub credentials.
docker push ACCOUNT_NAME/spring-rest-apiRunning API
Section titled “Running API”-
Now login to your EC2 instance and make sure docker is up and running using command
docker ps. If its not running refer the Docker instrutions -
Once the docker is up and running, you can now download the image you published earlier and start the image
docker pull ACCOUNT_NAME/spring-rest-apidocker run -it --rm -d -p 8080:8080 ACCOUNT_NAME/spring-rest-api- You can now access your API using your instance public IP
http://PUBLIP-IP:8080/productsand you should be able to see the items from your DynamoDB table
If you get any access denied error while reading from DynamoDB, remember you need to provide permission using roles for EC2 to talk to DynamoDB service.
Tada! You just published a API for anyone to use :)
You just experienced the power of containers where you build your application once and anyone can use them without worrying about dependencies in their system. It just works and not just on your machine ;)