Open6

CircleCIでの自動テストをdocker-composeで実装

あつしあつし

システムスペックの自動テストをCircleCIで行うため、開発環境のdocker-composeとCircleCIの設定作業をやる

※CircleCI CLIが使えずコミットログが大変なことに...

あつしあつし

DBセットアップ時に下記のエラー

rails aborted!
ActiveRecord::ConnectionNotEstablished: could not connect to server: No such file or directory
	Is the server running locally and accepting
	connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

Caused by:
PG::ConnectionBad: could not connect to server: No such file or directory
	Is the server running locally and accepting
	connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
あつしあつし

同じくDBセットアップ時にパスワード認証エラー

rails aborted!
ActiveRecord::ConnectionNotEstablished: FATAL:  password authentication failed for user "hogehoge"

Caused by:
PG::ConnectionBad: FATAL:  password authentication failed for user "hogehoge"
あつしあつし

この時の設定ファイル

circleci.yml
jobs:
  rspec:
    machine:
      image: circleci/classic:edge
    steps:
      - checkout
      - run:
          name: Decode dotenv file
          command: echo $ENV_FILE | base64 --decode > .env
      - run:
          name: Build containers
          command: docker-compose -f docker-compose.dev.yml build
      - run:
          name: Setup containers
          command: docker-compose -f docker-compose.dev.yml up -d
      - run:
          name: sleep 10second
          command: sleep 10
      - run:
          name: Setup DB
          command: docker-compose -f docker-compose.dev.yml run rails_dev rails db:create db:migrate
      - run:
          name: Start RSpec
          command: docker-compose -f docker-compose.dev.yml run rails_dev bundle exec rspec
      - run:
          name: Down containers
          command: docker-compose -f docker-compose.dev.yml down

・・・
docker-compose.dev.yml
version: '3'
services:
  db:
    image: postgres:13
    environment:
      POSTGRES_USER: $DB_USERNAME
      POSTGRES_PASSWORD: $DB_PASSWORD
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
  
  rails_dev:
    build:
      context: .
      dockerfile: Dockerfile.dev
    command: bundle exec puma -C config/puma.rb
    volumes:
      - .:/hogehoge
      - public-data:/hogehoge/public
      - tmp-data:/hogehoge/tmp
      - log-data:/hogehoge/log
    tty: true
    stdin_open: true
    environment:
      - "SELENIUM_DRIVER_URL=http://selenium_chrome:4444/wd/hub"
    depends_on:
      - db

  web_dev:
    build:
      context: .
      dockerfile: ./containers/nginx/develop/Dockerfile.dev
    volumes:
      - public-data:/hogehoge/public
      - tmp-data:/hogehoge/tmp
    ports:
      - 80:80
    tty: true
    stdin_open: true

  selenium_chrome:
    image: selenium/standalone-chrome-debug
    logging:
      driver: none

volumes:
    public-data:
    tmp-data:
    log-data:
    db-data: