a) Create Spring boot application

  1. https://start.spring.io/
    Screen Shot 2018-11-08 at 10.30.54 PM.png
  2. Open SpringToolSuite and import as maven project from the location the above one was saved.
  3. Ensure you have this dependency in pom.xml

    <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

     

  4. Update pom.xml to add a final name to customize the name of your jar file taht is getting generated when you do a maven install.Screen Shot 2018-11-08 at 10.34.49 PM
  5.  update application.properties  to have the below value [ By default spring boot application have the embedded tomcat listen to 8080 port]

    server.port=8085

     

  6.  Create a new class HelloResource.java as below
    Screen Shot 2018-11-08 at 11.37.42 PM.png

b) Create Dockerfile 

  • Create a text file on project root location , and name it as Dockerfile

    FROM openjdk:8
    COPY target/dockerspring-boot.jar docker-spring-boot.jar
    EXPOSE 8085
    ENTRYPOINT [“java”, “-jar”, “docker-spring-boot.jar”]

    Run as maven install

 

c) Docker commands to create the image and push image to dockerhub

Time to write some docker commands

  • docker ps
  • docker images

docker build -f Dockerfile -t docker-spring-boot .

Screen Shot 2018-11-08 at 10.49.15 PM

 

docker run -p 8085:8085 docker-spring-boot -d

 

Screen Shot 2018-11-08 at 10.53.07 PMScreen Shot 2018-11-08 at 10.53.28 PM

Open another terminal and write below commands

docker images

Screen Shot 2018-11-08 at 11.32.25 PM

docker ps

Screen Shot 2018-11-08 at 11.33.34 PM

d) view the application in browser

  • Open browser and enter
    http://localhost:8085/rest/docker/hello
    you would seeScreen Shot 2018-11-08 at 11.35.38 PM.png
  • Now push this image to dockerhubdocker psdocker loginScreen Shot 2018-11-08 at 11.39.07 PMdocker imagesScreen Shot 2018-11-08 at 11.45.46 PM

    docker ps

     

    docker tag 06153d5d831a codinko/docker-spring-boot:initialone

    docker push codinko/docker-spring-boot

    That’s it !!!

    e) View the created docker application image  in dockerhub

  •  Let’s view it in dockerhubScreen Shot 2018-11-08 at 11.52.00 PMScreen Shot 2018-11-08 at 11.54.32 PM.png
    —–
  • In case due to internet bandwidth or any reason, you don’t want to upload your image to dockerhub, but want to save it and distribute, there’s an option.Solutions to these problems can be to save the Docker container locally as a a tar archive, and then you can easily load that to an image when needed.To save a Docker image after you have pulled, committed or built it you use the docker save command. For example, lets save a local copy of the docker-spring-boot docker image we made:
    docker save docker-spring-boot  >docker-spring-boot.tar

    If we want to load that Docker container from the archived tar file in the future, we can use the docker load command:

    docker load --inputdocker-spring-boot.tar

    Reference on how to push/pull from/to dockerhub: 
    https://ropenscilabs.github.io/r-docker-tutorial/04-Dockerhub.html

 

f) A variation: Q – How to do the same using docker-compose.yml instead of doing docker build and docker run?

After doing the maven install, skip the rest of the steps above and do the below

Create docker-compose.yml on project root

version: ‘3’

services:

docker-spring-boot-01:
image: docker-spring-boot-01-img
container_name: docker-spring-boot-01-container
build:
context: .
dockerfile: Dockerfile
ports:
– 8085:8085

It looks like this:

Screen Shot 2019-01-01 at 11.17.14 AM.png

Step 11: docker-compose build
Step 12: docker-compose up

That’s it

g) Access the application from browser after using docker-compose

Access the service

Screen Shot 2019-01-01 at 11.18.01 AM

Screen Shot 2019-01-01 at 11.18.55 AM

Lets take a look at docker images and docker ps …

docker images

Screen Shot 2019-01-01 at 11.20.40 AM

docker ps

Screen Shot 2019-01-01 at 11.20.49 AM