初学者です。
既存のrailsアプリにdockerを導入時、
$ docker-compose run web bundle exec rake db:create
を実行すると
Creating my_app_web_run ... done Access denied for user 'user'@'172.18.0.3' (using password: YES) Couldn't create 'my_app_development' database. Please check your configuration.
というエラーが発生します。
原因が特定できず、困っています。
#ファイル設定
Dockerfile
1FROM ruby:3.0.2 2RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \ 3 && echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list 4RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs yarn 5 6 7WORKDIR /my_app 8COPY Gemfile /my_app/Gemfile 9COPY Gemfile.lock /my_app/Gemfile.lock 10RUN gem install bundler 11RUN bundle install 12COPY . /my_app 13 14RUN yarn install --check-files 15RUN bundle exec rails webpacker:compile 16 17# Add a script to be executed every time the container starts. 18COPY entrypoint.sh /usr/bin/ 19RUN chmod +x /usr/bin/entrypoint.sh 20ENTRYPOINT ["entrypoint.sh"] 21EXPOSE 3000 22 23CMD ["rails", "server", "-b", "0.0.0.0"]
#docker-compose.yml version: "3" services: db: image: mysql:8.0.26 environment: MYSQL_ROOT_PASSWORD: password ports: - "3306:3306" volumes: - mysql-data:/var/lib/mysql web: build: . command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'" volumes: - .:/my_app ports: - "3000:3000" depends_on: - db stdin_open: true tty: true volumes: mysql-data: driver: local
#entrypoint.sh #!/bin/bash set -e # Remove a potentially pre-existing server.pid for Rails. rm -f /my_app/tmp/pids/server.pid # Then exec the container's main process (what's set as CMD in the Dockerfile). exec "$@"
#database.yml default: &default adapter: mysql2 encoding: utf8mb4 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> username: root password: password socket: /tmp/mysql.sock host: db
#試したこと
①root パスワードの確認
$ mysql -u root -p # environment: MYSQL_ROOT_PASSWORD: password ここに設定してあるpasswordで入れました
②userのmysql接続を許可
mysql> RENAME USER 'user'@'localhost' to 'user'@'%'; Query OK, 0 rows affected (0.02 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
あなたの回答
tips
プレビュー