title: Accessing docker container mysql databases
body: <p>I am trying to access mysql databases from my docker host to the container.</p>
  
  <p>It's my own dockerfile which install a database expose on port 3306.
  I launch my docker with docker-compose, and my compose file is mapping 3308 host port on 3306 container port.</p>
  
  <p>I can access to mysql from the host like this :
  <code>mysql -h localhost -P 3308 -u root -pMyPassword</code></p>
  
  <p>It's working well, but what I can't figure out, is why I can't see any datas from my container?</p>
  
  <p>From inside the container, I have a test databases which I can connect to without any problem. But when I connect from the host to the container mysql process, It seems to show me the mysql datas from the host machine, not from the container one. </p>
  
  <p>Any ideas?</p>
  
  <p>Thanks :)</p>
  
  <p>EDIT 1 :</p>
  
  <p>So here is the first way I can connect to mysql into the container :</p>
  
  <pre><code>docker exec -it MyContainer mysql -uroot -pMyPassword
  mysql&gt; show databases;
  +--------------------+
  | Database           |
  +--------------------+
  | information_schema |
  | mysql              |
  | performance_schema |
  | test_db            |
  +--------------------+
  </code></pre>
  
  <p>It show me my db : test_db
  But If i access from :</p>
  
  <pre><code>mysql -h localhost -P 3308 -u root -pMyPassword
  mysql&gt; show databases;
  +--------------------+
  | Database           |
  +--------------------+
  | information_schema |
  | mysql              |
  | performance_schema |
  +--------------------+
  </code></pre>
  
  <p>My test_db isn't here.</p>
  
  <p>And the result of <code>docker ps</code></p>
  
  <pre><code>CONTAINER ID        IMAGE                 COMMAND                  CREATED                 STATUS              PORTS                                                                                                    NAMES
  a0de6a691d72        MyContainer           "docker-entrypoint.sh"   3 hours ago             Up 3 hours          9000/tcp, 0.0.0.0:8085-&gt;80/tcp, 0.0.0.0:3308-&gt;3306/tcp, 0.0.0.0:8084-&gt;8000/tcp, 0.0.0.0:8086-&gt;8080/tcp   MyContainer
  </code></pre>
  
  <p>EDIT 2 :
  I am developing a standard docker container for web hosting production environnement. Each host is controlled by ajenti. The host work with an nginx reverse proxy which redistribute websites on correct container. Every thing is wokring well. So here is my Dockerfile :</p>
  
  <pre><code>FROM php:5.6-fpm
  
  RUN apt-get update &amp;&amp; apt-get install -y --no-install-recommends \
      git \
      libxml2-dev \
      python \
      build-essential \
      make \ 
      gcc \
      python-dev \
      locales \
      python-pip
  
  RUN dpkg-reconfigure locales &amp;&amp; \
      locale-gen C.UTF-8 &amp;&amp; \
      /usr/sbin/update-locale LANG=C.UTF-8
  
  ENV LC_ALL C.UTF-8
  
  ARG MYSQL_ROOT_PASSWORD
  
  RUN export DEBIAN_FRONTEND=noninteractive; \
      echo mysql-server mysql-server/root_password password $MYSQL_ROOT_PASSWORD | debconf-set-selections; \
      echo mysql-server mysql-server/root_password_again password $MYSQL_ROOT_PASSWORD | debconf-set-selections;
  
  RUN apt-get update &amp;&amp; apt-get install -y -q mysql-server php5-mysql
  
  RUN rm /etc/apt/apt.conf.d/docker-gzip-indexes
  
  RUN apt-get update &amp;&amp; apt-get install -y wget
  
  RUN wget http://repo.ajenti.org/debian/key -O- | apt-key add -
  RUN echo "deb http://repo.ajenti.org/debian main main debian" &gt; /etc/apt/sources.list.d/ajenti.list
  RUN apt-get update &amp;&amp; apt-get install -y ajenti cron unzip ajenti-v ajenti-v-php-fpm ajenti-v-mysql ajenti-v-nginx
  
  RUN apt-get install -y python-setuptools python-dev \
      &amp;&amp; easy_install -U gevent==1.1b3 \
      &amp;&amp; sed -i -e s/ssl_version=PROTOCOL_SSLv3/ssl_version=PROTOCOL_SSLv23/ /usr/local/lib/python2.7/dist-packages/gevent-1.1b3-py2.7-linux-x86_64.egg/gevent/ssl.py
  
  EXPOSE 80 8000 8080 3306 
  
  RUN mkdir /tmp/tempfiles \
      &amp;&amp; mv /srv /tmp/tempfiles \
      &amp;&amp; mv /var/lib/mysql /tmp/tempfiles \
      &amp;&amp; mv /var/log /tmp/tempfiles \
      &amp;&amp; mv /etc/ajenti /tmp/tempfiles
  
  COPY docker-entrypoint.sh /usr/local/bin/
  RUN ln -s /usr/local/bin/docker-entrypoint.sh /entrypoint.sh # backwards compat
  
  ENTRYPOINT ["docker-entrypoint.sh"]
  </code></pre>
  
  <p>As I said, I wanted to be able do deploy a new container easily. So I created a docker-entrypoint.sh which copy wanted files to my volume when I start the container :</p>
  
  <pre><code>#!/bin/bash
  DIR="/var/lib/mysql"
  # look for empty dir 
  if [ ! "$(ls -A $DIR)" ]; then
      cp -avr /tmp/tempfiles/mysql /var/lib/
  fi
  # rest of the logic
  
  DIR="/srv"
  # look for empty dir 
  if [ ! "$(ls -A $DIR)" ]; then
      cp -avr /tmp/tempfiles/srv /
  fi
  # rest of the logic
  
  DIR="/var/log"
  # look for empty dir 
  if [ ! "$(ls -A $DIR)" ]; then
      cp -avr /tmp/tempfiles/log /var/
  fi
  # rest of the logic
  
  DIR="/etc/ajenti"
  # look for empty dir 
  if [ ! "$(ls -A $DIR)" ]; then
      cp -avr /tmp/tempfiles/ajenti /etc/
  fi
  # rest of the logic
  </code></pre>
  
  <p>Finally, my docker-compose.yml to launch everything and map ports :</p>
  
  <pre><code>version: '2'
  
  services:
      ajenti:
          build: 
              context: ./dockerfiles/                                        
              args:                                                                      
                  MYSQL_ROOT_PASSWORD: MyPassword
          volumes:
              - ./logs:/var/log
              - ./html:/srv
              - ./ajenti:/etc/ajenti
              - ./mysql-data:/var/lib/mysql
              - ./apache2:/etc/apache2
          ports:
              - "8084:8000"
              #NGINX
              - "8085:80"
              #APACHE
              - "8086:8080"
              - "3308:3306"
  </code></pre>
  
  <p>Hope this will help to find a solution !</p>