Docker – Extending official images
By Tom In DevOpsIn one of our projects we provide our developers a mysql container for the local development environment pre-seeded with tables and data so the app will work “out of the box“. Previously we built the mysql container from a raw ubuntu image but now that there is a official mysql image on the docker hub I wanted to change to use it instead. i.e. I wanted to start our Dockerfile with FROM mysql
. I ran into a issue. The official image uses VOLUME /var/lib/mysql
so any mysqld data we add in our child container doesn’t persist the build. This occurs because Volumes don’t use the layered filesystem – the docker ISSUE is tracked in https://github.com/docker/docker/issues/3639.
There are lots of workarounds, but the one I chose was to modify our mysql container to use a different datadir
(e.g. /var/lib/mysql2
) so it is not clobbered by the official image. This was the resulting Dockerfile:
# Base from Official mysql Docker image v5.6 FROM mysql:5.6 # Modify child mysql to use a different datadir:/var/lib/mysql2 (because /var/lib/mysql wont persist the build) RUN mkdir -p /var/lib/mysql2 && \ sed --in-place -e '/^datadir\s*=\s*\/var\/lib\/mysql/cdatadir = /var/lib/mysql2' /etc/mysql/my.cnf && \ chown mysql:mysql /var/lib/mysql2 # Fetch our mysql dump from S3 ADD https://s3.amazonaws.com/blah/blah/blah/create_schema.sql /opt/create_schema.sql # Run the script that seeds the database with data COPY seeddata.sh /seeddata.sh RUN chmod a+x /seeddata.sh RUN /seeddata.sh # Make our new datadir a VOLUME so data persists across docker restarts from fig VOLUME /var/lib/mysql2
The same problem occurs for the official mongo container, and I used the same solution:
# Base from Official Mongo Docker image v2.4 FROM mongo:2.4 # Download our mongo database dump into the container ADD https://s3.amazonaws.com/blah/blah/blah/mongodump.dump.tar.gz /opt/mongodump.dump.tar.gz # Modify child mongo to use a different dbpath: /data/db2 (because /data/db wont persist the build) RUN \ # Create alternative mongo data folder mkdir -p /data/db2 && \ \ # Create a mongo config file to tell Mongo to use our alternative mongo data folder echo "dbpath = /data/db2" > /etc/mongodb.conf && \ \ chown mongodb:mongodb /data/db2 && \ \ # Unpack data dump tarball into the container cd /opt && \ tar -xzf mongodump.dump.tar.gz # Run the script that seeds the database with data COPY seeddata.sh /seeddata.sh RUN chmod a+x /seeddata.sh RUN /seeddata.sh # Make our new datadir a VOLUME so data persists across docker restarts from fig VOLUME /data/db2
I found for the mongo hack to work I had to start the container with: mongod --config /etc/mongodb.conf
No Comment