Spring boot + Docker Compose + Env file - spring

I am trying to set multiple profiles for spring boot via docker using an env file like so
.env file
ENV=docker,test
docker-compose
environment:
- spring.profiles.active=${ENV}
this seems to cause spring boot to view docker,test as one string if I don't load the env var it works like this
docker-compose
environment:
- spring.profiles.active=docker,test
any ideas on how to fix this?

figure out I can do this env_file: .env

Related

Run spring boot docker image with a different configuration in docker compose

Hello I'm starting with docker and docker compose and I have the following problem:
I'm working in a spring micro services architecture where I have one configuration service, one discovery service, one gateway service and multiple resource services.
To run these services, I build jar files, which I place in separated folder per service with their config files (application.yml and bootstrap.yml):
e.g:
config-service/
config-service.jar
application.yml
discovery-service/
discovery-service.jar
bootstrap.yml
gateway-service/
gateway-service.jar
bootstrap.yml
crm-service/
crm-service.jar
bootstrap.yml
This works so far on my server.
Now I want to deploy my services in different environments as docker images (created with mvn build image and buildpack) using docker compose, where the configuration files vary depending on the environment. How can I deploy a service as a container using an existing image but with a different configuration file?
Thank you in advance!
There are a few possibilities when handling configuration in a containerized environment.
One of the options is that Spring boot allows you to use environment variables for each application property. For example, let's say you have a spring.datasource.url property, in that case you could also define that property by setting a SPRING_DATASOURCE_URL environment variable:
version: '3.8'
services:
my-spring-boot-app:
image: my-image:0.0.1
environment:
- SPRING_DATASOURCE_URL=jdbc:my-database-url
Alternatively, you could use volumes to put an external file on a specific location within a container:
version: '3.8'
services:
my-spring-boot-app:
image: my-image:0.0.1
volumes:
./my-app/bootstrap.yml:/etc/my-app/bootstrap.yml
In this example I'm copyingbootstrap.yml from a relative folder on my host machine, to /etc/my-app within the container. If you put these files within the same folder as your JAR file, you can override the configuration.

can't access to a spring boot API based in remote docker

I'm trying to get a connection to a Spring Boot application containerized in a old docker:
My docker version is 1.12.2
Spring Boot 2.5.1
here is my Dockerfile:
FROM openjdk:8-alpine
ADD manager.jar manager.jar
EXPOSE 8443
ENTRYPOINT ["java","-Dspring.profiles.active=dev","-jar","manager.jar"]
I've build the image inside docker with this simple command:
docker build -t manager .
and then run the container:
docker run -p 8443:8443 -t manager manager
But when I'm trying to have a connection by typing the adress:
https://SERVERNAME:8443
...timeout.
Did I make a mistake somewhere?
So like I said in my last post, after remove the TLS properties from the application.properties, everything is solved. It's a server configuration problem.

Spring boot with docker build mvn spring-boot:build-image with jvm parametes

I am trying build docker image for my spring boot docker using the command
mvn spring-boot:build-image
And below is my docker-compose.yml
version: '3.3'
services:
spring-boot-container:
ports:
- '7000:7000'
environment:
- SPRING_PROFILES_ACTIVE=${environment}
- JASYPT_ENCRYPTOR_PASSWORD=${JASYPT_ENCRYPTOR_PASSWORD}
- cloud_config_uri=${cloud_config_uri}
- "JAVA_OPTS=-Dspring.cloud.config.uri=http://localhost:8888"
image: 'artifactory.cloud.health.com/docker-all/spring_boot_app:latest'
restart: always
container_name: spring_boot_app
But my spring boot app is not coming up with proper profiles and not picking java _opts.
Basically in old approach i create dockerfile, then i give ENTRYPOINT where i pass -Dspring.active.profiles.
But since we using mvn spring-boot:build-image i dont know how we pass those entrypoint variables.
Ok, finally resolved, its syntax issue. The below way of writing environment works!
environment:
- JAVA_OPTS=
-Dspring.profiles.active=${environment}
-Dspring.cloud.config.enabled=true
-Dspring.cloud.config.uri=${cloud_config_uri}
-Djasypt.encryptor.password=${JASYPT_ENCRYPTOR_PASSWORD}

It is possibile to send variable from docker env to yml file in spring project?

I want to set variables in my spring project YML configuration from docker environment variable without using Docker Compose.
To use an environment variable in your spring project, first, you need to set in your docker container.
Set ENV in Docker Container:
Docker run -e LOGGING_LEVEL_ROOT="info" -e PROFILE="production" -it your_image
Now, You can use LOGGING_LEVEL_ROOT in your spring project YML configuration.
logging:
level:
root: ${LOGGING_LEVEL_ROOT}
Or
spring:
profiles: ${PROFILE}
Note that, since the application.properties and application.yml files
accept Spring style placeholders (${…​}), the Maven filtering is
changed to use #..# placeholders. (You can override that by setting a
Maven property called resource.delimiter.)
oot-features-external-config-placeholders-in-properties

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. Reason: Fa

I am developing a web application by using the Spring Boot, Spring Cloud Config and Docker. There are 3 projects. One is springboot web, Spring Cloud Config Server and MySql Database. I have use the Spring Boot Web with Themeleaf. It's basically perform a CRUD operation. In the development phase it's working fine. But when I deploy it in the Docker
Spring-Boot webapp container(springboot-thymeleaf-web-crud) is giving me the following error--
APPLICATION FAILED TO START
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
I have created 3 container on Docker Compose file.
version: '3'
services:
springboot-thymeleaf-web-crud:
image: numery/springboot-thymeleaf-web-crud:latest
networks:
- employee-network
ports:
- 8080:8080
depends_on:
- employee-database
- spring-cloud-config-server-employee
spring-cloud-config-server-employee:
image: numery/spring-cloud-config-server-employee:latest
networks:
- employee-network
ports:
- 8888:8888
employee-database:
image: mysql:5
networks:
- employee-network
environment:
- MYSQL_ROOT_PASSWORD=rootpassword
- MYSQL_DATABASE=employee_directory
volumes:
- /home/numery/Docker-Database/employee_directory:/var/lib/mysql
networks:
employee-network:
SpringBoot Web Application.properties is given below
spring.application.name=employee-service-mysql
server.port=8080
spring.cloud.config.uri=http://spring-cloud-config-server-
employee:8888
spring.profiles.active=dev
SpringBoot Config Server is providing the following properties--
spring.datasource.url: jdbc:mysql://employee-
database:3306/employee_directory?
useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
spring.datasource.username: root
spring.datasource.password: rootpassword
spring.datasource.validationQuery: SELECT 1
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.tomcat.max-wait: 20000
spring.tomcat.max-active: 50
spring.tomcat.max-idle: 20
spring.tomcat.min-idle: 15
spring.jpa.properties.hibernate.dialect:
org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql: true
spring.jpa.format-sql: true
spring.jpa.database: MYSQL
spring.jpa.hibernate.ddl-auto: create
Out of these 3 containers Mysql and Spring Cloud Config container is working fine. But for the Spring Boot Webapp(springboot-thymeleaf-web-crud) is exiting by giving the error above.
Note: I use the same datasouce(Spring Data JPA) configuration on some other SpringBoot Rest API. Which are working fine. But this is the first time I am using SpringBoot Web. Do I need to explicitly define any configuration on the data-source.
Please help!!
The datasource URL, that is spring.datasource.url: jdbc:mysql://employee- may be wrong.
OR Should add your datasource url to your Application.properties file
The url should be in the format jdbc:oracle:thin:#hostname:portNumber/schemaName
For further details and clarification, Not using the hostname and port in jdbc url
I perform the following task to solve the problem--
1) We should have up and running the mysql
container.
2) Inspect mysql container which ip running--
docker inspect mysqlcontainer | grep IP
3) Add the IP of that mysql container to my
springboot web app--
"spring.datasource.url:
jdbc:mysql://172.20.0.2:3306/employee_directory?
useSSL=false&serverTimezone
=UTC&useLegacyDatetimeCode=false"
4) run mvn clean, mvn install and build the
SpringBoot Web image.
5) If we down the container we need to perform the
same task again. Because each time Mysql
container obtain the different ip.

Resources