Docker-compose services cannot talk to each other

This works fine locally but I am getting the following error in CircleCI:

Failure/Error: Mysql2::Client.new(host: "db", username: "root")
          
          Mysql2::Error::ConnectionError:
            Can't connect to MySQL server on 'db' (115)

config.yml

version: 2
jobs:
  build:
    docker:
      - image: cimg/base:2021.03
    working_directory: ~/
    steps:
      - checkout
      - setup_remote_docker:
          version: 20.10.2
      - run: docker-compose run web bundle exec rspec

docker-compose.yml

version: '2'
services:
  db:
    image: mysql:5.6
    command:
      --server-id=1
      --log-bin=mysql-bin
      --binlog-format=ROW
    environment:
      MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
    healthcheck:
      test: mysql -u root -e "SHOW STATUS LIKE 'Uptime'\G"
      interval: 10s
      timeout: 5s
      retries: 10
  web:
    build: .
    command: bundle exec rails s
    depends_on:
      - db

Dockerfile

FROM ruby:2.5.5
RUN apt-get update -qq
RUN mkdir /app
WORKDIR /app
COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock
RUN bundle install
COPY . /app

I figured it out, the tests were running before the DB was ready. The following change fixed it:

  web:
    build: .
    command: bundle exec rails s
    depends_on:
      db:
        condition: service_healthy
1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.