dockerized postgres and dockerized Spring boot app - spring

my application.properties file
server.port=8085
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://init-postgres:5432/dbname
spring.datasource.username=username
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
and for dockerizing postgres I'm using command
docker run -d -p 5432:5432 --name init-postgres -e POSTGRES_DB=dbname -e POSTGRES_USER=username -e POSTGRES_PASSWORD=password postgres
but it gives java.net.UnknownHostException: init-postgres
I'm beginner with Docker and learning it from a tutorial. to dockerized Postgresql & Spring boot app communication.

If you need to dockerize both of them without docker-compose
application.config
spring.datasource.url=jdbc:postgresql://init-postgres:5432/dbname
Create network
docker network create mynet
Run postgres container with created network
docker run --net mynet --name init-postgres -d -e POSTGRES_DB=dbname -e POSTGRES_USER=username -e POSTGRES_PASSWORD=password -p 5432:5432 postgres
Create jar archive
mvn clean
mvn compile
mvn package
Create dockerfile
FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/<HERE_IS_NAME_OF_YOUR_JAR_FILE>.jar
COPY ${JAR_FILE} myapp.jar
EXPOSE 8085
ENTRYPOINT ["java","-jar" , "/myapp.jar"]
Build spring boot image myapp
docker build -t myapp .
Run spring boot container
docker run --name myapp-container --net mynet -p 8080:8080 myapp

If your application runs on the host without docker and your database lives inside a docker container, you need to change this line:
spring.datasource.url=jdbc:postgresql://init-postgres:5432/dbname
with
spring.datasource.url=jdbc:postgresql://localhost:5432/dbname
instead, if both the application and the database are running on docker you need to refer with the docker container name, as you stated above in the snippet you posted.
I suggest to use docker-compose, it's a handy tool that can ease the difficulties of deployment and it's useful when developing too since it allows to bring up & down your application without too much hassle. In the official docker website there is a nice introduction to the tool with examples.

Related

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.

Dockerized Spring boot app connect to database docker image

I have a basic spring boot application with gradle which makes calls to an Oracle database and the database properties are specified in an application.properties file.
I created a Docker image of the spring boot application with the plugin "com.google.cloud.tools.jib" and using the following command:
./gradlew jibDockerBuild --image=app1
I have a docker-compose file in which i specify the image as an service and i want the application to start when i run the command: "docker-compose up"
The docker-compose file is the following:
version: '3'
services:
app1:
image: "app1"
ports:
- "8731:8731"
But when I hit the run the "docker-compose up" command in CMD I recieve the following exception:
java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection
More informations:
My Oracle database is a docker container with the name : "ORA12201_1" and port 3769
Inside the application.properties the database properties specified are correct since I am amble to start the application from IntelliJ
you can connect from IntelliJ without problem as the container exposes the port (3769) to the host (your PC), but now you are trying to connect from one Docker container to another.
The containers do not share the network (isolation) so you need to connect them.
One of the recommended approach is User-defined networks
Create first a network
docker network create --driver bridge my_network
Run the applications
docker run -p 5432:5432 --network my_network -d --name=postgres postgres
docker run -p 5050:80 --network my_network -d --name=pgadmin dpage/pgadmin4
You can verify they are effectively running on the same network with
docker network inspect my_network
Spring Boot config
You can now connect from one to another using host.docker.internal as hostname, for example in your Spring Boot application.properties
spring.datasource.url=jdbc:postgresql://host.docker.internal:5432/postgres

Why does my Spring Boot App hang when started in a Docker Swarm

I created a docker image of a Spring Boot app and when I run it using
docker run -d -p 4000:8080 stanlick/stats:v1
It works great! However, when I try to run it as a swarm (even with replicas:1) it hangs on startup.
Running
docker service logs play-ball_web
reveals:
Dockerfile:
FROM openjdk:12-jdk-alpine
VOLUME /tmp ARG JAR_FILE
COPY target/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

how to run docker with keycloak image as a daemon in prod environment

I am using docker to run my Keycloak server in aws production environment. The problem is keycloak uses wildfly which is constant running. Because of this I cannot close the shell. I am trying to find a way to run docker as a daemon thread.
The command I use to run docker
docker run -p 8080:8080 jboss/keycloak
Just user docker's detach option -d.
docker run -p 8080:8080 -d jboss/keycloak

Passing env variables to DOCKER Spring Boot

I have a SpringBoot application and its Dockerfile is as follows. I have application.properties for different environments like local/dev/qa/prod. When I run the application locally in IDE, I pass -Dspring.profiles.active=local in VM options so that it loads the application-local.properties. For running as docker containers, I build an image which comprises of all the application.properties. i.e. it's only SAME docker image for all the environments.
When I run the image in an environment, I want to somehow make the SpringBoot to understand that its dev env, so it has to load application-dev.properties. I am using AWS ECS for managing the containers.
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD target/sample-test-sb-sample-app-1.0-exec.jar app.jar
EXPOSE 8080
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
The easiest (and probably the best way) to do it via environment variable in a docker container:
SPRING_PROFILES_ACTIVE=dev,swagger
UPDATE:
In order to set environment variables to docker, you do not need to modify Dockerfile. Just build your docker image and then run it with the env variables set:
docker run your-docker-container -e SPRING_PROFILES_ACTIVE='dev,swagger' -p 8080:8080
In the .Dockerfile file:
ENTRYPOINT [ "sh", "-c", "java -Dspring.profiles.active=**${ENV}** -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
And while running the docker:
docker run --env ENV=*local* -d -p 8080:8080 <*image id*>
This way, the environment variable gets local as value and passes to Dockerfile when we bring up a container.
Update
You can also do like
ENTRYPOINT ["java","-jar", "-Dspring.profiles.active=${ENV} -Djava.security.egd=file:/dev/./urandom","app.jar"]
and while docker image
docker run --env ENV=local -d -p 8080:8080 <*image id*>

Resources