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

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.

Related

Connecting to a Mongo container from Spring container

I have a problem here that I really cannot understand. I already saw few topics here with the same problem and those topics was successfully solved. I basically did the same thing and cannot understand what I'm doing wrong.
I have a Spring application container that tries to connect to a Mongo container through the following Docker Composer:
version: '3'
services:
app:
build: .
ports:
- "8080:8080"
links:
- db
db:
image: mongo
volumes:
- ./database:/data
ports:
- "27017:27017"
In my application.properties:
spring.data.mongodb.uri=mongodb://db:27017/app
Finally, my Dockerfile:
FROM eclipse-temurin:11-jre-alpine
WORKDIR /home/java
RUN mkdir /home/java/bar
COPY ./build/libs/foo.jar /home/java/bar/foo.jar
CMD ["java","-jar", "/home/java/bar/foo.jar"]
When I run docker compose up --build I got:
2022-11-17 12:08:53.452 INFO 1 --- [null'}-db:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server db:27017
Caused by: java.net.UnknownHostException: db
Running the docker compose ps I can see the mongo container running well, and I am able to connect to it through Mongo Compass and with this same Spring Application but outside of container. The difference running outside of container is the host from spring.data.mongodb.uri=mongodb://db:27017/app to spring.data.mongodb.uri=mongodb://localhost:27017/app.
Also, I already tried to change the host for localhost inside of the spring container and didnt work.
You need to specify MongoDB host, port and database as different parameters as mentioned here.
spring.data.mongodb.host=db
spring.data.mongodb.port=27017
spring.data.mongodb.authentication-database=admin
As per the official docker-compose documentation the above docker-compose file should worked since both db and app are in the same network (You can check if they are in different networks just in case)
If the networking is not working, as a workaround, instead of using localhost inside the spring container, use the server's IP, i.e, mongodb://<server_ip>:27017/app (And make sure there is no firewall blocking it)

How to connect my spring boot app to redis container on docker?

I have created a spring app and i want to connect it to redis server which is deployed on docker-compose i put the needed properties as follow :
spring.redis.host=redis
spring.redis.port=6379
But i keep getting a ConnexionException so how can i Know on which host redis is running and how to connect to it.
Here is my docker-compose file :
version: '2'
services:
redis:
image: 'bitnami/redis:5.0'
environment:
# ALLOW_EMPTY_PASSWORD is recommended only for development.
- ALLOW_EMPTY_PASSWORD=yes
- REDIS_DISABLE_COMMANDS=FLUSHDB,FLUSHALL
ports:
- '6379:6379'
volumes:
- 'redis_data:/bitnami/redis/data'
volumes:
redis_data:
driver: local
From docker compose documentation
By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name
If you want to access redis by container name ('redis' in this case), the Spring boot application has also be deployed as a docker compose service, but it doesn't appear in the docker-compose file that you've provided in the question, so please add it.
Alternatively If you're trying to run the spring boot application on host machine, use 'localhost' instead of 'redis' in order to access the redis container.
Another approach you can use is "docker network" , Below are the steps to follow :
Create a docker network for redis
docker network create redis-docker
Spin up redis container is redis-docker network.
docker run -d --net redis-docker --name redis-stack -p 6379:6379 -p 8001:8001 redis/redis-stack:latest
Inspect the redis-docker container
docker network inspect redis-docker
Copy the "IPv4Address" IP and paster in application.yml
Now build , start your application.

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

dockerized postgres and dockerized Spring boot app

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.

How to deploy Spring Boot RESTful Web Service Docker img to EC2?

What I'm trying to do is simple, deploy a Spring Boot RESTful Web Service to EC2 so it's accessible publicly.
For this I need to do the following:
Write Spring Boot web service, containerize and test locally - done
Here is my Dockerfile:
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
ADD ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
When I docker run this locally on 8080 it works fine (returns static json).
Push to Dockerhub - done
Launch an Amazon Linux AMI on aws and make it http accessible (port 80) - done
Install apache (httpd) and start - done
Here is where I need some help
I run docker image from dockerhub like so
docker run --rm -p 80:8080 kaspartr/demo
It doesn't allow cause of course the port is taken by apache. And if I stop it and run it is deployed but I cannot access it online.
Can someone please explain how do you deploy docker image into the apache?
Do I need to change the Dockerfile or something else?
Thank you!!
Typically I run application on separate port and do docker forward:
Add yo you application.properties
server.port=9001
And add to docker-compose.yml:
version: '1'
services:
your-service:
build: .
ports:
- '9001:9001'
environment:
SERVICE_URL: http://service:9001/path
Since Apache has already taken port 80, you cannot make your container runs on port 80 too.
My guess is that you are planning to use Apache as a reverse proxy to your container. Try publishing your container to another port, and proxying port 80 to the container using Apache.

Resources