avatarMd Sajedul Karim

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

5255

Abstract

renowned applications must have entries in the registry</li><li>Docker Hub is a public registry that anyone can use, and Docker is configured to look for images on Docker Hub by default.</li><li>You can even run your own private registry.</li><li>docker push and pull command are used to push and pull images from the public registry’s</li></ul><p id="e8de"><b>The Docker daemon</b></p><ul><li>listens for Docker API requests and manages Docker objects</li><li>A daemon can also communicate with other daemons to manage Docker services.</li></ul><p id="fa58"><b>Docker Client</b></p><ul><li>a primary way that many Docker users interact with Docker</li><li>It takes clients command and sends it to the Docker daemon</li><li>Can communicate with multiple daemons</li></ul><p id="cf43"><b>3. Dockerizing spring app with detailed explanations </b>For dockerizing spring boot applications, we won’t do extra things. We just add a file named <b>Dockerfile </b>into the home directory. This file will contain all instructions for running this application</p><figure id="663c"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*DZn53S3nk-FOL_SSqwvpPQ.png"><figcaption>Dockerfile location and code</figcaption></figure><p id="2ab4">Our docker file code is</p> <figure id="6f4a"> <div> <div>

            <iframe class="gist-iframe" src="/gist/mesuk/ae8c398ad59ab4d9bccf3ac5390eb8a5.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="5827"><b>FROM: </b>We are building this image using openjdk:8 alpine image. JDK has many more images, we are using this version. You can check more images from the docker hub link

<b>ARG: </b>We are taking jar path as ARG variable. Here our jar file is in <b>build/libs/springreadyapp.jar </b>. ARG is only available during the build of a Docker image (RUN etc), not after the image is created and containers are started from it (ENTRYPOINT, CMD) <b>Copy: </b>Copying this jar file as springreadyapp.jar. We will execute the run command on this jar file with this name <b>ENTRYPOINT: </b>This will be executable to start when the container is booting. We must define them as <i>JSON-Array</i> because we will use an <i>ENTRYPOINT</i> in combination with a <i>CMD</i> for some application arguments. Here we are passing the jar file run commands</p><p id="91f3"><b>4. Docker various operations </b>We have to follow below steps</p><ul><li>build the project using the Gradle command and Jar creation.</li><li>Build docker image based on our jar file</li><li>Run container using provided docker image</li><li>Delete docker container and docker image</li></ul><p id="cb8a"><b>Build project </b>Using gradlew command you can clean and build the project. sometimes it shows an executable permission error. In this case, you have to allow executable permission to gradlew. Its command is given below:</p><div id="514f"><pre><span class="hljs-built_in">chmod</span> 777 gradlew ./gradlew clean build -x <span class="hljs-built_in">test</span></pre></div><figure id="01b6"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*nTIO4HEyKay65ZMlMpIePQ.png"><figcaption></figcaption></figure><p id="4980">Now the jar file is ready. We can start building an image based on our docker file</p><p id="c818"><b>Building docker image </b>The <code>docker build</code> command builds Docker images from a Dockerfile and a “context”. A build’s context is the set of files located in the specified <code>PATH</code> or <code>URL</code>. Using this command it compacts the image with specified instructions and files of that context. In our case, we will build an image from OpenJDK 8 version, copy the jar from the libs folder and add a command for execution. Here is the build command with tag:</p><div id="a5a8"><pre><span class="hljs-keyword">Build</span> Image docker image <span class="hljs-keyword">build</span> -t springreadyapp . <span class="hljs-keyword">View</span> Images docker images</pre></div><p id="c172">Here, we are building a docker image with tag <b>springreadyapp. </b>Your application has been compiled, packaged, and converted to a Docker image. The output is like bellow</p><figure id="70a9"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*T4ixamD4m9_yjNGz7BoMcw.png"><figcaption></figcaption></figure><p id="8c15">Here our image build is done and image id is : <b>c7b3be12d247</b></p><p id="588b"><b>Docker container run from image </b>Now we have to run a container from our image <b>c7b3be12d247. </b>To do this we have to do port mapping also. Here is the command</p><div id="1a71"><pre><span class="hljs-built_in">Run</span> command docker <span class="hljs-built_in">run</span> -d -p 8081:8081 c7b3be12d247</pre></div><div id="9e65"><pre><span class="hljs-keyword">Container</span> List docker <span class="hljs-keyword">container</span> ps</pre></div><p id="3db7">here, -d means detach mode, and <b>-p 8081:8081</b> means external port is 8081 is mapped with internal port 8081. Here, you must ensure that the external port must be free. Here, it’s important to define the port mapping, which maps a

Options

port on the host (8081) to the port inside Docker (8081), which is the port we have defined in the properties of the Spring Boot application. The output is like below</p><figure id="153d"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*IBGcSfK-jzvr_gIkLl4lSQ.png"><figcaption></figcaption></figure><p id="e84f">The container is running and container id is : <b>c7b3be12d247.</b></p><p id="b383"><b>5. Monitor docker insights </b>We can monitor the docker container insights using both command line and docker desktop UI tools.</p><div id="92fe"><pre>Insights the docker container docker exec -it 804c6b86d49a /bin/sh Using exec command you entered the docker containe. Now you can<span class="hljs-built_in"> execute </span>all your necessary command here</pre></div><figure id="4b51"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*xIeUUVgW039DrUFPOM84_A.png"><figcaption></figcaption></figure><p id="c1e7">You can also achieve this using Docker desktop. It is like bellow image</p><figure id="42bc"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*mlbFrmmMG5DZhorB39H2NQ.png"><figcaption></figcaption></figure><p id="a1f6">Now your application is running and it is running on eternal port 8081. you can check the output from here</p><figure id="f17d"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*-3vAj1_5a6npbQni8LdeYg.png"><figcaption></figcaption></figure><p id="a141"><b>Container and image control</b></p><div id="1db4"><pre>Docker <span class="hljs-built_in">container</span> stop docker <span class="hljs-built_in">container</span> stop <span class="hljs-number">35</span>ce00ba436f Here, <span class="hljs-number">35</span>ce00ba436f <span class="hljs-keyword">is</span> the <span class="hljs-built_in">container</span> id Docker <span class="hljs-built_in">container</span> removed docker <span class="hljs-built_in">container</span> rm -f <span class="hljs-number">35</span>ce00ba436f Here, -f means forcefully removing the <span class="hljs-built_in">container</span>. This <span class="hljs-keyword">is</span> optional</pre></div><figure id="6153"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*96k-8F8Ne4IuoxC0WeFoOg.png"><figcaption></figcaption></figure><p id="f8b7"><b>Docker Image Delete</b></p><p id="92da">docker image rm <b>c7b3be12d247</b> Here, <b>c7b3be12d247 </b>is docker image id.</p><figure id="c8c0"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*EBQMY93nIqvbBijkBM9b8A.png"><figcaption></figcaption></figure><p id="ca40"><b>6. Advance part: Automotion </b>Now you may want to docker image build, container run, and push images to docker hub using Gradle command in an gradle project. We will use the <a href="https://plugins.gradle.org/search?term=com.palantir.docker"><i>Palantir Docker Plugin</i></a><i> </i>for docker automation. To achieve this we have to dome steps on our build.gradle file. Here is the gradle file</p> <figure id="1942"> <div> <div>

            <iframe class="gist-iframe" src="/gist/mesuk/93c7c1fda9978c34608c7b8f7d3a1db4.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="e95d">Here I added only the automation-related changes. We require docker and docker-run both plugins. In <b>docker</b> closure, I added the Dockerfile path and file access instructions. In <b>dockerRun </b>closure, I added instructions for running the docker container from docker images.</p><div id="ce1b"><pre>Docker push image <span class="hljs-keyword">to</span> docker hub

It’s <span class="hljs-keyword">command</span> <span class="hljs-keyword">is</span> : ./gradlew dockerPushDockerHub Using this <span class="hljs-keyword">command</span>, this <span class="hljs-keyword">new</span> <span class="hljs-keyword">version</span> of image will push <span class="hljs-keyword">to</span> docker hub account. Here, my repository path <span class="hljs-keyword">is</span> <span class="hljs-keyword">tag</span> <span class="hljs-string">'DockerHub'</span>, <span class="hljs-string">"mesuk08/springreadyapp:${project.version}"</span> in docker closure</pre></div><p id="ec02">During docker push/pull operation from the command line, you may need to log in and log out from the terminal. Login and logout command is below</p><div id="a8e0"><pre>docker <span class="hljs-built_in">logout</span> docker login</pre></div><p id="961f"><b>How to use docker image </b>Other users will pull the image with tag number from docker hub and just run that image into their premises. Now here our tag is : 0.7 It’s command is like : <b>docker pull mesuk08/springreadyapp:0.7</b></p><figure id="0ee9"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*8AJdMhDmuB_A7TnWbtGtfw.png"><figcaption></figcaption></figure><p id="2879">You can get full source code in this <a href="https://github.com/mesuk/SpringReadyApp">GitHub repository</a>. <b>Swagger URL</b> : <a href="http://localhost:8081/springreadyapp/swagger-ui.html#/">http://localhost:8081/springreadyapp/swagger-ui.html#/</a></p><p id="845a">Thanks for reading :)</p></article></body>

Dockerizing Spring App & Automating Docker with Gradle

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers.

Photo by Dominik Lückmann on Unsplash

Today we will cover bellow things

  1. The importance of docker
  2. Overview on docker
  3. Dockerizing spring app with detailed explanations
  4. Docker various operations eg: Image creation and deletion, container creation and deletion and so more
  5. Monitor Docker images using command line and docker desktop apps
  6. Advance part: Automotion of docker task eg: docker build, run and push to docker hub from Gradle command

Prerequisites: Before starting this article you must have knowledge of spring boot with Gradle dependencies. You can learn this from the below link

  1. The importance of docker
  • is a tool designed to make it easier to create, deploy, and run applications by using containers.
  • Containers are great for continuous integration and continuous delivery (CI/CD) workflows.
  • Docker’s portability and lightweight nature also make it easy to dynamically manage workloads, scaling up or tearing down applications and services as business needs dictate, in near real-time.
  • Docker is lightweight and fast. It provides a viable, cost-effective alternative to hypervisor-based virtual machines, so you can use more of your compute capacity to achieve your business goals.
  • Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package. So the developer can rest assured that the application will run on any other Linux machine regardless of any customized settings that the machine might have that could differ from the machine used for writing and testing the code.
  • Docker is a bit like a virtual machine. But unlike a virtual machine, rather than creating a whole virtual operating system, Docker allows applications to use the same Linux kernel as the system that they’re running on and only requires applications to be shipped with things not already running on the host computer. This gives a significant performance boost and reduces the size of the application.
  • importantly, Docker is open source. This means that anyone can contribute to Docker and extend it to meet their own needs if they need additional features that aren’t available out of the box.

2. Overview on docker Docker is a software platform for building applications based on containers — small and lightweight execution environments that make shared use of the operating system kernel but otherwise run in isolation from one another. Originally built for Linux, Docker now runs on Windows and macOS as well. To understand how Docker works, let’s take a look at some of the components you would use to create Docker-containerized applications.

Docker ecosystem and architecture

I am explaining the basic terms

Docker Image:

  • A read-only template with instructions for creating a Docker container.
  • The filename is Dockerfile and placed in the code folder
  • Contains instructions and image related files
  • For our purpose, it will describe our java dependency and a war file and its run commands.
  • It manages the version and it pushed to Registry like docker hub

Docker container

  • A container is a runnable instance of an image
  • You can create, start, stop, move, or delete a container using the Docker API or CLI
  • When a container is removed, any changes to its state that are not stored in persistent storage disappear.
  • We build a container from the docker image and run this container in a specific port. Port mapping required for these steps

Docker Registry

  • stores Docker images with appropriate versions
  • All renowned applications must have entries in the registry
  • Docker Hub is a public registry that anyone can use, and Docker is configured to look for images on Docker Hub by default.
  • You can even run your own private registry.
  • docker push and pull command are used to push and pull images from the public registry’s

The Docker daemon

  • listens for Docker API requests and manages Docker objects
  • A daemon can also communicate with other daemons to manage Docker services.

Docker Client

  • a primary way that many Docker users interact with Docker
  • It takes clients command and sends it to the Docker daemon
  • Can communicate with multiple daemons

3. Dockerizing spring app with detailed explanations For dockerizing spring boot applications, we won’t do extra things. We just add a file named Dockerfile into the home directory. This file will contain all instructions for running this application

Dockerfile location and code

Our docker file code is

FROM: We are building this image using openjdk:8 alpine image. JDK has many more images, we are using this version. You can check more images from the docker hub link ARG: We are taking jar path as ARG variable. Here our jar file is in build/libs/springreadyapp.jar . ARG is only available during the build of a Docker image (RUN etc), not after the image is created and containers are started from it (ENTRYPOINT, CMD) Copy: Copying this jar file as springreadyapp.jar. We will execute the run command on this jar file with this name ENTRYPOINT: This will be executable to start when the container is booting. We must define them as JSON-Array because we will use an ENTRYPOINT in combination with a CMD for some application arguments. Here we are passing the jar file run commands

4. Docker various operations We have to follow below steps

  • build the project using the Gradle command and Jar creation.
  • Build docker image based on our jar file
  • Run container using provided docker image
  • Delete docker container and docker image

Build project Using gradlew command you can clean and build the project. sometimes it shows an executable permission error. In this case, you have to allow executable permission to gradlew. Its command is given below:

chmod 777 gradlew
./gradlew clean build -x test

Now the jar file is ready. We can start building an image based on our docker file

Building docker image The docker build command builds Docker images from a Dockerfile and a “context”. A build’s context is the set of files located in the specified PATH or URL. Using this command it compacts the image with specified instructions and files of that context. In our case, we will build an image from OpenJDK 8 version, copy the jar from the libs folder and add a command for execution. Here is the build command with tag:

Build Image
docker image build -t springreadyapp .
View Images
docker images

Here, we are building a docker image with tag springreadyapp. Your application has been compiled, packaged, and converted to a Docker image. The output is like bellow

Here our image build is done and image id is : c7b3be12d247

Docker container run from image Now we have to run a container from our image c7b3be12d247. To do this we have to do port mapping also. Here is the command

Run command
docker run -d -p 8081:8081 c7b3be12d247
Container List
docker container ps

here, -d means detach mode, and -p 8081:8081 means external port is 8081 is mapped with internal port 8081. Here, you must ensure that the external port must be free. Here, it’s important to define the port mapping, which maps a port on the host (8081) to the port inside Docker (8081), which is the port we have defined in the properties of the Spring Boot application. The output is like below

The container is running and container id is : c7b3be12d247.

5. Monitor docker insights We can monitor the docker container insights using both command line and docker desktop UI tools.

Insights the docker container
docker exec -it 804c6b86d49a /bin/sh
Using exec command you entered the docker containe. Now you can execute all your necessary command here

You can also achieve this using Docker desktop. It is like bellow image

Now your application is running and it is running on eternal port 8081. you can check the output from here

Container and image control

Docker container stop
docker container stop 35ce00ba436f
Here, 35ce00ba436f is the container id
Docker container removed
docker container rm -f 35ce00ba436f
Here, -f means forcefully removing the container. This is optional

Docker Image Delete

docker image rm c7b3be12d247 Here, c7b3be12d247 is docker image id.

6. Advance part: Automotion Now you may want to docker image build, container run, and push images to docker hub using Gradle command in an gradle project. We will use the Palantir Docker Plugin for docker automation. To achieve this we have to dome steps on our build.gradle file. Here is the gradle file

Here I added only the automation-related changes. We require docker and docker-run both plugins. In docker closure, I added the Dockerfile path and file access instructions. In dockerRun closure, I added instructions for running the docker container from docker images.

Docker push image to docker hub
It’s command is : ./gradlew dockerPushDockerHub
Using this command, this new version of image will push to docker hub account. 
Here, my repository path is 
tag 'DockerHub', "mesuk08/springreadyapp:${project.version}" in docker closure

During docker push/pull operation from the command line, you may need to log in and log out from the terminal. Login and logout command is below

docker logout
docker login

How to use docker image Other users will pull the image with tag number from docker hub and just run that image into their premises. Now here our tag is : 0.7 It’s command is like : docker pull mesuk08/springreadyapp:0.7

You can get full source code in this GitHub repository. Swagger URL : http://localhost:8081/springreadyapp/swagger-ui.html#/

Thanks for reading :)

Docker
Spring Boot
Gradle
Dockerfiles
Docker Image
Recommended from ReadMedium