我正在尝试使用/home/ubuntu/data
标记将容器中的本地目录/var/lib/mysql
映射到-v
文件夹,但容器的状态变为Exited (0) 1
。但是,如果我根本不使用-v
标志,则容器为Up
,但这不是我想要的。可能是什么原因?我看到与工作示例相对的事件日志中缺少volume mount
行。
$ docker -v
Docker version 17.09.0-ce, build afdb6d4
Dockerfile
FROM ubuntu:16.04
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y mysql-server \
&& sed -i "s/127.0.0.1/0.0.0.0/g" /etc/mysql/mysql.conf.d/mysqld.cnf \
&& mkdir /var/run/mysqld \
&& chown -R mysql:mysql /var/run/mysqld
VOLUME ["/var/lib/mysql"]
EXPOSE 3306
CMD ["mysqld_safe"]
这不起作用。
$ docker run -i -t -d -v /home/ubuntu/data:/var/lib/mysql --name mysql_container mysql_image
事件日志。
2017-11-... container create 08b44c094... (image=mysql_image, name=mysql_container)
2017-11-... network connect 62bb211934... (container=08b44c094..., name=bridge, type=bridge)
2017-11-... container start 08b44c094... (image=mysql_image, name=mysql_container)
2017-11-... container die 08b44c094... (exitCode=0, image=mysql_image, name=mysql_container)
2017-11-... network disconnect 62bb211934... (container=08b44c094..., name=bridge, type=bridge)
容器日志。
$ docker logs -t mysql_container
2017-11-... mysqld_safe Logging to syslog.
2017-11-... mysqld_safe Logging to '/var/log/mysql/error.log'.
2017-11-... mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
不使用-v
$ docker run -i -t -d --name mysql_container mysql_image
事件日志。
2017-11-... container create 84993141... (image=mysql_image, name=mysql_container)
2017-11-... network connect 62bb2119... (container=84993141..., name=bridge, type=bridge)
2017-11-... volume mount 8c36b53d33... (container=84993141...7, destination=/var/lib/mysql, driver=local, propagation=, read/write=true)
2017-11-... container start 84993141... (image=mysql_image, name=mysql_container)
容器日志。
$ docker logs -t mysql_container
2017-11-... mysqld_safe Logging to syslog.
2017-11-... mysqld_safe Logging to '/var/log/mysql/error.log'.
2017-11-... mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
2017-11-... mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
答案 0 :(得分:1)
这是一个有点复杂但有趣的案例。
那你怎么检查发生了什么?使用以下命令:
docker run -i -t -v /tmp/data:/var/lib/mysql mysql_image bash
现在你在容器内,所以让我们试试命令:
mysqld_safe
它结束了,但让我们看看/var/log/mysql/error.log
我们在那里看到:
2017-11-25T17:22:24.006180Z 0 [ERROR] InnoDB: Operating system error number 13 in a file operation.
2017-11-25T17:22:24.006211Z 0 [ERROR] InnoDB: The error means mysqld does not have the access rights to the directory.
2017-11-25T17:22:24.006221Z 0 [ERROR] InnoDB: Operating system error number 13 in a file operation.
2017-11-25T17:22:24.006229Z 0 [ERROR] InnoDB: The error means mysqld does not have the access rights to the directory.
2017-11-25T17:22:24.006237Z 0 [ERROR] InnoDB: Cannot open datafile './ibdata1'
好的,让我们看看/var/lib/mysql
在没有卷映射时的样子:
root@4474b1cd4300:/var/lib/mysql# ls -lah
total 109M
drwx------ 5 mysql mysql 4.0K Nov 25 17:24 .
drwxr-xr-x 1 root root 4.0K Nov 25 17:13 ..
-rw-r----- 1 mysql mysql 56 Nov 25 17:13 auto.cnf
-rw-r--r-- 1 root root 0 Nov 25 17:13 debian-5.7.flag
-rw-r----- 1 mysql mysql 419 Nov 25 17:13 ib_buffer_pool
-rw-r----- 1 mysql mysql 48M Nov 25 17:13 ib_logfile0
-rw-r----- 1 mysql mysql 48M Nov 25 17:13 ib_logfile1
-rw-r----- 1 mysql mysql 12M Nov 25 17:13 ibdata1
drwxr-x--- 2 mysql mysql 4.0K Nov 25 17:13 mysql
drwxr-x--- 2 mysql mysql 4.0K Nov 25 17:13 performance_schema
drwxr-x--- 2 mysql mysql 12K Nov 25 17:13 sys
让我们看看我们用卷映射得到了什么:
root@fca45ee1e8fb:/var/lib/mysql# ls -lah
total 8.0K
drwxr-xr-x 2 root root 4.0K Nov 25 17:22 .
drwxr-xr-x 1 root root 4.0K Nov 25 17:13 ..
如何使这项工作? 将您的命令更改为:
CMD chown -R mysql:mysql /var/lib/mysql && if [ ! -c /var/lib/mysql/ibdata1 ]; then mysqld --initialize-insecure; fi && mysqld_safe
那里发生了什么?
chown -R mysql:mysql /var/lib/mysql - get back mysql:mysql owner
if [ ! -c /var/lib/mysql/ibdata1 ]; then mysqld --initialize-insecure; fi - recreate mysql files with root user without pass but only if files not already exists (required for next runs)
mysqld_safe - run mysql