In our last post "Microservices with Spring" we created the start of a template to use for our microservices to be deployed to some cloud provider. Our next step is going to be to containerize our template using docker.
In this post we will create a Dockerfile, add the commands to copy in our built version of our service and finally add the command that will start our service inside the container.
Step 1: You can open the same project from "Microservices with Spring" or create a new project and add in the hello mapping again.
Step 2: Create the "Dockerfile" in the base of you application.
Step 3: Add in our docker file commands.
FROM - Identifying our base image to build off of. openjdk alpine is pretty common
RUN / USER - There is a school of though that if we run our app under a different user other then root we get some more security abstraction.
ARG - Just defining an variable
COPY - place the files we need into our container
ENTRYPOINT - The command we want run on container start-up
STEP 4: Lets build our application
run command >./gradlew build
STEP 5: Add in our plugin (id 'com.google.cloud.tools.jib' version '1.8.0') to build our container via gradle.
Step 6: Build our image.
Lets run the command - ./gradlew jibDockerBuild --image=colwellcoding/dockerize-demo
Here --image defines the name of our image and after build you should see something like this.
Step 7: Run and bask in our glory!
Run the command - docker run -p 8080:8080 -t colwellcoding/dockerize-demo
Again "-t colwellcoding/dockerize-demo" is the name of your image you built in step 6. After which you should see your image running in the console and be able to hit your endpoint again at http://localhost:8080/hello
In our next post we make a more meaningful REST API to be used in our application, as promised.
I hope this was helpful. If so please follow and sign-up for update notifications.
Reference Links:
https://spring.io/
https://start.spring.io/
Comments