<p>I have already googled on this subject and found few threads. Based on these threads I have followed the following steps. But I am facing a problem.</p>

<p>Basically, I want to create a docker image for mysql and then connect to it from my host machine (Mac OS X).</p>

<p>Based on <a href="https://stackoverflow.com/questions/33001750/connect-to-mysql-in-a-docker-container-from-the-host">this</a> post , I have to share the mysql unix socket with the host. towards this I have done the following steps</p>

<pre><code>1. Start docker quick terminal
2. docker run --name mysql -e MYSQL_ROOT_PASSWORD=password -d mysql/mysql-server:latest
3. docker exec -it mysql bash
4. mysql -uroot -p
5. create database MyDB;
6. GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password';
7. exit;
8. mkdir /Users/abhi/host
9. docker run -it -v /host:/shared mysql/mysql-server:latest
</code></pre>

<p>Now I get the error</p>

<pre><code>MacBook-Pro:~$ docker run -it -v /Users/abhi/host:/shared mysql/mysql-server
error: database is uninitialized and password option is not specified
  You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
</code></pre>

<p>But you see that I have provided the password and initialized my database.</p>

<p>All I want is that from my host machine, I can connect to the mysql database running inside docker.</p>

<p>EDIT:: ----- solution which worked ------</p>

<p>Thanks RICO. Finally the steps which worked for me are</p>

<pre><code>1. Start docker quick terminal
2. docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -d mysql/mysql-server:latest
3. docker exec -it mysql bash
4. mysql -uroot -p
5. create database MyDB;

  or:
  CREATE USER 'root'@'%' IDENTIFIED BY 'root';
  GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;

6. GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password';
7. exit;
8. docker-machine env default
</code></pre>

<p>Use the IP address obtained in step 8. port is 3306, user is root, password is password, database is MyDB.</p>

<p>Connection is successful!</p>

Knows Not Much
<p>So you basically you need to expose the mysql port to your host:</p>

<pre><code>docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -d mysql/mysql-server:latest
</code></pre>

<p>Then you can access from your host using the mysql command line:</p>

<pre><code>mysql -h127.0.0.1 -ppassword -uroot
</code></pre>

<p>Not sure why you are trying to run another container to connect (perhaps you meant linking two containers)</p>

<p>If you are using Mac (or Windows) with docker-machine you want to connect to the IP address of your docker-machine VM. For example:</p>

<pre><code>$ docker-machine ssh default
                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ o           __/
             \    \         __/
              \____\_______/
 _                 _   ____     _            _
| |__   ___   ___ | |_|___ \ __| | ___   ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__|   &lt;  __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 1.9.0, build master : 16e4a2a - Tue Nov  3 19:49:22 UTC 2015
Docker version 1.9.0, build 76d6bc9
docker@default:~$ ifconfig eth1
eth1      Link encap:Ethernet  HWaddr 08:00:27:E6:C7:20
          inet addr:192.168.99.100  Bcast:192.168.99.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fee6:c720/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:18827 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10280 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1791527 (1.7 MiB)  TX bytes:2242596 (2.1 MiB)
</code></pre>

<p>Then connect to:</p>

<pre><code>mysql -h192.168.99.100 -ppassword -uroot
</code></pre>
