Search code examples
dockerelixirphoenix-frameworkgitlab-ciecto

Could not find Docker hostname on Gitlab CI


I have an app inside a Docker container based on Elixir image, that need to connect to a database and run tests using a Gitlab runner.

The build stage works fine but there is a problem to connect to a database to run tests. I tried both connecting to a service and running another database container, but from the logs it looks like the problem is with the Phoenix app:

** (RuntimeError) :database is nil in repository configuration
    lib/ecto/adapters/postgres.ex:121: Ecto.Adapters.Postgres.storage_up/1
    lib/mix/tasks/ecto.create.ex:40: anonymous fn/3 in Mix.Tasks.Ecto.Create.run/1
    (elixir) lib/enum.ex:675: Enum."-each/2-lists^foreach/1-0-"/2
    (elixir) lib/enum.ex:675: Enum.each/2
    (mix) lib/mix/task.ex:301: Mix.Task.run_task/3
    (mix) lib/mix/cli.ex:75: Mix.CLI.run_task/2

This is how the config/test.exs file looks like

config :app, App.Repo,
  adapter: Ecto.Adapters.Postgres,
  username: System.get_env("POSTGRES_USER"),
  password: System.get_env("POSTGRES_PASSWORD"),
  database: System.get_env("POSTGRES_DB"),
  hostname: System.get_env("POSTGRES_HOSTNAME"),
  pool: Ecto.Adapters.SQL.Sandbox

This is the output from the runner:

$ docker run --rm -t $CONTAINER echo $MIX_ENV $POSTGRES_USER $POSTGRES_HOSTNAME $POSTGRES_DB
test username db test_db

I'm trying to figure out why I get this error :database is nil, and if it is related to Gitlab, Ecto or Phoenix.

Edit

I wrote static values in the config/*.exs files (for some reason it didn't pick them up), but now it can't find the postgresql hostname. Although the postgresql instance is running it can't find it.

I checked if the instance is running with docker ps


Solution

  • Based on the message :database is nil in repository configuration it seems to me like your POSTGRES_DB variable is not set. You can try to change that configuration line to

    database: System.get_env("POSTGRES_DB") || "postgres"
    

    to see whether you still get the same error. If you don't, you can debug from there.