{"id":295,"date":"2015-02-18T17:47:31","date_gmt":"2015-02-18T17:47:31","guid":{"rendered":"https:\/\/www.peopleperhour.com\/engineering\/?p=295"},"modified":"2015-02-26T09:39:18","modified_gmt":"2015-02-26T09:39:18","slug":"docker-extending-official-images","status":"publish","type":"post","link":"https:\/\/www.peopleperhour.com\/engineering\/2015\/02\/18\/docker-extending-official-images\/","title":{"rendered":"Docker &#8211; Extending official images"},"content":{"rendered":"<p>In 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 &#8220;<em>out of the box<\/em>&#8220;. Previously we built the mysql container from a raw ubuntu image but now that there is a official <a href=\"https:\/\/registry.hub.docker.com\/_\/mysql\/\">mysql image<\/a> on the docker hub I wanted to change to use it instead. i.e. I wanted to start our Dockerfile with <code>FROM mysql<\/code>. I ran into a issue. The official image uses <code>VOLUME \/var\/lib\/mysql<\/code> so any mysqld data we add in our child container doesn&#8217;t persist the build. This occurs because Volumes don&#8217;t use the layered filesystem &#8211; the docker ISSUE is tracked in <a href=\"https:\/\/github.com\/docker\/docker\/issues\/3639\">https:\/\/github.com\/docker\/docker\/issues\/3639<\/a>.<\/p>\n<p>There are lots of workarounds, but the one I chose was to modify our mysql container to use a different <code>datadir<\/code> (e.g. <code>\/var\/lib\/mysql2<\/code>) so it is not clobbered by the official image. This was the resulting Dockerfile:<\/p>\n<pre class=\"lang:ini\"># Base from Official mysql Docker image v5.6\r\nFROM mysql:5.6\r\n\r\n# Modify child mysql to use a different datadir:\/var\/lib\/mysql2 (because \/var\/lib\/mysql wont persist the build)\r\nRUN mkdir -p \/var\/lib\/mysql2 &amp;&amp; \\\r\n    sed --in-place -e '\/^datadir\\s*=\\s*\\\/var\\\/lib\\\/mysql\/cdatadir        = \/var\/lib\/mysql2' \/etc\/mysql\/my.cnf &amp;&amp; \\\r\n    chown mysql:mysql \/var\/lib\/mysql2\r\n\r\n# Fetch our mysql dump from S3\r\nADD https:\/\/s3.amazonaws.com\/blah\/blah\/blah\/create_schema.sql \/opt\/create_schema.sql\r\n\r\n# Run the script that seeds the database with data\r\nCOPY seeddata.sh \/seeddata.sh\r\nRUN chmod a+x \/seeddata.sh\r\nRUN \/seeddata.sh\r\n\r\n# Make our new datadir a VOLUME so data persists across docker restarts from fig\r\nVOLUME \/var\/lib\/mysql2\r\n<\/pre>\n<p>The same problem occurs for the official mongo container, and I used the same solution:<\/p>\n<pre class=\"lang:ini\"># Base from Official Mongo Docker image v2.4\r\nFROM mongo:2.4\r\n\r\n# Download our  mongo database dump into the container\r\nADD https:\/\/s3.amazonaws.com\/blah\/blah\/blah\/mongodump.dump.tar.gz \/opt\/mongodump.dump.tar.gz\r\n\r\n# Modify child mongo to use a different dbpath: \/data\/db2 (because \/data\/db wont persist the build)\r\nRUN \\\r\n    # Create alternative mongo data folder\r\n    mkdir -p \/data\/db2 &amp;&amp; \\\r\n    \\\r\n    # Create a mongo config file to tell Mongo to use our alternative mongo data folder\r\n    echo \"dbpath = \/data\/db2\" &gt; \/etc\/mongodb.conf &amp;&amp; \\\r\n    \\\r\n    chown mongodb:mongodb \/data\/db2 &amp;&amp; \\\r\n    \\\r\n    # Unpack data dump tarball into the container\r\n    cd \/opt &amp;&amp; \\\r\n    tar -xzf mongodump.dump.tar.gz\r\n\r\n# Run the script that seeds the database with data\r\nCOPY seeddata.sh \/seeddata.sh\r\nRUN chmod a+x \/seeddata.sh\r\nRUN \/seeddata.sh\r\n\r\n# Make our new datadir a VOLUME so data persists across docker restarts from fig\r\nVOLUME \/data\/db2\r\n<\/pre>\n<p>I found for the mongo hack to work I had to start the container with: <code>mongod --config \/etc\/mongodb.conf<\/code><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In 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 &#8220;out of the box&#8220;. Previously we built the mysql container from a raw ubuntu image but now that&#8230;<\/p>\n","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[23],"tags":[24],"class_list":["post-295","post","type-post","status-publish","format-standard","hentry","category-devops-2","tag-docker"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2CA4w-4L","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.peopleperhour.com\/engineering\/wp-json\/wp\/v2\/posts\/295","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.peopleperhour.com\/engineering\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.peopleperhour.com\/engineering\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.peopleperhour.com\/engineering\/wp-json\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/www.peopleperhour.com\/engineering\/wp-json\/wp\/v2\/comments?post=295"}],"version-history":[{"count":8,"href":"https:\/\/www.peopleperhour.com\/engineering\/wp-json\/wp\/v2\/posts\/295\/revisions"}],"predecessor-version":[{"id":304,"href":"https:\/\/www.peopleperhour.com\/engineering\/wp-json\/wp\/v2\/posts\/295\/revisions\/304"}],"wp:attachment":[{"href":"https:\/\/www.peopleperhour.com\/engineering\/wp-json\/wp\/v2\/media?parent=295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.peopleperhour.com\/engineering\/wp-json\/wp\/v2\/categories?post=295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.peopleperhour.com\/engineering\/wp-json\/wp\/v2\/tags?post=295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}