title: Persisting MySQL data in Docker
body: <p>I am new to Docker. I have successfully created a Docker <em>image</em> with the following Dockerfile:</p>
  
  <pre><code>From alpine:3.4
  MAINTAINER SomeName - domain.tld
  
  # Timezone
  ENV TIMEZONE Asia/Kolkata
  
  # RUN sed -i 's#dl-cdn\.alpinelinux\.org#mirrors\.aliyun\.com#' /etc/apk/repositories
  
  # install mysql, apache and php and php extensions, tzdata, wget
  RUN echo "@community http://dl-cdn.alpinelinux.org/alpine/edge/community" &gt;&gt; /etc/apk/repositories &amp;&amp; \
      apk add --update \
      mysql mysql-client \
      apache2 \
      curl wget \
      tzdata \
      php5-apache2 \
      php5-cli \
      php5-phar \
      php5-zlib \
      php5-zip \
      php5-bz2 \
      php5-ctype \
      php5-mysqli \
      php5-mysql \
      php5-pdo_mysql \
      php5-opcache \
      php5-pdo \
      php5-json \
      php5-curl \
      php5-gd \
      php5-gmp \
      php5-mcrypt \
      php5-openssl \
      php5-dom \
      php5-xml \
      php5-iconv \
      php5-xdebug@community
  
  RUN curl -sS https://getcomposer.org/installer | \
      php -- --install-dir=/usr/bin --filename=composer
  
  # configure timezone, mysql, apache
  RUN cp /usr/share/zoneinfo/${TIMEZONE} /etc/localtime &amp;&amp; \
      echo "${TIMEZONE}" &gt; /etc/timezone &amp;&amp; \
      mkdir -p /run/mysqld &amp;&amp; chown -R mysql:mysql /run/mysqld /var/lib/mysql &amp;&amp; \
      mysql_install_db --user=mysql --verbose=1 --basedir=/usr --datadir=/var/lib/mysql --rpm &gt; /dev/null &amp;&amp; \
      mkdir -p /run/apache2 &amp;&amp; chown -R apache:apache /run/apache2 &amp;&amp; chown -R apache:apache /var/www/localhost/htdocs/ &amp;&amp; \
      sed -i 's#AllowOverride none#AllowOverride All#' /etc/apache2/httpd.conf &amp;&amp; \
      sed -i 's#ServerName www.example.com:80#\nServerName localhost:80#' /etc/apache2/httpd.conf &amp;&amp; \
      sed -i '/skip-external-locking/a log_error = \/var\/lib\/mysql\/error.log' /etc/mysql/my.cnf &amp;&amp; \
      sed -i '/skip-external-locking/a general_log = ON' /etc/mysql/my.cnf &amp;&amp; \
      sed -i '/skip-external-locking/a general_log_file = \/var\/lib\/mysql\/query.log' /etc/mysql/my.cnf
  
  # Configure xdebug
  RUN echo "zend_extension=xdebug.so" &gt; /etc/php5/conf.d/xdebug.ini &amp;&amp; \ 
      echo -e "\n[XDEBUG]"  &gt;&gt; /etc/php5/conf.d/xdebug.ini &amp;&amp; \ 
      echo "xdebug.remote_enable=1" &gt;&gt; /etc/php5/conf.d/xdebug.ini &amp;&amp; \  
      echo "xdebug.remote_connect_back=1" &gt;&gt; /etc/php5/conf.d/xdebug.ini &amp;&amp; \ 
      echo "xdebug.idekey=PHPSTORM" &gt;&gt; /etc/php5/conf.d/xdebug.ini &amp;&amp; \ 
      echo "xdebug.remote_log=\"/tmp/xdebug.log\"" &gt;&gt; /etc/php5/conf.d/xdebug.ini
  
  #start apache
  RUN echo "#!/bin/sh" &gt; /start.sh &amp;&amp; \
      echo "httpd" &gt;&gt; /start.sh &amp;&amp; \
      echo "nohup mysqld --skip-grant-tables --bind-address 0.0.0.0 --user mysql &gt; /dev/null 2&gt;&amp;1 &amp;" &gt;&gt; /start.sh &amp;&amp; \
      echo "sleep 3 &amp;&amp; mysql -uroot -e \"create database db;\"" &gt;&gt; /start.sh &amp;&amp; \
      echo "tail -f /var/log/apache2/access.log" &gt;&gt; /start.sh &amp;&amp; \
      chmod u+x /start.sh
  
  WORKDIR /var/www/localhost/htdocs/
  
  EXPOSE 80
  EXPOSE 3306
  
  #VOLUME ["/var/www/localhost/htdocs","/var/lib/mysql","/etc/mysql/"]
  ENTRYPOINT ["/start.sh"]
  </code></pre>
  
  <p>Ideally I want the docker image to contain Apache, PHP and MySQL -- all in one image. I want the image to be as portable as possible. So that every time I need to start a new project, I can just create a new container and mount volumes and go ahead and code.</p>
  
  <p>When I start a new container, I get everything as expected. But the mysql data cannot be mounted. I think it is because the mounting is one-way (or is it?) If I add <code>-v /project_dir/data:/var/lib/mysql</code> while starting a new container, mysql doesn't start up because the <code>project_dir/data</code> is empty. My questions are:</p>
  
  <ol>
  <li>How to mount data from my <code>project_dir</code> to <code>/var/lib/mysql</code> <em>inside</em> the container?</li>
  <li>How can I set different mysql passwords to different containers?</li>
  </ol>
  
  <p><strong>Edit:</strong> If I don't mount my local volume (i.e. <code>project_dir/data</code>) and start the container, get a shell tty, I see that <code>/var/lib/mysql</code> has several files, which I assume are needed to run mysql and its initial databases. But if I mount my local volume and <code>ls</code> inside <code>/var/lib/mysql</code> (inside the container), it is empty. That is why MySQL is not starting up and I cannot persist data from <em>outside</em> the container. My question is: how I can get it running and persist data from local directory.</p>