フリーランス 技術調査ブログ

フリーランス/エンジニア Ruby Python Nodejs Vuejs React Dockerなどの調査技術調査の備忘録

docker secret createコマンド(docker swarmの知識が必要)を使ってmysqlのパスワードを設定する(結論できなかった記事)

はじめに

  • github上にdocker-compose.ymlのファイルを公開するにあたり、mysqlのパスワードが丸見えになっているので、何とかしたいと思っていたら、docker secret createコマンドというものをみつけたので試しに使ってみる。
  • docker swarmの環境を構築できる人でないと利用できないです。
  • この記事にはdocker swarmのことは記載されておりません。

docker secret createとは

  • コンテンツとしてファイルまたはSTDINからシークレットを作成する docs.docker.com

  • APIのバージョンが1.25から利用することが可能である

# docker version
Client:
 Version:       18.02.0-ce-rc2
 
  ## 下記のバージョンが1.25以上である必要がある
 API version:   1.36
 Go version:    go1.9.3
 Git commit:    f968a2c
 Built: Thu Feb  1 00:06:05 2018
 OS/Arch:       linux/amd64
 Experimental:  false
 Orchestrator:  swarm

シークレットを作成する

$ openssl rand -base64 12 | docker secret create db_root_password -
$ openssl rand -base64 12 | docker secret create db_dba_password -
  • 上記のコマンドを実行時に下記のエラーが発生したら
Error response from daemon: This node is not a swarm manager. Use "docker swarm init" or "docker swarm join" to connect this node to swarm and try again.

docker swarm init

# docker swarm init
Swarm initialized: current node (174h48bbfo8a2cmno7oxm1knb) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token <トークンID> <IPアドレス>:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

作成したシークレットの確認

# docker secret ls
ID                          NAME                DRIVER              CREATED             UPDATED
<パスワードが表示されます>   db_dba_password                         11 seconds ago      11 seconds ago
<パスワードが表示されます>   db_root_password                        3 minutes ago       3 minutes ago

docker-comseコマンド実行

  • Warningがあがりコンテナのプロセスが起動するができなかった
# docker-compose up -d

ERROR: The Compose file './docker-compose.yml' is invalid because:
Invalid top-level property "secrets". Valid top-level sections for this Compose file are: services, version, networks, volumes, and extensions starting with "x-".

You might be seeing this error because you're using the wrong Compose file version. Either specify a supported version (e.g "2.2" or "3.3") and place your service definitions under the `services` key, or omit the `version` key and place your service definitions at the root of the file to use version 1.
For more on the Compose file format versions, see https://docs.docker.com/compose/compose-file/

docker-compse.yml

version: '3.3'
  db:
    image: mysql
    container_name: db
    secrets:
      - db_root_password
      - db_dba_password
    deploy:
      replicas: 1
    command: --default-authentication-plugin=mysql_native_password
    volumes:
      - ./db/:/var/lib/mysql
    ports:
      - "3306:3306"  
    environment:
      MYSQL_USER: dba
      MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db_root_password
      MYSQL_PASSWORD_FILE: /run/secrets/db_dba_password
secrets:
  db_root_password:
    external: true
  db_dba_password:
    external: true      

結論

  • 上記のdocker-composeの設定だけではうまくいかない。
  • docker swarmの環境を構築しないと利用できない
  • dockerのネットワークを作成する必要がある
  • docker-composeで指定するバージョンは2.2または3.3にする必要がある

補足

  • swarmを起動してしまってdocker-composeで下記のWARNINGが発生した場合
# docker-compose up -d
WARNING: The Docker Engine you're using is running in swarm mode.
  • 下記のコマンドでswarmを停止させましょう。
# docker swarm leave --force
Node left the swarm.

参考ページ

  • docker swarmを理解している人は下記の参考ページをもとに設定してみてください。 blog.ruanbekker.com