解決Windows Docker 客戶端使用 Mariadb 官方image啟動報錯

MariaDB Image使用的是當前最新的v10.3版本

docker pull mariadb:latest  

docker-compose.yml文件內容:

version: "3"  
services:  
  mariadb:
    image: mariadb
    container_name: mariadb
    restart: always
    volumes:
      - ${DATA_DIR}:/var/lib/mysql
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: public

.env 文件內容:

DATA_DIR=d:/data  

容器起來後沒多久就掛掉重啟,查看log信息:

2018-09-17 13:25:21 0 [Note] mysqld (mysqld 10.3.9-MariaDB-1:10.3.9+maria~bionic) starting as process 1 ...  
2018-09-17 13:25:21 0 [Note] InnoDB: Using Linux native AIO  
2018-09-17 13:25:21 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins  
2018-09-17 13:25:21 0 [Note] InnoDB: Uses event mutexes  
2018-09-17 13:25:21 0 [Note] InnoDB: Compressed tables use zlib 1.2.11  
2018-09-17 13:25:21 0 [Note] InnoDB: Number of pools: 1  
2018-09-17 13:25:21 0 [Note] InnoDB: Using SSE2 crc32 instructions  
2018-09-17 13:25:21 0 [Note] InnoDB: Initializing buffer pool, total size = 256M, instances = 1, chunk size = 128M  
2018-09-17 13:25:21 0 [Note] InnoDB: Completed initialization of buffer pool  
2018-09-17 13:25:21 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().  
2018-09-17 13:25:21 0 [ERROR] InnoDB: The Auto-extending innodb_system data file './ibdata1' is of a different size 0 pages than specified in the .cnf file: initial 768 pages, max 0 (relevant if non-zero) pages!  
2018-09-17 13:25:21 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error  
2018-09-17 13:25:21 0 [Note] InnoDB: Starting shutdown...  
2018-09-17 13:25:21 0 [ERROR] Plugin 'InnoDB' init function returned error.  
2018-09-17 13:25:21 0 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.  
2018-09-17 13:25:21 0 [Note] Plugin 'FEEDBACK' is disabled.  
2018-09-17 13:25:21 0 [ERROR] Could not open mysql.plugin table. Some plugins may be not loaded  
2018-09-17 13:25:21 0 [ERROR] Unknown/unsupported storage engine: InnoDB  
2018-09-17 13:25:21 0 [ERROR] Aborting  

這個問題很多人已經遇到了,github上有很多討論,比如這個( https://github.com/docker-library/mariadb/issues/38 )還有這個( https://github.com/laradock/laradock/issues/916 )等,解決方法是在啟動容器的時候加一條命令 mysqld --innodb-flush-method=fsync

最終在Windows Docker 可以正常運行的docker-compose.yml是:

version: "3"  
services:  
  mariadb:
    image: mariadb
    container_name: mariadb
    restart: always
    volumes:
      - ${DATA_DIR}:/var/lib/mysql
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: public
    command: 'mysqld --innodb-flush-method=fsync'

希望官方能夠在這之後的版本中解決這個問題。