title: How to exec init script only once after docker run but before entrypoint?
body: <p>I am learning docker these days, and starts with building mysql image by myself.</p>
  
  <p>Dockerfile:</p>
  
  <pre><code>FROM centos
  MAINTAINER Michael
  ENV REFRESHED_AT 2016-07-29
  RUN yum install -y mysql mariadb-server
  VOLUME /var/lib/mysql
  ENTRYPOINT ["/usr/libexec/mysqld", "--user=root"]
  EXPOSE 3306
  </code></pre>
  
  <p>docker run command</p>
  
  <pre><code>docker run -d --name mysql -v /root/docker/mysql/data:/var/lib/mysql -p 3306:3306 michael/mysql
  </code></pre>
  
  <p>This gave an error because I have to exec <code>mysql_install_db</code> first to init DB. But I could not add <code>RUN mysql_install_db</code> in the Dockerfile since I want to use <code>Volume</code> as external data store.</p>
  
  <p>So how should I do this ?</p>
  
  <p>I know there's an official image named <code>mysql</code>. I just want to do this as practice.</p>
  
  <hr>
  
  <p>UPDATE:
  Thanks to @Pieter . I finally did this by using his solution which is provide another <code>entrypoint.sh</code> combines init &amp; start scripts then make it as ENTRYPOINT in Dockerfile :</p>
  
  <pre><code>FROM centos
  MAINTAINER Michael
  ENV REFRESHED_AT 2016-07-29
  RUN yum install -y mysql mariadb-server
  VOLUME /var/lib/mysql
  COPY entrypoint.sh /usr/local/bin/
  ENTRYPOINT ["entrypoint.sh"]
  EXPOSE 3306
  </code></pre>
  
  <p>entrypoint.sh</p>
  
  <pre><code>#!/bin/bash
  
  if [ ! -d "/var/lib/mysql/mysql" ]; then #check whether the DB is initialized.
      echo 'Initializing database'
      mysql_install_db
      echo 'Database initialized'
  fi
  
  /usr/libexec/mysqld --user=root
  </code></pre>
  
  <p>docker run</p>
  
  <pre><code>docker run -d --name mysql -v /root/docker/mysql/data:/var/lib/mysql -p 3306:3306 michael/mysql
  </code></pre>
  
  <p>And this gives a generic solution for such scenario.</p>