Docker-compose mysql:import .sql

我的docker-compose脚本成功pipe理运行一个mysql / mariadb服务,并将包含数据库模式的“init.sql”脚本复制到“/docker-entrypoint-initdb.d”。 但是,sql脚本从来没有像按照文档那样执行。

没有错误,没有在日志中。 我检查了sql文件被复制到容器中的正确位置。 我检查了脚本在空数据库上没有错误的情况下运行。

我错过了什么? 如果脚本被执行(甚至由于某种原因,什么都不做),它甚至可以在日志中看到吗?

编辑: docker-compose版本:1.8.1,图片mariadb:10.1.21)

# docker-compose.yml version: '2' volumes: data-volume: {} services: mysql: image: mariadb ports: - "3306:3306" environment: MYSQL_ROOT_PASSWORD: pwd MYSQL_DATABASE: users_db volumes: - data-volume:/var/lib/mysql - ./resources/docker-sql/init.sql:/docker-entrypoint-initdb.d/init.sql backend: image: myapp ports: - "8000:80" depends_on: - mysql links: - mysql 

查看mariadb映像的docker-entrypoint.sh文件的来源,这个脚本应该执行你的init.sql

  1. 只有当容器之前没有包含数据库(脚本的第76行)时才会执行该脚本
  2. 会有一个日志输出通知你脚本被调用。

要使用docker-compose实现这一点,您必须停止服务,删除容器(摆脱数据库)并重新启动它:

 docker-compose stop docker-compose rm docker-compose start 

下面是图像行为的一个例子,只有.sh文件而不是.sql

 $ cat hello.sh echo "This is output of the hello script" $ docker run -it -v `pwd`/hello.sh:/docker-entrypoint-initdb.d/hello.sh -v mariadb_test:/var/lib/mysql -e MYSQL_RANDOM_ROOT_PASSWORD=1 mariadb Initializing database 2017-02-09 14:13:54 140617005938624 [Note] /usr/sbin/mysqld (mysqld 10.1.21-MariaDB-1~jessie) starting as process 63 ... 2017-02-09 14:13:54 140617005938624 [Note] InnoDB: Using mutexes to ref count buffer pool pages 2017-02-09 14:13:54 140617005938624 [Note] InnoDB: The InnoDB memory heap is disabled 2017-02-09 14:13:54 140617005938624 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins [...] 2017-02-09 14:14:03 139836212086528 [Note] InnoDB: Dumping buffer pool(s) not yet started 2017-02-09 14:14:03 139836971521984 [Note] Plugin 'FEEDBACK' is disabled. 2017-02-09 14:14:03 139836971521984 [Warning] 'user' entry 'root@830dbd0908f3' ignored in --skip-name-resolve mode. 2017-02-09 14:14:03 139836971521984 [Warning] 'proxies_priv' entry '@% root@830dbd0908f3' ignored in --skip-name-resolve mode. 2017-02-09 14:14:03 139836971521984 [Note] mysqld: ready for connections. Version: '10.1.21-MariaDB-1~jessie' socket: '/var/run/mysqld/mysqld.sock' port: 0 mariadb.org binary distribution Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it. GENERATED ROOT PASSWORD: dau6voh4eej2jooRohpiop4eh6ahl7Uz 2017-02-09 14:14:05 139836970654464 [Warning] 'proxies_priv' entry '@% root@830dbd0908f3' ignored in --skip-name-resolve mode. /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/hello.sh This is output of the hello script 2017-02-09 14:14:05 139836970351360 [Note] mysqld: Normal shutdown 2017-02-09 14:14:05 139836970351360 [Note] Event Scheduler: Purging the queue. 0 events 2017-02-09 14:14:05 139836195301120 [Note] InnoDB: FTS optimize thread exiting. 2017-02-09 14:14:05 139836970351360 [Note] InnoDB: Starting shutdown... 2017-02-09 14:14:05 139836970351360 [Note] InnoDB: Waiting for page_cleaner to finish flushing of buffer pool 2017-02-09 14:14:07 139836970351360 [Note] InnoDB: Shutdown completed; log sequence number 1616829 2017-02-09 14:14:07 139836970351360 [Note] mysqld: Shutdown complete MySQL init process done. Ready for start up. [...] 

你看到日志输出中的某处, hello.sh脚本的stdout被埋没了。 在随后的容器启动时,脚本不会执行,因为数据库已经创build(在本地mariadb卷中):

 $ docker run -it -v `pwd`/hello.sh:/docker-entrypoint-initdb.d/hello.sh -v mariadb_test:/var/lib/mysql -e MYSQL_RANDOM_ROOT_PASSWORD=1 mariadb 2017-02-09 14:19:13 140155189532608 [Note] mysqld (mysqld 10.1.21-MariaDB-1~jessie) starting as process 1 ... 2017-02-09 14:19:13 140155189532608 [Note] InnoDB: Using mutexes to ref count buffer pool pages 2017-02-09 14:19:13 140155189532608 [Note] InnoDB: The InnoDB memory heap is disabled 2017-02-09 14:19:13 140155189532608 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins 2017-02-09 14:19:13 140155189532608 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier 2017-02-09 14:19:13 140155189532608 [Note] InnoDB: Compressed tables use zlib 1.2.8 2017-02-09 14:19:13 140155189532608 [Note] InnoDB: Using Linux native AIO 2017-02-09 14:19:13 140155189532608 [Note] InnoDB: Using SSE crc32 instructions 2017-02-09 14:19:13 140155189532608 [Note] InnoDB: Initializing buffer pool, size = 256.0M 2017-02-09 14:19:13 140155189532608 [Note] InnoDB: Completed initialization of buffer pool 2017-02-09 14:19:13 140155189532608 [Note] InnoDB: Highest supported file format is Barracuda. 2017-02-09 14:19:13 140155189532608 [Note] InnoDB: 128 rollback segment(s) are active. 2017-02-09 14:19:13 140155189532608 [Note] InnoDB: Waiting for purge to start 2017-02-09 14:19:13 140155189532608 [Note] InnoDB: Percona XtraDB (http://www.percona.com) 5.6.34-79.1 started; log sequence number 1616839 2017-02-09 14:19:13 140154429736704 [Note] InnoDB: Dumping buffer pool(s) not yet started 2017-02-09 14:19:13 140155189532608 [Note] Plugin 'FEEDBACK' is disabled. 2017-02-09 14:19:13 140155189532608 [Note] Server socket created on IP: '::'. 2017-02-09 14:19:13 140155189532608 [Warning] 'proxies_priv' entry '@% root@830dbd0908f3' ignored in --skip-name-resolve mode. 2017-02-09 14:19:13 140155189532608 [Note] mysqld: ready for connections. Version: '10.1.21-MariaDB-1~jessie' socket: '/var/run/mysqld/mysqld.sock' port: 3306 mariadb.org binary distribution