带-v标志的Docker run命令将容器置于退出状态

时间:2017-11-25 15:33:11

标签: mysql docker dockerfile

我正在尝试使用/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

1 个答案:

答案 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
  1. mysql:mysql是该目录的所有者
  2. 我们有很多mysql特定文件
  3. 让我们看看我们用卷映射得到了什么:

    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 ..
    
    1. Docker将此目录映射为root用户
    2. Docker将此目录映射到主机,因此所有文件都会消失,因为在主机上该目录为空
    3. 如何使这项工作? 将您的命令更改为:

      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