Docker Compose Unable to obtain connection from database (jdbc:postgresql://db:5432/postgres) for user ‘postgres’: The connection attempt failed

The Problem:

I am using Docker Compose to create two containers: One with a Postgres database on it and the other with Flyway on it. The goal is to use Flyway to migrate scripts to the Postgres database instance. When I run docker-compose up I get the following error:

Unable to obtain connection from database (jdbc:postgresql://db:5432/) for user 'luke_skywalker': The connection attempt failed.

My code is below and thank you for your help!

Here is my docker-compose.yml:

version: "3.7"

services:
  postgres_db:
    image: postgres:11.3
    container_name: postgres_database
    restart: always
    # map host port to default postgres port of the contianer; note host port was randomly chose from ports that are unassinged
    environment:
      POSTGRES_USER: ${postgres_user}
      POSTGRES_PASSWORD: ${postgres_password}
    ports:
      - 11102:5432

  flyway_migration:
    #https://github.com/flyway/flyway-docker
    image: flyway/flyway
    container_name: flyway
    environment:
      FLYWAY_PASSWORD: ${postgres_password}
      FLYWAY_USER:  ${postgres_user}
    volumes:
        #create flyway supported volume in the container that has our migration scripts; https://github.com/flyway/flyway-docker
      - .migration_scripts:/flyway/sql
    #change back to 60
    command: -url=jdbc:postgresql://db:5432/ -connectRetries=1 migrate -X
    depends_on:
      - postgres_db

Here is the full error message:

Attaching to postgres_database, flyway
postgres_database   | 2021-05-27 03:23:59.359 UTC (1) LOG:  listening on IPv4 address "0.0.0.0", port 5432
postgres_database   | 2021-05-27 03:23:59.359 UTC (1) LOG:  listening on IPv6 address "::", port 5432
postgres_database   | 2021-05-27 03:23:59.365 UTC (1) LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_database   | 2021-05-27 03:23:59.385 UTC (23) LOG:  database system was shut down at 2021-05-27 03:19:05 UTC
postgres_database   | 2021-05-27 03:23:59.389 UTC (1) LOG:  database system is ready to accept connections
flyway              | DEBUG: Loading config file: /flyway/conf/flyway.conf
flyway              | DEBUG: Unable to load config file: /flyway/flyway.conf
flyway              | DEBUG: Unable to load config file: /flyway/flyway.conf
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/jna-4.5.2.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/msal4j-1.10.0.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/mssql-jdbc-9.2.1.jre8.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/jna-platform-4.5.2.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/postgresql-42.2.19.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/hsqldb-2.5.1.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/derbyclient-10.15.2.0.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/mysql-connector-java-8.0.24.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/derbytools-10.15.2.0.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/jaybird-jdk18-3.0.10.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/google-cloud-spanner-jdbc-2.0.2.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/aws-secretsmanager-jdbc-1.0.6.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/derbyshared-10.15.2.0.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/snowflake-jdbc-3.13.1.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/jtds-1.3.1.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/derby-10.15.2.0.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/mariadb-java-client-2.7.2.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/google-cloud-storage-1.113.13.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/sqlite-jdbc-3.34.0.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/ojdbc8-19.6.0.0.jar
flyway              | DEBUG: Adding location to classpath: /flyway/drivers/h2-1.4.200.jar
flyway              | DEBUG: Using configuration:
flyway              | DEBUG: flyway.connectRetries -> 1
flyway              | DEBUG: flyway.jarDirs -> /flyway/jars
flyway              | DEBUG: flyway.locations -> filesystem:sql
flyway              | DEBUG: flyway.password -> *************
flyway              | DEBUG: flyway.url -> jdbc:postgresql://db:5432/postgres
flyway              | DEBUG: flyway.user -> luke_skywalker
flyway              | DEBUG: Multiple databases found that handle url 'jdbc:postgresql://db:5432/postgres': CockroachDB, PostgreSQL
flyway              | DEBUG: Scanning for classpath resources at 'classpath:db/callback' ...
flyway              | DEBUG: Determining location urls for classpath:db/callback using ClassLoader java.net.URLClassLoader@18be83e4 ...
flyway              | DEBUG: Unable to resolve location classpath:db/callback.
flyway              | Flyway Community Edition 7.9.1 by Redgate
flyway              | DEBUG: AWS SDK available: false
flyway              | DEBUG: Google Cloud Storage available: false
flyway              | DEBUG: Scanning for filesystem resources at 'sql'
flyway              | DEBUG: Scanning for resources in path: sql (sql)
flyway              | DEBUG: Found filesystem resource: sql/V1__initial.sql
flyway              | WARNING: Connection error: The connection attempt failed. (Caused by db) Retrying in 1 sec...
flyway              | ERROR: Unexpected error
flyway              | org.flywaydb.core.internal.exception.FlywaySqlException:
flyway              | Unable to obtain connection from database (jdbc:postgresql://db:5432/postgres) for user 'luke_skywalker': The connection attempt failed.
flyway              | ----------------------------------------------------------------------------------------------------------------------------------------
flyway              | SQL State  : 08001
flyway              | Error Code : 0
flyway              | Message    : The connection attempt failed.
flyway              |
flyway              |   at org.flywaydb.core.internal.jdbc.JdbcUtils.openConnection(JdbcUtils.java:68)
flyway              |   at org.flywaydb.core.internal.jdbc.JdbcConnectionFactory.<init>(JdbcConnectionFactory.java:68)
flyway              |   at org.flywaydb.core.Flyway.execute(Flyway.java:510)
flyway              |   at org.flywaydb.core.Flyway.migrate(Flyway.java:170)
flyway              |   at org.flywaydb.commandline.Main.executeOperation(Main.java:227)
flyway              |   at org.flywaydb.commandline.Main.main(Main.java:148)
flyway              | Caused by: org.postgresql.util.PSQLException: The connection attempt failed.
flyway              |   at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:315)
flyway              |   at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:51)
flyway              |   at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:223)
flyway              |   at org.postgresql.Driver.makeConnection(Driver.java:465)
flyway              |   at org.postgresql.Driver.connect(Driver.java:264)
flyway              |   at org.flywaydb.core.internal.jdbc.DriverDataSource.getConnectionFromDriver(DriverDataSource.java:263)
flyway              |   at org.flywaydb.core.internal.jdbc.DriverDataSource.getConnection(DriverDataSource.java:227)
flyway              |   at org.flywaydb.core.internal.jdbc.JdbcUtils.openConnection(JdbcUtils.java:54)
flyway              |   ... 5 more
flyway              | Caused by: java.net.UnknownHostException: db
flyway              |   at java.base/java.net.AbstractPlainSocketImpl.connect(Unknown Source)
flyway              |   at java.base/java.net.SocksSocketImpl.connect(Unknown Source)
flyway              |   at java.base/java.net.Socket.connect(Unknown Source)
flyway              |   at org.postgresql.core.PGStream.createSocket(PGStream.java:231)
flyway              |   at org.postgresql.core.PGStream.<init>(PGStream.java:95)
flyway              |   at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:98)
flyway              |   at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:213)
flyway              |   ... 12 more
flyway exited with code 1