top of page

Docker - Running a containerized Spring boot application

  • Writer: megha dureja
    megha dureja
  • May 4, 2020
  • 3 min read

Updated: Feb 22, 2021



In this blog, we'll package a java/ spring boot application, run it and look at the different commands for managing images and containers.


"Docker is a platform for packaging, deploying, running and scaling applications using containers. It can run containers on developer's laptop or in the cloud."

1) Download and install Docker Community Edition for free.

$ docker --version


2) Write a Dockerfile


First, create the Dockerfile (text document that contains all the commands execute to build a Docker image) and place this file at the top of your project(root folder where pom.xml file resides).

The first step to take when you create a Dockerfile is to access the DockerHub website. It contains many pre-designed images that will help you save.

FROM openjdk:8-jre
ADD HelloWorld.class HelloWorld.class
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "HelloWorld"]

Basics :-

  • FROM defines the base image used to start the build process.

  • ADD copies the files from a source on the host into the container’s own filesystem at the set destination.

  • CMD can be used for executing a specific command within the container.

  • ENTRYPOINT sets a default application to be used every time a container is created with the image.

  • ENV sets environment variables.

  • EXPOSE associates a specific port to enable networking between the container and the outside world.

  • MAINTAINER defines a full name and email address of the image creator.

  • RUN is the central executing directive for Dockerfiles.

  • USER sets the UID (or username) which is to run the container.

  • VOLUME is used to enable access from the container to a directory on the host machine.

  • WORKDIR sets the path where the command, defined with CMD, is to be executed.

  • LABEL allows you to add a label to your docker image.

  • COPY copies multiple source files from the context to the file system of the container at the specified path

A common base image for including JDK in your application is the default

openjdk:latest image.

This image is based on debian operating system(base OS). The size of this image is 640.9 MB. The JDK version can be seen by running the image:-

$ docker run -it openjdk java -version

If you want to use Oracle Jdk, then there is no official Docker image available at Docker hub. So you need to download Oracle JDK and package it in the image. An image built this way is 536.3 MB.

Alpine Linux is a stripped down Linux distribution built around musl libc and BusyBox. Docker hub contains an Alpine-based OpenJDK and can be downloaded as openjdk:alpine. The size of this image is 144.9 MB. This is <25% of the default openjdk:latest image.


Similarly, openjdk:8-jre is 309 MB and openjdk:8-jre-alpine is 107.8 MB.

Make sure to pick JDK or JRE, whatever is appropriate, as the base image.


Each Docker image has its own root filesystem, which needs to have some sort of OS installed. [ base (OS) image + child image (add additional functionalities) ]


3) Build a Docker Image


Once your code is ready with your application, and the Dockerfile is written, all you have to do is create your image to contain your application.

$ docker build -f Dockerfile -t <image_name> .

Docker Image will contain Dockerfile, libraries, and the code your application needs to run, all bundled together.


4) Running a Docker Container


Executing a Docker image with a dedicated below command will create and start a container from that image.

$ docker run <image_name> //runs an image

First, Docker looks for this image on local system, since it is not there, it downloads from docker hub. Then it runs downloaded image in the container, which displays a message telling us everything's working fine, then it spells out the process it took to run the image.


A container contains: A Docker image; An execution environment; A standard set of instructions.


Dockerfile -> Build -> Image -> Run -> Container

--------------------------------------------------------------------------------------------------------------------------


Note:- To get a new Docker image, you can either get it from a registry (such as the DockerHub) or create your own. There are tens of thousands of images available on DockerHub. You can also search for images directly from the command line using docker search. An important distinction to be aware of when it comes to images is the difference between base and child images:


--------------------------------------------------------------------------------------------------------------------------



Build docker image from maven

build docker image locally

mvn clean install

push image to remote repo

mvn clean deploy -Ddocker.user=<username> -Ddocker.password=<passwd> -Ddocker.url=<docker-registry-url>



Issues:-


Error:

Failed to execute goal com.spotify:dockerfile-maven-plugin: build Exception caught: HttpHostConnectException: Connect to localhost:2375

Solution:-

1) set DOCKER_HOST into env variables as tcp://localhost:2375

2) change docker desktop settings -

check "Expose daemon on tcp://localhost:2375 without TLS"

3) restart the docker desktop



Recent Posts

See All

Comentários


Drop Me a Line, Let Me Know What You Think

Thanks for submitting!

© 2023 by Train of Thoughts. Proudly created with Wix.com

bottom of page