0
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 1 year has passed since last update.

【Docker】Extension fieldsを使用してdocker-compose.yml内で値を使い回す

Posted at

はじめに

 本記事は、プログラミング初学者が学習を進めていて疑問に思った点について調べた結果を備忘録も兼ねてまとめたものです。
 そのため、記事の内容に誤りが含まれている可能性があります。ご容赦ください。
 間違いを見つけた方は、お手数ですが、ご指摘いただけますと幸いです。

Extension fieldsを使用してdocker-compose.yml内で値を使い回す

以下のように記述することでdocker-compose.yml内で値を使い回すことができます。

docker-compose.yml
version: '3.9'
x-db-env: &db-env
  MYSQL_ROOT_PASSWORD : rootpassword
  MYSQL_DATABASE: myapp_development
  MYSQL_TEST_DATABASE: myapp_test
  MYSQL_USER: user
  MYSQL_PASSWORD: userpassword

x-pulic-data-driver_ops: &public-driver_opts
  type: none
  device: ${PWD}/public
  o: bind

x-tmp-data-driver_ops: &tmp-data-driver_ops
  <<: *public-driver_opts
  device: ${PWD}/tmp


services:
  db:
    platform: linux/x86_64
    build: ./docker/db
    command:
      --default-authentication-plugin=mysql_native_password &&
      bash -c "chmod +x /docker-entrypoint-initdb.d/00_grant.sh"
    healthcheck:
      test: mysqladmin ping -h db -u$${MYSQL_USER} -p$${MYSQL_PASSWORD}
      interval: 1s
    restart: on-failure
    volumes:
      - mysql_data:/var/lib/mysql
      - type: bind
        source: ./docker/db/my.cnf
        target: /etc/mysql/conf.d/my.cnf
      - type: bind
        source: ./docker/db/
        target: /docker-entrypoint-initdb.d
    environment: *db-env
    ports:
      - "3306:3306"
  web:
    build: ./docker/web
    volumes:
      - public-data:/myapp/public
      - tmp-data:/myapp/tmp
    ports:
      - 80:80
    depends_on:
      - api
  api:
    <省略>
volumes:
  mysql_data:
  public-data:
    driver_opts: *public-driver_opts
  tmp-data:
     driver_opts: *tmp-data-driver_ops
0
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
0
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?