KItis
KItis

Reputation: 5646

Create docker image with named/host volume for spring boot application

I have spring boot application which I am trying to dockerize for the first time. I am using docker version 20.10.1 and my host pc is ubuntu 20.04

for this spring boot application, I have a data directory , which has data created when the application is running. I want to access this data from the host operating system. That is why I am using volume.

When I try to mount my container to named volume or to a host volume, but it always create anonymous volume regardless of the command I type.

Here is my docker file.

FROM openjdk:15
COPY target/lib/* /usr/src/app/lib/
COPY target/core-api-7.3.6.jar /usr/src/app/lib/core-api-7.3.6.jar
COPY config/application.properties  /usr/src/app/config/application.properties
COPY data/poscms/config/*  /usr/src/app/data/poscms/config/
WORKDIR /usr/src/app
ENTRYPOINT ["java", "-jar", "lib/core-api-7.3.6.jar"]
VOLUME /usr/src/app/data

/usr/src/app/data this is the directory where core-app.jar application will create its runtime data, I need to access these data from my host pc

Following is the command for building the image

docker build -t core-app:5.0 .

then I create image using following command

docker run -it -d -p 7071:7071 core-app:5.0 -v /home/bob/data/:/usr/src/app/data

when I check the volumes by running following command

docker volume ls

I can see anonymous volume being created by this container

and my host path which is /home/kapila/data/ is empty and container data is not written to host path.

I experience the same behaviour with named volume as well.

I created a named volume using following command

docker volume create tmp

docker run -it -d -p 7071:7071 core-app:5.0 -v tmp:/usr/src/app/data

and still docker create anonymous volume and data is not written to tmp volume

my host PC is ubuntu pc. Could someone point out what I am doing wrong here

Upvotes: 0

Views: 2379

Answers (2)

KItis
KItis

Reputation: 5646

I was able to fix the issue, and only change I did, to make it work, is that, to change the base image from

FROM openjdk:15

to

FROM adoptopenjdk/openjdk15:ubi

and now named and host volume mounts are working as expected. I am not sure what is wrong with official openjdk:15 image.

Upvotes: 0

Alferd Nobel
Alferd Nobel

Reputation: 3949

I do something like this:

  • In your project root , have these files pertaining to docker as required:

    1.  DockerFile 2.docker-compose.yml 3. docker-env-preview.env
    

DockerFile content

FROM openjdk:8-jdk-alpine

ARG jarfilepath

RUN mkdir /src

WORKDIR /src

VOLUME /src/tomcat

ADD $jarfilepath yourprojectname.jar

docker-compose.yml content

version: '3'
services:
  project-name:
    container_name: project-name-service
    build:
      context: .
      args:
        jarfilepath: ./target/project-0.0.1.jar
    env_file:
      - docker-env-preview.env
    ports:
      - "8831:8831"
      - '5005:5005'
    networks:
      - projectname_subnet
    command: java -jar -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 projectname.jar --spring.profiles.active=preview
networks:
  project-name_subnet:
    external: true

docker-env-preview.env

This file will contain your environment variables values. The applicaiton.properties can read this file to fetch the values, like buildserver.ip=${BUILD_SERVER_DOMAIN}. Basically you define what you want need . Like the example below.

GARBABE_SERVER_DOMAIN=h-db-preview
GARBABE_SERVER_PORT=5422
GARBABE_DB=projectdb
GARBABE_USER=user
GARBABE_PASSWORD=pwd
JPA_DDL_AUTO=validate
JPA_DIALECT=org.hibernate.dialect.PostgreSQLDialect
JPA_SHOW_SQL=false
JPA_SE_SQL_COMMENTS=true
JPA_FORMAT_SQL=false
JPA_NON_CONTEXTUAL_CREATION=true
APP_NAME=project-name-service
BUILD_SERVER_METHOD=http
BUILD_SERVER_DOMAIN=7.8.9.4

Commands to execute :

  1. mvn clean package (if you use maven )
  2. docker-compose up -d --build ( execute docker ps -> check the details on the running container),
  3. To view the logs : sudo docker logs <project-name-service> -f
  4. To get into the container console, docker exec -it <project-name-service> bash

Upvotes: 1

Related Questions