title: How to use JDBC connection from host to a docker mysql server
body: <p>I am new in docker and I try to connect to a mysql container from the local machine (host).</p>
  
  <p>I pulled the latest version of mysql with: </p>
  
  <pre><code>docker pull mysql/mysql-server:latest
  </code></pre>
  
  <p>and started this (and the myadmin container with the following: </p>
  
  <pre><code>docker run --name mysql1 -e MYSQL_ROOT_PASSWORD=root -d mysql --default-authentication-plugin=mysql_native_password -P 3306 -h localhost
  docker run --name myadmin -d --link mysql1:db -p 8081:80 phpmyadmin/phpmyadmin
  </code></pre>
  
  <p>I can access the phpmyadmin on my local browser (with localhost:8081) and I created a DB named 'userDB'.</p>
  
  <p>Here you can see the docker check:</p>
  
  <pre><code>docker ps
  CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                            NAMES
  03126272c0e3        phpmyadmin/phpmyadmin   "/run.sh supervisord…"   7 minutes ago       Up 7 minutes        9000/tcp, 0.0.0.0:8081-&gt;80/tcp   myadmin
  c8d032921d7c        mysql                   "docker-entrypoint.s…"   8 minutes ago       Up 8 minutes        3306/tcp, 33060/tcp              mysql1
  </code></pre>
  
  <p>On my local JavaEE application (running on an GlassFish Webserver) I try to connect this mysql database with the following commands: </p>
  
  <pre><code>    try {
          Class.forName("com.mysql.jdbc.Driver");
          Connection con = DriverManager.getConnection(
                  "jdbc:mysql://localhost:3306/userDB", "root", "root");
          return con;
      } catch (Exception ex) {
          System.out.println("Database.getConnection() Error --&gt;"
                  + ex.getMessage());
          return null;
      }
  </code></pre>
  
  <p>and got the following exception:
  com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure</p>
  
  <pre><code>The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
  </code></pre>
  
  <p>What mistake do I do? How can i connect my Java application with the mysql docker container? </p>