Docker. Connect from one container to another container with mysql

I have a network:

docker network create -d bridge --subnet container_network

I have one container with mysql running. Run command:

  • access docker VM in private network (docker-machine)
  • Building in docker and deploying in ubuntu [closed]
  • Which one should i use? docker kill or docker stop?
  • azk - How to increase a VM memory in azk?
  • Mono.Btls.MonoBtlsException: Ssl error:1000007d:SSL routines:OPENSSL_internal:CERTIFICATE_VERIFY_FAILED
  • Changing the user's uid in a pre-build docker container (jenkins)
  • docker run -tid -p 3306:3306 --name container_mysql --network container_network container_mysql

    And one container with code:

    docker run -tid -v $(pwd):/code -p 5000:5000 --name container_code --network container_network container_code

    I am trying to access mysql DB from my container with code, but nothing works:

    mysql -h -P 3306 -u root -p
    mysql -h container_mysql -P 3306 -u root -p
    mysql -h -P 3306 -u root -p
    mysql -h -P 3306 -u root -p

    First command gives me:

    ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0


    ERROR 2003 (HY000): Can't connect to MySQL server on <host> (111)

    Any ideas how to achieve the goal?

    Edit 1
    Dockerfile for Mysql container:

    FROM ubuntu:16.04
    RUN apt-get update
    RUN echo "mysql-server mysql-server/root_password password 1234" | debconf-set-selections
    RUN echo "mysql-server mysql-server/root_password_again password 1234" | debconf-set-selections
    RUN apt-get install mysql-server -y

  • WSO2 not compatible with Docker
  • Docker-compose not passing environment variable to container
  • Request hangs for nginx reverse proxy to an ASP.NET 5 web application on docker
  • Saving docker container image
  • pecl install mongo command for mongo-1.5.5.tgz produce invalid tgz error
  • Java RMI Server in Docker Container
  • One Solution collect form web for “Docker. Connect from one container to another container with mysql”

    First of all, you should consider to use networks insted of link. First, link should be deprecated very soon. Second, when you put 2 container on the same network, docker add to the /etc/hosts of both containers a line for resolve the IP from container name. This is for allows you to access to a container with their name. In your case you can access to the mysql container from code container like this

    mysql -h container_mysql

    Look this link for more details about docker networking. This link for more info about docker-compose and networking.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.