Some days ago I confronted to docker, and more precisely with Springboot application. I had to create an image of my springboot app and export it in docker. So I decide to write an article to show you how I dockerize my app.

A little word about Docker

Docker bring the possibility to work easily with container in a simple way.

For the official docker website, docker is

Docker simplifies and accelerates your workflow, while giving developers the freedom to innovate with their choice of tools, application stacks, and deployment environments for each project.

And we can also find this explanation

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and deploy it as one package. By doing so, thanks to the container, the developer can rest assured that the application will run on any other Linux machine regardless of any customized settings that machine might have that could differ from the machine used for writing and testing the code. — OpenSource.com, what is Docker?

We can understand that Docker is a super power machine to help developer in their flow whitout thinking about infrastructure stuff. You code then dockerize !

Using Docker can help to solve troubles like “it works on your machine but not mine ?!” we hear a lot time a developer that complains on problem to run an application on his computer. It can prevents the confusion when a dev’s been working on their local machine on a project for days (or weeks), and as soon as it’s deployed it and the app won’t run. Most likely because somebody add dependencies that are necessary to run the application but missing in the pom.xml.

Docker tools

Engine

It’s what builds and executes Docker images from either a single Dockerfile or docker-compose.yml

Swarm

creates a swarm of Docker Engines where you can deploy application services. You don’t need additional orchestration software to create or manage a swarm

Compose

This is what you use when you have an application made up of multiple microservices, databases and other dependencies. The docker-compose.yml allows you to configure all those services in one place and start them all with a single command.

Machine

Docker Machine is a tool that lets you install Docker Engine on virtual hosts, and manage the hosts

Some terminology

Docker image

A Docker image contains a set of instructions for creating a container that can run on the Docker platform.

docker container

Containers are instances of Docker images that can be run using the Docker run command. The basic purpose of Docker is to run containers.

docker hub/registry

The registry works basically like a git repository, allowing you to push and pull container images.

docker volume

In order to be able to save (persist) data and also to share data between containers, Docker came up with the concept of volumes

Docker File

Docker knows nothing ! you should tell to docker what you need in your container because there is nothing installed in it.

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.- Docker, Dockerfile Reference

a docker file begin with a base image, you will tel to your container what you want to install.

Then you will settup instruction that docker should follow (environment variable, where files lives …)

When creating this docker file you will certainly use some command that are common, here is some of them :

FROM: define the base image of the container

RUN: execute a command in a new layer on the top of image.

EXPOSE: informs Docker that the container listens on the specified network ports at runtime

ENV: sets the environment variable in a key – value form

VOLUME : creates a mount point with the specified name and marks it as holding externally mounted volumes from the native host or other containers.

Exemple of Java docker file

Copy to Clipboard

You can have a lot of different docker file , that could be the subject for a futur post 🙂

We define the bases of docker and see what is the role of the dockerfile with some example. Later, in another post, we will go deeper in dockerfiles and in the usage of docker.

So see you soon with another post on Docker 🙂

Dockerize !

Create a simple springboot app

For creating the application I used SpringInitialzr web app. I added only the spring-web dependency and use maven with java 11. But no problem to use a older version of java of use Gradle

We will just add a simple REST controller and try to access to the defined url when the image is running in Docker.

I use Docker Desktop for windows (don’t forget to activate Hyper-v options).

first we will add a controller in our application

Copy to Clipboard

When you go to http://localhost:8081/docker the page will display Hello Docker.

That’s simple !

The Jar

run a maven build install to generate the artifact of the application

The dockerfile

now create the dockerfile for our application

Copy to Clipboard

1. FROM : create the based image, here we use openjdk 11.

2. VOLUME : mount point where tomcat will create a working folder.

3. ADD : add the jar previously create with maven.

4. ENTRYPOINT : the executable which should run when a container is started from a Docker image.

Build

In a terminal, go to the Dockerfile location and then run the following command to export the image to docker

Copy to Clipboard

This will add a new image in your docker

You can check if the image is well created by using the following command :

Copy to Clipboard

Or you can see in the docker desktop the following information :

Run Docker, Run

In order to run our docker image we have to do the port mapping. It is because our application is running on some kind of virtual environment, so we have to do the port mapping from your host machine to the docker machine. Use the following command to do that :

Copy to Clipboard

If you want to stop the container run the following command :

Copy to Clipboard

You can find the CONTAINER_ID by using the command :

Copy to Clipboard

Use Docker Compose

we can also run a container by using the docker compose tool from docker. You have to write a Yaml file with the instruction to run the image you want. Here is a yml file we can use for :

Copy to Clipboard

Then, to run it, use that command :

Copy to Clipboard

-f : define the compose file you want to run

up : Builds, (re)creates, starts, and attaches to containers for a service.

the version on the top of the file correspond to the version of the compose file and depend of your docker engine version (https://docs.docker.com/compose/compose-file/compose-versioning/)

normally docker compose is use to run multiple container at the same time, but here I would just to introduce it with an example.

I hope you findthis article useful and have understood how to containerize a Spring Boot application using Docker. Thanks for reading

you can find the source here