Home>

Now, I am trying to introduce Docker to an existing Rails application by referring to the following article.
[Docker] Building a development environment with a Rails project under construction
An error occurred there and I could not solve it by myself, so I will ask you a question.

Create and edit the file referring to the above article, and put it in the terminal$docker-compose buildAfter hitting
$docker-compose run web rails db: createWhen I typed the command, the following error occurred.

Unknown MySQL server host'db' (-2)
Couldn't create'project_development' database. Please check your configuration.
rails aborted!
Mysql2 :: Error :: ConnectionError: Unknown MySQL server host'db' (-2)
Corresponding source code

▼ Dockerfile

FROM ruby: 2.6.5
RUN apt-get update&&apt-get install -y nodejs --no-install-recommends&&rm -rf/var/lib/apt/lists/*
RUN apt-get update&&apt-get install -y mariadb-client --no-install-recommends&&rm -rf/var/lib/apt/lists/*
RUN apt-get update -qq&&apt-get install -y build-essential libpq-dev nodejs
RUN mkdir/workdir
WORKDIR/workdir
ADD Gemfile/workdir/Gemfile
ADD Gemfile.lock /workdir/Gemfile.lock
ENV BUNDLER_VERSION 2.0.1
RUN gem install bundler
RUN bundle install
ADD./Workdir

▼ database.yml

default:&default
  adapter: mysql2
  encoding: utf8
  pool:<% = ENV.fetch ("RAILS_MAX_THREADS") {5}%>
  username: root
  password: password
  socket: /tmp/mysql.sock
  host: db
development:
<<: * default
  database: project_development
  username: root
  password: password
  database: project_development
test: test:
<<: * default
  database: project_test
production:
<<: * default
  database: project_production
  username: root
  password:<% = ENV ['DATABASE_PASSWORD']%>
  socket: /var/lib/mysql/mysql.sock

▼ docker-compose.yml

version: '3'
services: services:
  db:
    image: mysql: 5.7.8
    environment: environment:
      MYSQL_DATABASE: project_development
      MYSQL_ROOT_PASSWORD: password
      MYSQL_USER: root
      MYSQL_PASSWORD: password
    volumes:
      --./mysql/mysql_data:/var/lib/mysql
    ports: ports:
      -"4306: 3306"
  web:
    build: build:
      context: ..
      dockerfile: Dockerfile
    command:/bin/sh -c "rm -f /workdir/tmp/pids/server.pid&&bundle exec rails s -p 3000 -b '0.0.0.0'"
    tty: true
    stdin_open: true
    depends_on:
      --db
    ports: ports:
      -"3000: 3000"
    volumes:
      --.:/Workdir
What I tried

I tried to delete all Docker images by referring to the following article, but I got the same error.
[PF creation] MYSQL error when building Docker + Rails container

  • Answer # 1

    It seems that it was because mysql was not started before creating the database.
    I solved it with the following command.

    % sudo mysql.server restart
    % docker-compose run web rails db: create

  • Answer # 2

    host: The host name where MySQL is running
    So, localhost and ip should come, but what was db?

  • Answer # 3

    If you don't delete the container and run it again, it will probably succeed

    docker-compose runAfter that, MySQL is not started in time

    docker-compose runAfter executing
    Immediately check it with Docker Desktop's Dashboard etc.
    You can see that the Rails command was executed before the next MySQL log was printed:

    2020-10-07T12: 35: 28.981887Z 0 [Note] mysqld: ready for connections.

    depends_onStarts the dependent Docker container first,
    It does not determine that the internal service is in the started state
    To wait for thiswait-for-itAnd so on

    Reference: Control startup and shutdown order in Compose | Docker Documentation