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