Containers are a hot topic nowadays. Is it easy or does it come with a steep learning curve and what are the things to consider? In this blog serie I will show some simple examples of how you can use containers and in which environments. In part 1 I will build a simple SpringBoot application which can be accessed through REST. We will then dockerize it and first run it local and after that, push it to DockerHub.
Part 1: Building your first container, push it DockerHub and run it.
I have created a simple SpringBoot app which uses an in-memory database H2 to store and retrieve climbers. I have exposed 3 REST endpoints with which you can:
- get all climbers names: GET /api/climbers
- get a specific climber: GET /api/climbers/{id}
- add a climber: POST /api/climbers
I already have written a blog about how make use of springboot here so I will not go into the details of that here. If you want the source code, just download the zip ClimbersAPI. Just start the application and after it starts up, use Postman to check if it works. You should see the following results:
Add climber
I added a couple more for some extra data.
Show all climbers
Show specific climber
Ok so that seems to work ok. Now for the Docker part. We want not to ship the springboot app but we want ship it as a whole container so how do we do that? First you want install Docker. There are plenty of good tutorials about that. If your system doesn’t meet the requirements, you can always use Boot2Docker as I did. Once you have installed this, start the Docker Quickstart Terminal on your desktop. From there, navigate to the place where our project is. I just added a docker folder under my user where I placed the project. We are going to use a Maven plugin from Spotify to make a docker container from the springboot app. You can see the plugin in the pom.xml:
com.spotify dockerfile-maven-plugin 1.3.6 ${docker.image.prefix}/${project.artifactId} ${project.version} target/${project.build.finalName}.jar
Next to this we need a DockerFile which tells how the images should be build up. Mine looks like this:
FROM openjdk:8-jdk-alpine VOLUME /tmp ARG JAR_FILE ADD ${JAR_FILE} app.jar ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
This just says to use openjdk version 8 and to add the springboot jar. Then set the entrypoint which configures the container what to execute. Lets first do a:
mvn package
This just packages the project.
Now do
mvn dockerfile:build
to make a docker image which we can run. As you can see we have successfully build the hugohendriks/climbersapi-docker:1.0.0 image
If we want to be able to push the image to DockerHub where we can store it in a repository, we first have to login. Go to DockerHub to make an account if you haven’t already done so.
docker login
After this is done, you should be able to push the image.
mvn dockerfile:push
When this is done you can go to DockerHub and you should see your image there.
Now for the proof of the pudding lets see if we can pull this image and run it. First lets delete the image you have by first checking the id of the image.
docker images
Now delete the just create image by running
docker rmi -f XXXXXXXXXXXX
and after that do a fresh pull
and to top it off, lets run the image
docker run -p 8080:8080 -t hugohendriks/climbersapi-docker:1.0.0
When it has completely started up, use postman to test it. As you can see, we are talking to 192.168.99.100 and we get a response back of the initial db loaded.
I just scratched the surface here of Docker. In a next post I will try to run this image on Oracle Container Cloud Service.
References:
Pingback: Deploy your docker container on the Oracle Container Cloud Service