Dockerizing Springboot Application

Dockerizing Springboot Application

Docker is a powerful tool that allows developers to package their applications into containers that can be easily deployed and run on any platform.?This package includes everything needed to run it including the code, the runtime, the libraries, tools, environments, and configurations.They are called?docker images and a running instance of a docker image is called a?docker container.

Moreover, you can run multiple containers of completely different configurations on the same infrastructure. All containers are completely isolated and run independently from each other.

It’s important to keep the size of the Docker image as small as possible. A smaller image size has several advantages, such as faster image transfer times, lower storage requirements, and faster container startup times.

Creat docker file --> create image

1- Choose the right base image for your application. OpenJDK base image is recomemnded for springboot applications

2- Build slim image . In a multi-stage build, you use multiple?FROM?instructions to define different stages in the build process. Each stage can have its own set of instructions and dependencies, and the final image only includes the files and dependencies from the last stage.

3- Use environment variables allows to change the configuration of application without having to rebuild the Docker image. Spring Boot applications can use application.properties?or?application.yml?file to specify configuration properties. These properties can be overridden at runtime using environment variables, which Spring Boot automatically maps to properties.

4- Give command specified in the?ENTRYPOINT?instruction.

Eventually a docker file may look like :

# we will use openjdk 8 with alpine as it is a very small linux distro
FROM openjdk:11

ENV SPRING_PROFILES_ACTIVE=production

# copy the packaged jar file into our docker image
EXPOSE 9000

ADD target/my-application.jar /my-application.jar

# set the startup command to execute the jar
ENTRYPOINT ["java", "-jar", "/app.jar"]n        

5- To build docker image

docker build -t my-application:latest .        

Creating docker compose --> create apps containers

Use Docker Compose to define your application’s?services?and?dependencies. it allows you to define application’s?services,?networks, and?volumes?in a single file, making it easy to manage and deploy your application . An example below to create db service for springboot application.

version: '3.7

services:
? app:
? ? image: 'my-application:latest'
? ? build:
? ? ? context: .
? ? container_name: app
? ? ports:
? ? ? - '9000:9000'
? ? depends_on:
? ? ? - postgresdb


? postgresdb:
? ? image: 'postgres:latest'
? ? container_name: postgresdb
? ? ports:
? ? ? - '5432:5432'
? ? environment:
? ? ? - POSTGRES_USER=postgres
? ? ? - POSTGRES_PASSWORD=admin
? ? volumes:
? ? ? # copy the sql script to create tables
? ? ? - ./create_schema.sql:/docker-entrypoint-initdb.d/create_schema.sql'        

Finally , Run docker compose to start up your application . you will have app and db conatiners up and running.

docker-compose up        

要查看或添加评论,请登录

Marwa Ali的更多文章

  • Spring Security 6 with Spring Boot 3 + KeyCloak

    Spring Security 6 with Spring Boot 3 + KeyCloak

    What is KeyCloak ? KeyCloak Open Source Identity and Access Management.It provides user federation, strong…

    1 条评论
  • Spring Security 6 with Spring Boot 3 + JWT

    Spring Security 6 with Spring Boot 3 + JWT

    In continuation to my article Spring security 6 and spring boot 3 , Next introducing JWT token. Learn Jwt token here .

  • Spring Security 6 with Spring Boot 3

    Spring Security 6 with Spring Boot 3

    Say goodbye to Old security , Say Hi to Spring Security 6 with Spring Boot 3 . it is easier and simpler.

  • SpringBoot batch framework

    SpringBoot batch framework

    Spring Batch is a lightweight, comprehensive batch framework designed to enable the development of robust batch…

  • Kafka Event sourcing in Event Driven Architecture

    Kafka Event sourcing in Event Driven Architecture

    What is Event Sourcing ? Event Sourcing is ensuring every change to the state of an application is captured in an event…

  • Istio addons

    Istio addons

    #devops #istio #grafana #promtheus #servicemesh Please see my previous artcile at Grafana An open source monitoring…

  • Istio service mesh

    Istio service mesh

    #devops #kubernets #istio #servicemesh What is a service mesh? Developers and operators face chanllenges with a…

  • Springboot Distributed State Machine

    Springboot Distributed State Machine

    #statemachine What is a distributed state? An application may exist in a finite number of states. when something…

  • Microservices Saga Pattern with Spring State machine

    Microservices Saga Pattern with Spring State machine

    What are sagas in microservices ? A database-per-microservice model provides many benefits for microservices…

  • SpringBoot State machine

    SpringBoot State machine

    The concept of a state machine is most likely older than any reader of this reference documentation and definitely…

社区洞察

其他会员也浏览了