connect to mysql server in docker

If I run mysql server in first docker container:

docker run --name mysqlserver -e MYSQL_ROOT_PASSWORD=pass -d mysql:5.5

and link second docker container with first container:

  • How do i wait for a db container to be up before my spring-boot app starts
  • Dockerfile COPY {source-…}
  • Where to keep Dockerfile's in a project?
  • how can I support http_proxy or logic control in Dockerfile during docker build?
  • Run apache tomcat server in docker container
  • Creating a container with a variable port
  • docker run --name myapp --link mysqlserver:mysql -d vladimir/app

    and if in second docker container I have python app, where I just want to connect to the database in first container, how to determine what host to use?

  • How to run Google Datalab locally?
  • Cannot connect to mongodb instance from docker: Connection refused
  • “Caching” intermediate Docker build
  • docker does not run on windows error
  • Why docker exec is killing nohup process on exit?
  • kubernetes hpa cannot get cpu consumption
  • One Solution collect form web for “connect to mysql server in docker”

    I am not too familiar with mysql. Looking at the mysql documentation it seems like this might be the sort of command you want:

    import _mysql

    when you did the –link in your apps run, you declared that you are linking the EXPOSEd ports for the container named mysqlserver to the container to be named myapp. Docker updates the /etc/hosts file. So, if you cat the /etc/hosts file you should see a line that looks like: mysql 5c960433e26a mysqlserver

    so this is why the reference in your python to host ‘mysql’ works. The default port is 3306. So, that should connect you. Use this host and port for whatever mysql connection package you are using from python.

    This is how I connect with other objects, on their well known ports. You can also use environment variables, but, I think this might be being phased out…dunno…

    root@d23d8be2a6fa:/# env | grep MYSQL

    so you could modify your connect line to use the environment:

    import os
    myhost = os.environ.get('MYSQL_PORT_3306_TCP_ADDR')
    myport = int(os.environ.get('MYSQL_PORT_3306_TCP_PORT'))
    import _mysql

    Either way. This is discovery. There are more complicated methods as well, using consul, etcd, skydns, registrator. I like the skydns/registrator pairing, registrator watches your docker daemon, when new containers are created it inserts dns records into etcd, which skydns can use to mimic a dns server. Using this technique you get ip and port as well.

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