1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Docker]DBコンテナの作成・起動までの流れ

Posted at

はじめに

現在、Web系自社開発企業への転職を目標にポートフォリオを作成中。
Dockerを活用してLEMP環境を構築し、Laravel+VuejsでWebアプリケーションの開発を行っております。

今回は、Dockerを活用したDBコンテナの作成・起動までの流れを記録として残します。

目次

  1. DBコンテナの作成・起動までの流れ
  2. DBコンテナ作成中に発生したエラー
  3. 解決策
  4. 参考文献

DBコンテナの作成・起動までの流れ

  • docker-compose.ymlの作成
version: '3.8'

volumes:
  mysql-volume:

services:
  app:
    build:
      context: .
      dockerfile: ./docker/php/Dockerfile
    volumes:
      - ./src/:/var/www/html
    environment:
      - DB_CONNECTION=mysql
      - DB_HOST=db
      - DB_PORT=3306
      - DB_DATABASE=${DB_NAME}
      - DB_USERNAME=${DB_USER}
      - DB_PASSWORD=${DB_PASSWORD}

  web:
    build:
      context: .
      dockerfile: ./docker/nginx/Dockerfile
    ports:
      - ${WEB_PORT}:80
    depends_on:
      - app
    volumes:
      - ./src/:/var/www/html

  db:
    build:
      context: .
      dockerfile: ./docker/mysql/Dockerfile
    ports:
      - ${DB_PORT}:3306
    environment:
      MYSQL_DATABASE: ${DB_NAME}
      MYSQL_USER: ${DB_USER}
      MYSQL_PASSWORD: ${DB_PASSWORD}
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
      TZ: 'Asia/Tokyo'
    volumes:
      - mysql-volume:/var/lib/mysql
  • .envの作成
WEB_PORT=80
DB_PORT=3306

DB_NAME=larasns
DB_USER=default
DB_PASSWORD=secret
DB_ROOT_PASSWORD=root
  • docker-compose.ymlがあるディレクトリで以下コマンドを実行

イメージを作成

docker compose build

コンテナを作成、起動

docker compose up -d

DBコンテナ作成中に発生したエラー

Access denied for user 'root'@'172.18.0.5' (using password: YES)

解決策

MySQLのデータを初期化した

docker-compose down --volumes

docker-compose.ymlがあるディレクトリで以下コマンドを実行

docker compose build
docker-compose up -d

参考文献

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?