I have a Springboot app that I have Dockerized.
I have it exposed on port 8081
, and can access it as expected.
http://<ipaddress>:8081
Problem
The Springboot app in the docker container needs to connect to a postgres database on the same host (not in a container), but it appears like it does not gave access to the host network.
Connection to localhost:5432 refused
Docker cmd:
docker run -t --rm -d -p 8081:8081 --name nexct-approval-service-container nexct-approval-service-image
So I have read in order to connect to the network, you can use:
--network host
However, then it stops allowing access to the application itself (port 8081):
WARNING: Published ports are discarded when using host network mode
Question
How can I allow access to the SpringBoot app on port 8081 and allow the Springboot app access to the host network so it can connect to the database?
UPDATE
My database connection is defined in Spring Boot:
application.properties
spring.datasource1.driver-class-name=org.postgresql.Driver
spring.datasource1.jdbc-url=jdbc:postgresql://localhost:5432/pims
spring.datasource1.username=postgres
spring.datasource1.password=
MultipleDBConfig.java
@Configuration
@ComponentScan(basePackages = "com.nexct")
public class MultipleDBConfig {
@Bean(name = "datasource1")
@ConfigurationProperties("spring.datasource1")
@Primary
public DataSource dataSource1(){
return DataSourceBuilder.create().build();
}
@Bean(name = "datasource2")
@ConfigurationProperties("spring.datasource2")
public DataSource dataSource2(){
return DataSourceBuilder.create().build();
}
}
If you use the host network mode for a container, that container's network stack is not isolated from the Docker host (the container shares the host's networking namespace), and the container does not get its own IP-address allocated.
Docker network host, also known as Docker host networking, is a networking mode in which a Docker container shares its network namespace with the host machine. To access the application inside the container, use the port at the host's IP address (e.g., port 80).
Published portsThis creates a firewall rule which maps a container port to a port on the Docker host to the outside world. Here are some examples. Map TCP port 80 in the container to port 8080 on the Docker host. Map TCP port 80 in the container to port 8080 on the Docker host for connections to host IP 192.168.1.100.
You can expose a port through your Dockerfile or use --expose and then publish it with the -P flag. This will bind the exposed port to your Docker host on a random port (verified by running docker container ls ). You can expose a port through your Dockerfile or use --expose and then publish it with the -p 80:80 flag.
In my service, I replaced the references to localhost
with host.docker.internal
, and I was able to publish the container's ports with -p
and connect to services on localhost without having to use --network host
. My services references a .env file that has the hostnames, so I just created another .env file with the updated hostname.
You should change : localhost
in connection string to :
172.17.0.1
its an IP address of containers network
Then check again.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With