Postgres is unavailable - sleeping (docker-compose error)

I keep getting the error Postgres is unavailable - sleeping when I run docker-compose up
here is the full log

Starting asminiapp_db_1 ... done
Starting asminiapp_web_1 ... done
Attaching to asminiapp_db_1, asminiapp_web_1
db_1   | 
db_1   | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1   | 
db_1   | 2020-03-28 17:35:21.729 UTC [1] LOG:  starting PostgreSQL 12.2 (Debian 12.2-2.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1   | 2020-03-28 17:35:21.729 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1   | 2020-03-28 17:35:21.730 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1   | 2020-03-28 17:35:21.734 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2020-03-28 17:35:21.753 UTC [24] LOG:  database system was interrupted; last known up at 2020-03-28 17:25:18 UTC
db_1   | 2020-03-28 17:35:21.953 UTC [24] LOG:  database system was not properly shut down; automatic recovery in progress
db_1   | 2020-03-28 17:35:21.955 UTC [24] LOG:  redo starts at 0/1645BC0
db_1   | 2020-03-28 17:35:21.956 UTC [24] LOG:  invalid record length at 0/1645BF8: wanted 24, got 0
db_1   | 2020-03-28 17:35:21.956 UTC [24] LOG:  redo done at 0/1645BC0
db_1   | 2020-03-28 17:35:21.973 UTC [1] LOG:  database system is ready to accept connections
web_1  | Resolving Hex dependencies...
web_1  | Dependency resolution completed:
web_1  | Unchanged:
web_1  |   bunt 0.2.0
web_1  |   certifi 2.5.1
web_1  |   connection 1.0.4
web_1  |   cowboy 2.7.0
web_1  |   cowlib 2.8.0
web_1  |   credo 1.3.2
web_1  |   db_connection 2.2.1
web_1  |   decimal 1.8.1
web_1  |   earmark 1.4.3
web_1  |   ecto 3.4.0
web_1  |   ecto_sql 3.4.0
web_1  |   ex_doc 0.21.3
web_1  |   excoveralls 0.12.3
web_1  |   gettext 0.17.4
web_1  |   hackney 1.15.2
web_1  |   idna 6.0.0
web_1  |   jason 1.2.0
web_1  |   makeup 1.0.1
web_1  |   makeup_elixir 0.14.0
web_1  |   metrics 1.0.1
web_1  |   mime 1.3.1
web_1  |   mimerl 1.2.0
web_1  |   nimble_parsec 0.5.3
web_1  |   parse_trans 3.3.0
web_1  |   phoenix 1.4.16
web_1  |   phoenix_ecto 4.1.0
web_1  |   phoenix_pubsub 1.1.2
web_1  |   plug 1.10.0
web_1  |   plug_cowboy 2.1.2
web_1  |   plug_crypto 1.1.2
web_1  |   postgrex 0.15.3
web_1  |   ranch 1.7.1
web_1  |   ssl_verify_fun 1.1.5
web_1  |   telemetry 0.4.1
web_1  |   unicode_util_compat 0.4.1
web_1  | All dependencies are up to date
web_1  | \nNo Dialyxer config: Skipping setup...
web_1  | Postgres is unavailable - sleeping
web_1  | Postgres is unavailable - sleeping
web_1  | Postgres is unavailable - sleeping
web_1  | Postgres is unavailable - sleeping
web_1  | Postgres is unavailable - sleeping
web_1  | Postgres is unavailable - sleeping
web_1  | Postgres is unavailable - sleeping

my Dockerfile.dev

FROM elixir:1.9.4-alpine

# The name of your application/release (required)
ARG AsminiApp


# The environment to build with
ARG MIX_ENV=dev


# Set this to true if this release is not a Phoenix app
ARG SKIP_PHOENIX=false

RUN apk update && \
		apk upgrade --no-cache && \
		apk add bash openssl git make postgresql-client


ENV REPLACE_OS_VARS=true \
    APP_NAME=${APP_NAME}

# Install hex
RUN /usr/local/bin/mix local.hex --force && \
    /usr/local/bin/mix local.rebar --force && \
    /usr/local/bin/mix hex.info

RUN mix archive.install hex phx_new 1.4.14


# create a directory to bundle the application
WORKDIR /usr/app/

ENV MIX_ENV=prod
COPY mix.exs mix.lock ./run.sh ./

EXPOSE 4000

CMD ["./run.sh" ]

my docker-compose.yml file

version: '3'
services:
  web:
    build:
      context: .
      dockerfile: Dockerfile.dev
    ports:
      - "4000:4000"
    volumes:
      - .:/app
    depends_on:
      - db
    command:
      - ./run.sh

  db:
    image: postgres:latest
    restart: always
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: "postgres"
      POSTGRES_DATABASE: "asminiapp_test"
      POSTGRES_PASSWORD: "postgres"

and this is my run.sh file

#!/bin/sh
# Adapted from Alex Kleissner's post, Running a Phoenix 1.3 project with docker-compose
# https://medium.com/@hex337/running-a-phoenix-1-3-project-with-docker-compose-d82ab55e43cf

set -e


# Ensure the app's dependencies are installed
mix deps.get

# Prepare Dialyzer if the project has Dialyxer set up
if mix help dialyzer >/dev/null 2>&1
then
  echo "\nFound Dialyxer: Setting up PLT..."
  mix do deps.compile, dialyzer --plt
else
  echo "\nNo Dialyxer config: Skipping setup..."
fi

# Wait for Postgres to become available.
until psql -h db -U "postgres" -c '\q' 2>/dev/null; do
  >&2 echo "Postgres is unavailable - sleeping"
  sleep 1
done

# echo "\nPostgres is available: continuing with database setup..."

# Potentially Set up the database
mix ecto.create
mix ecto.migrate

echo "\nTesting the installation..."
# "Proove" that install was successful by running the tests
mix test

echo "\n Launching Phoenix web server..."
# Start the phoenix web server
mix phx.server

Your run.sh file prints "Postgres is unavailable - sleeping" in loop until it can connect to postgres successfully with psql. Try to execute the same command manually from the app container (psql -h db -U "postgres" -c '\q') to see what the actual error is.

this is the new error psql: error: could not connect to server: fe_sendauth: no password supplied

As already said in the slack, you need to provide a password to psql, it’s -p or -P or even --password.

Though as also said in the slack, your compose file already makes sure that the database is up before your web container gets started.

1 Like