Host lookup failure with Hound and Selenium under docker-compose

I am trying to add end-to-end testing with Hound and Selenium to my Phoenix application that is run under docker-compose, and I keep getting could not create a new session: timeout, check webdriver is running from Hound, even though I am invoking mix through a wrapper script that first waits until Selenium is ready. I cannot figure out why the Selenium status check passes and then Hound fails to connect, because they are both set to point to the same hostname and port.

docker-compose.yml

version: "3.3"
services:
  phoenix:
    build:
      context: .
      dockerfile: Dockerfile.dev
    ports:
      - 4000:4000
      - 4001:4001
      - 4002:4002
    depends_on:
      - db
      - selenium
    environment:
      - POSTGRES_HOST=db
    volumes:
      - .:/paddy

  db:
    image: postgres:11
    environment:
      - POSTGRES_HOST_AUTH_METHOD=trust
    ports:
      - "${DB_PORT}:5432"
    volumes:
      - "${HOME}/srv/paddy-db:/var/lib/postgresql/data"

  selenium:
    image: selenium/standalone-firefox:4
    ports:
      - 4444:4444
      - 7900:7900
    volumes:
      - /dev/shm:/dev/shm

Entrypoint script for the phoenix service:

#!/bin/bash

echo 'Waiting for database to be ready...'
while ! pg_isready -q -h "$POSTGRES_HOST" -p 5432 -U postgres; do
  sleep 2
done

echo 'Waiting for Selenium to be ready...'
while ! [ "$(curl -sSL http://selenium:4444/status | jq -r '.value.ready')" == "true" ]; do
  sleep 2
done

exec mix "$@"

Relevant parts of config/test.exs

config :paddy, PaddyWeb.Endpoint,
  http: [port: 4002],
  server: true

config :hound,
  driver: "selenium",
  host: "http://selenium",
  port: 4444,
  browser: "firefox",
  app_host: "http://phoenix",
  app_port: 4002

Hound error:

21:43:29.785 [error] GenServer Hound.SessionServer terminating
** (RuntimeError) could not create a new session: timeout, check webdriver is running
    (hound 1.1.1) lib/hound/session_server.ex:101: Hound.SessionServer.create_session/2
    (hound 1.1.1) lib/hound/session_server.ex:78: Hound.SessionServer.handle_call/3
    (stdlib 3.12.1) gen_server.erl:661: :gen_server.try_handle_call/4
    (stdlib 3.12.1) gen_server.erl:690: :gen_server.handle_msg/6
    (stdlib 3.12.1) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message (from #PID<0.853.0>): {:change_session, #PID<0.853.0>, :default, []}

Update:
This error seems to happen when I rerun docker-compose too soon after the previous run. If I wait a little bit longer, it gives me this instead:

     ** (RuntimeError) Reached error page: about:neterror?e=dnsNotFound&u=http%3A//phoenix%3A4002/&c=UTF-8&d=We%20can%E2%80%99t%20connect%20to%20the%20server%20at%20phoenix.
     Build info: version: '4.0.0-beta-4', revision: 'a51085a604'
     System info: host: '254bef88f074', ip: '172.18.0.3', os.name: 'Linux', os.arch: 'amd64', os.version: '5.10.0-0.bpo.5-amd64', java.version: '11.0.11'
     Driver info: driver.version: unknown

So it looks like Firefox is unable to resolve the hostname of the phoenix service/container.