/spring-docker-demo

Writing docker file for spring boot and running it in container using docker image.

Primary LanguageJava

spring-docker-demo

Writing docker file for spring boot and running it in container using docker image, pushing and pulling to/from docker hub.


For detail explanation of how to dockerize simple spring boot Java application, you can follow my website at Coding Guides

Explanation


  • First make sure that docker is installed on your machine, depending on the OS you're using, it can be downloaded from official documentation of Docker at Docker Website

  • Now write docker file with what each instruction tells about in Dockerfile located at the root directory of the project.

  • Now build the docker image using following command docker build --tag spring-docker-world . here spring-docker-world is the name I've used, you can use your own name and remember there is space and a . (dot)

  • Image is built, it's time to run the using the container, the command looks like docker run -d -p 8080:8080 spring-docker-world

  • At this moment if you don't get any error, your application is running on the docker container and not on the local machine, to confirm this type localhost:8080 in the browser and you'll see the simple message which has been written in SpringDockerAppApplication.java file.

  • Reference can be found at Docker Official Website and This tutorial


Pushing docker image to docker hub

  • Create account at docker hub Docker Hub

  • Create the repository

  • Here we need to create the tagname for our unofficial image by running the following command, docker tag spring-docker-world:latest badripaudel77/hello-java-docker:latest here, badripaudel77 is my dockerhub username and hello-java-docker:latest is the image name and tagname respectively in docker hub while spring-docker-world is the local image name, username will be different for you and rest can be same or different.

  • Now push to docker hub using the following command, docker push badripaudel77/hello-java-docker:latest

  • After a while it will be pushed to docker hub which you can see at Very First and the simplest Spring-Docker app

  • It can also be pulled using command docker pull badripaudel77/hello-java-docker:latest

  • And run to see if it's working just run the command docker run -p 8080:8080 badripaudel77/hello-java-docker:latest you should see the same output as before but this time pulling image from docker hub that we created and published.

Cheers !!! we've created docker file, build image, run image locally, pushed it to the docker hub and finally pulled image from remote docker hub repository.