I would like to install or add a mysql instance in my docker container and would like to access that mysql instance in my application in another docker container. So basically I'm creating one docker container that contains my application and I want to access a mysql inside that container
-
You better stick to https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/#each-container-should-have-only-one-concern . Go with the official MySQL docker and link your application against it. – mzl Jul 18 '17 at 03:25
-
Yeah actually that's what I did. I used the official mysql. But I want to link that to my application container how to achieve this? – MadzQuestioning Jul 18 '17 at 03:29
-
Old style (Looks like its deprecated now) was to start your application with "--link mysql:mysql" ((don't) see https://docs.docker.com/engine/userguide/networking/default_network/dockerlinks/ ). So.. you besser use user defined networks, since its the recommended way (See https://docs.docker.com/engine/userguide/networking/work-with-networks/ ). – mzl Jul 18 '17 at 03:47
-
1Possible duplicate of [Link mysql to application in docker](https://stackoverflow.com/questions/45156912/link-mysql-to-application-in-docker) – Henry Jul 18 '17 at 04:50
1 Answers
You can connect using networks. Take a look at this file which builds a mysql container and connects it to a network. And then connects another container to the same network. The required container accesses the mysql container using its ip address and port. I had to use IP address due to following reasons.
The Docker embedded DNS server enables name resolution for containers connected to a given network. This means that any connected container can ping another container on the same network by its container name. From within container2, you can ping container3 by name.
This functionality is not available for the default bridge network. Both container1 and container2 are connected to the bridge network, but you cannot ping container1 from container2 using the container name.
Source:Working with networks
While I was trying to do this process, I wrote about it in my blog. Might help you.

- 9,599
- 6
- 41
- 57