title: Docker failing to connect to springboot and postgres database running in the same network
body: <p>I want to containerize my spring-boot rest api together with the <code>postgres</code> database. Here is my <code>Dockerfile</code> for the <code>api</code>:</p>
  <pre><code>FROM openjdk:17
  
  WORKDIR /app
  
  COPY ./target/docker-0.0.1-SNAPSHOT.jar ./docker-0.0.1-SNAPSHOT.jar
  
  CMD [&quot;java&quot;,&quot;-jar&quot;,&quot;docker-0.0.1-SNAPSHOT.jar&quot;]
  </code></pre>
  <p>I've created a <code>.jar</code> file using <code>mvm</code>. Here is my <code>application.yml</code> file.</p>
  <pre class="lang-yaml prettyprint-override"><code>
  # postgres sql connection
  spring:
    datasource:
      password: password
      url: jdbc:postgresql://postgresdb:5432/todos
      username: admin
    jpa:
      hibernate:
        ddl-auto: create #create
      properties:
        hibernate:
          dialect: org.hibernate.dialect.PostgreSQLDialect
          format_sql: true
      show-sql: true
  # Configuring the port for our server
  server:
    port: 3001
  </code></pre>
  <p>Here is my <code>docker-compose</code> file:</p>
  <pre class="lang-yaml prettyprint-override"><code>version: &quot;3.9&quot;
  services:
    postgresdb:
      image: postgres:14.4-alpine
      container_name: pgsql
      restart: always
      volumes:
        - ./db/init.sql:/docker-entrypoint-initdb.d/0_init.sql
        - $HOME/database:/var/lib/postgresql/data
      ports:
        - &quot;5432:5432&quot;
      expose:
        - &quot;5432&quot;
      environment:
        POSTGRES_DB: todos
        POSTGRES_USER: admin
        POSTGRES_PASSWORD: password
        SERVICE_NAME: postgresdb
      networks:
        - internalnet
    springboot:
      container_name: springbootserver
      build: .
      image: springboot:1.0
      ports:
        - &quot;3001:3001&quot;
      expose:
        - &quot;3001&quot;
      environment:
        DB_USER: &quot;admin&quot;
        DB_PASSWORD: &quot;password&quot;
      depends_on:
        - postgresdb
      networks:
        - internalnet
  networks:
    internalnet:
      driver: bridge
  </code></pre>
  <p>If i run <code>docker logs springbootserver</code>  I'm getting the following error:</p>
  <pre class="lang-bash prettyprint-override"><code>org.postgresql.util.PSQLException: Connection to postgresdb:5232 refused. Check that the hostname and port are correct and that the postmaster is accepting 
  TCP/IP connections.
  </code></pre>
  <p>What am i missing here?</p>