title: Docker WARNING: Published ports are discarded when using host network mode
body: <p>I have a Springboot app that I have Dockerized.
  I have it exposed on port <code>8081</code>, and can access it as expected.</p>
  <pre><code>http://&lt;ipaddress&gt;:8081
  </code></pre>
  <p><strong>Problem</strong></p>
  <p>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.</p>
  <blockquote>
  <p>Connection to localhost:5432 refused</p>
  </blockquote>
  <p>Docker cmd:</p>
  <pre><code>docker run -t --rm -d -p 8081:8081 --name nexct-approval-service-container nexct-approval-service-image
  </code></pre>
  <p>So I have read in order to connect to the network, you can use:</p>
  <pre><code>--network host 
  </code></pre>
  <p>However, then it stops allowing access to the application itself (port 8081):</p>
  <pre><code>WARNING: Published ports are discarded when using host network mode
  </code></pre>
  <p><strong>Question</strong></p>
  <p>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?</p>
  <hr />
  <p><strong>UPDATE</strong></p>
  <p>My database connection is defined in Spring Boot:</p>
  <p><strong>application.properties</strong></p>
  <pre><code>spring.datasource1.driver-class-name=org.postgresql.Driver
  spring.datasource1.jdbc-url=jdbc:postgresql://localhost:5432/pims
  spring.datasource1.username=postgres
  spring.datasource1.password=
  </code></pre>
  <p><strong>MultipleDBConfig.java</strong></p>
  <pre><code>@Configuration
  @ComponentScan(basePackages = &quot;com.nexct&quot;)
  public class MultipleDBConfig {
  
      @Bean(name = &quot;datasource1&quot;)
      @ConfigurationProperties(&quot;spring.datasource1&quot;)
      @Primary
      public DataSource dataSource1(){
          return DataSourceBuilder.create().build();
      }
  
      @Bean(name = &quot;datasource2&quot;)
      @ConfigurationProperties(&quot;spring.datasource2&quot;)
      public DataSource dataSource2(){
          return DataSourceBuilder.create().build();
      }
  }
  </code></pre>