I ran into the same problem this week. I've found a working solution without the need for --volumes-from
The problem that's already stated is that /var/lib/mysql
is a volume, and since Docker is not going to support UNVOLUME
in it's Dockerfile in the near future, you can't use this location for your database storage if you want to start off with an empty database by default. (https://github.com/docker/docker/issues/18287). That's why I overwrite etc/mysqld.my.cnf
, giving mysql a new datadir.
Together with pwes' his answer, you can create a Dockerile like this:
FROM mysql:5.6
ENV MYSQL_DATABASE db
ENV MYSQL_ROOT_PASSWORD pass
COPY db.sql /docker-entrypoint-initdb.d/db.sql
COPY my.cnf /etc/mysql/my.cnf
RUN /entrypoint.sh mysqld & sleep 30 && killall mysqld
RUN rm /docker-entrypoint-initdb.d/db.sql
The only change there is in my.cnf
is the location of the datadir:
....
[mysqld]
skip-host-cache
skip-name-resolve
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql2 <-- can be anything except /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
explicit_defaults_for_timestamp
....
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…