首页 > 解决方案 > 自定义 docker 官方 mariadb 镜像

问题描述

我打算在 Dockerfile 中创建一个基于 mariadb 的数据库,但失败了。Dockerfile 内容如下:

FROM mariadb
RUN mysql -uroot --password="test" -e "CREATE DATABASE IF NOT EXISTS mymariadb;"

然后,当我使用命令构建自定义图像时docker build -t testdb。它失败并出现以下错误:

docker@docker-virtual-machine:~$ docker build -t tttt .
Sending build context to Docker daemon  32.26kB
Step 1/2 : FROM mariadb
 ---> 2bdd97ca79d9
Step 2/2 : RUN mysql -uroot --password="test" -e "CREATE DATABASE IF NOT EXISTS mymariadb;"
 ---> Running in 80339dc97c2e
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
The command '/bin/sh -c mysql -uroot --password="test" -e "CREATE DATABASE IF NOT EXISTS mymariadb;"' returned a non-zero code: 1
docker@docker-virtual-machine:~$

然后我遵循@Sebastian 的建议,但发现数据库没有创建,详细信息如下:

docker@docker-virtual-machine:~/test$ ls
Dockerfile  init.sql
docker@docker-virtual-machine:~/test$ cat init.sql
CREATE DATABASE IF NOT EXISTS mydb;
docker@docker-virtual-machine:~/test$
docker@docker-virtual-machine:~/test$ cat Dockerfile
FROM mariadb
COPY init.sql /docker-entrypoint-initdb.d/
CMD ["mysqld"]
docker@docker-virtual-machine:~/test$ docker build -t mydb .
Sending build context to Docker daemon  3.072kB
Step 1/3 : FROM mariadb
 ---> 2bdd97ca79d9
Step 2/3 : COPY init.sql /docker-entrypoint-initdb.d/
 ---> 59fa0f173d93
Step 3/3 : CMD ["mysqld"]
 ---> Running in e605ae1fda5b
Removing intermediate container e605ae1fda5b
 ---> 94b98e45dc05
Successfully built 94b98e45dc05
Successfully tagged mydb:latest
docker@docker-virtual-machine:~/test$ docker run -p 3306:3306/tcp -v /my/own/dat            adir1:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=test123 mydb
2019-01-26 17:30:21 0 [Note] mysqld (mysqld 10.3.12-MariaDB-1:10.3.12+maria~bion            ic) starting as process 1 ...
2019-01-26 17:30:21 0 [Note] InnoDB: Using Linux native AIO
2019-01-26 17:30:21 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtin            s
2019-01-26 17:30:21 0 [Note] InnoDB: Uses event mutexes
2019-01-26 17:30:21 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
2019-01-26 17:30:21 0 [Note] InnoDB: Number of pools: 1
2019-01-26 17:30:21 0 [Note] InnoDB: Using SSE2 crc32 instructions
2019-01-26 17:30:21 0 [Note] InnoDB: Initializing buffer pool, total size = 256M            , instances = 1, chunk size = 128M
2019-01-26 17:30:21 0 [Note] InnoDB: Completed initialization of buffer pool
2019-01-26 17:30:21 0 [Note] InnoDB: If the mysqld execution user is authorized,             page cleaner thread priority can be changed. See the man page of setpriority().
2019-01-26 17:30:21 0 [Note] InnoDB: 128 out of 128 rollback segments are active            .
2019-01-26 17:30:21 0 [Note] InnoDB: Creating shared tablespace for temporary ta            bles
2019-01-26 17:30:21 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Phys            ically writing the file full; Please wait ...
2019-01-26 17:30:21 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
2019-01-26 17:30:21 0 [Note] InnoDB: 10.3.12 started; log sequence number 163095            0; transaction id 21
2019-01-26 17:30:21 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/            ib_buffer_pool
2019-01-26 17:30:21 0 [Note] Plugin 'FEEDBACK' is disabled.
2019-01-26 17:30:21 0 [Note] InnoDB: Buffer pool(s) load completed at 190126 17:            30:21
2019-01-26 17:30:21 0 [Note] Server socket created on IP: '::'.
2019-01-26 17:30:21 0 [Warning] 'proxies_priv' entry '@% root@566af8cb98ef' igno            red in --skip-name-resolve mode.
2019-01-26 17:30:21 0 [Note] Reading of all Master_info entries succeded
2019-01-26 17:30:21 0 [Note] Added new Master_info '' to hash table
2019-01-26 17:30:21 0 [Note] mysqld: ready for connections.
Version: '10.3.12-MariaDB-1:10.3.12+maria~bionic'  socket: '/var/run/mysqld/mysq            ld.sock'  port: 3306  mariadb.org binary distribution

标签: mysqldockermariadb

解决方案


官方 MariaDB 映像CMD ["mysqld"]使用RUN mysql. 所以

FROM mariadb
COPY ./my-initial.sql /docker-entrypoint-initdb.d/
CMD ["mysqld"]

应该做的伎俩。

要执行初始 SQL,请参阅图像文档的这一部分


推荐阅读