Docker-compose MySql with Kingshard ================================== ###### tags: `Docker` `MySql` [MySQL iamge](https://hub.docker.com/_/mysql) ## MySql Architecture ![](https://i.imgur.com/45vqa4i.jpg) ### Docker MySQL Env variables > MYSQL_ROOT_PASSWORD Password for root superuser account. > MYSQL_DATABASE A database to be created on image startup. > MYSQL_USER, MYSQL_PASSWORD Be a super user for the database specified by the ***MYSQL_DATABASE*** > MYSQL_ALLOW_EMPTY_PASSWORD Allow a blank password for the root user. Not suggest to set. > MYSQL_RANDOM_ROOT_PASSWORD Generate a random password for root user, and printed to stdout. > MYSQL_ONETIME_PASSWORD Sets root user(not *MYSQL_USER*) as expired once init, focing change password on first login. ### Compose yaml #### port 暴露端口訊息在主機上 使用Host:Container格式, 或者只有指定容器的端口, host會隨機選擇端口 ```yaml= ports: - "3306:3306" ``` #### expose 暴露端口, 但不會映射到host上, 只會被有link的docker service訪問。 ```yaml= expose: - "3306" ``` #### networks 加入定義好的netwrok, 透過alias或是ipv4_address來定義容器可被搜尋到的名稱。 ```yaml= networks: backend: alias: - masterSql ipv4_address: 172.31.0.11 ``` #### command 容器啟動後默認執行的命令 可以是字串或是json array ```yaml= command: [ "--server-id=${MASTER_SERVER_ID}" ] ``` #### networks 定義頂層network的名稱, 透過ipam(ip address management)配置subnet(CIDR格式) ```yaml= networks: backend: driver: bridge ipam: config: - subnet: 172.31.0.0/24 ``` external 表示該網路已經在compose之外建立, 因此在docker-compse up不會嘗試創建它. 但如果不存在會噴錯. 並用name指名外部建立的network名稱 ```yaml= networks: backend: external: name: docker-compose-mysql_backend ``` #### .env file [environment variables file](https://docs.docker.com/compose/env-file/) 當成docker-compose預設的環境變數 * 每一行都是 VAL=VAL * 註解是# * 空行會被忽略 ```yaml= TAG=8.0.16 ``` [Environment variables in Compose](https://docs.docker.com/compose/environment-variables/) #### docker-compose中的環境變數 ```yaml= image: mysql:${TAG} ``` #### env_file 從文件中獲取環境變數 ```yaml= env_file: - env/base.env - env/master.env ``` #### environment 定義容器的環境變數 ### bash -c string 從中讀取命令, 如果字串後面帶有參數就代入命令, $0開始 ```bash= /bin/bash -c "source /src/github.com/flike/kingshard/dev.sh" ``` [bash](http://linuxcommand.org/lc3_man_pages/bash1.html) [GNU bash學習筆記](https://foreachsam.github.io/book-lang-bash/book/content/command/bash/) ### make -C direrctory ```bash= make -C /src/github.com/flike/kingshard ``` ### alpine [alpine 使用技巧](http://www.10tiao.com/html/357/201702/2247484888/1.html) [alpine apk](https://wangchujiang.com/linux-command/c/apk.html)