🐷

[Docker]DBコンテナ作成時のエラー

2022/10/18に公開約3,800字

状況


Laravelなしでphpのアプリを製作しようとしているのですが、ローカルのphp環境を管理するのが意外に面倒だったのでDockerを利用することにしました。

[ERROR] [Entrypoint]: MYSQL_USER="root", MYSQL_PASSWORD cannot be used for the root user
    Use one of the following to control the root user password:
    - MYSQL_ROOT_PASSWORD
    - MYSQL_ALLOW_EMPTY_PASSWORD
    - MYSQL_RANDOM_ROOT_PASSWORD

rootユーザのログインにはMYSQL_USERは使えません、というメッセージらしい。

docker_compose.ymlを確認してみる

version: "3"
services:
  db:
    image: mysql:5.7
    environment:
        MYSQL_DATABASE: mvc
        MYSQL_USER: root
        MYSQL_PASSWORD: aaaaaaaa
        MYSQL_ROOT_PASSWORD: aaaaaaaa
    ports:
        - "4306:3306"
    command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    volumes:
      - ./db/mysql:/var/lib/mysql

  php:
    build: ./php
    volumes:
      - ./nginx/html:/usr/share/nginx/html
      - ./php/php.ini:/usr/local/etc/php/conf.d/php.ini
    depends_on: ["db"]

  nginx:
    image: nginx:latest
    volumes:
      - ./nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf
      - ./nginx/html:/usr/share/nginx/html
    restart: always
    ports: ["8080:80"]
    depends_on: ["php"]

docker-compose.yml記述の参考はこちら。
https://qiita.com/si-ma/items/805c3323ea7573f61727

修正


environmentをこの様に修正

environment:
      MYSQL_DATABASE: mvc
      MYSQL_ROOT_PASSWORD: aaaaaaaa
      MYSQL_PASSWORD: aaaaaaaa

docker-compose build → docker-compose up -dでコンテナ再作成。うまくいきました。DBにも接続できました。

(base) ➜  docker_p docker-compose build
db uses an image, skipping
nginx uses an image, skipping
Building php
[+] Building 2.3s (8/8) FINISHED
 => [internal] load build definition from Dockerfile                                                                                      0.0s
 => => transferring dockerfile: 36B                                                                                                       0.0s
 => [internal] load .dockerignore                                                                                                         0.0s
 => => transferring context: 2B                                                                                                           0.0s
 => [internal] load metadata for docker.io/library/php:7.4.0-fpm                                                                          2.2s
 => [internal] load build context                                                                                                         0.0s
 => => transferring context: 28B                                                                                                          0.0s
 => [1/3] FROM docker.io/library/php:7.4.0-fpm@sha256:6bbea5150492b82eeb90ee6dcbbef1586e15daa4a6373e2a8c9cc1ca3f82cf03                    0.0s
 => CACHED [2/3] RUN apt-get update     && docker-php-ext-install pdo_mysql                                                               0.0s
 => CACHED [3/3] ADD ./php.ini /usr/local/etc/php/php.ini                                                                                 0.0s
 => exporting to image                                                                                                                    0.0s
 => => exporting layers                                                                                                                   0.0s
 => => writing image sha256:e3e17b1a8acf2ca2baf016d361f4af99c0b64d9870bda3154f99a1fa285cd512                                              0.0s
 => => naming to docker.io/library/docker_p_php                                                                                           0.0s

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
(base) ➜  docker_p docker-compose up -d
Recreating docker_p_db_1 ... done
docker_p_php_1 is up-to-date
docker_p_nginx_1 is up-to-date

Discussion

ログインするとコメントできます