Currently I am trying to set up three different docker containers (php and apache) and let them listen to 4th in which my sql server is running. Does somebody know how can I get my mysql container listen to the 3 other containers?
Access MySQL from 3 other docker containers
1.1k views Asked by RedXIII At
2
There are 2 answers
0
On
You can do a --link mysql:mysql
in the docker run
of the other containers (apache, php...) and it will be availabe as mysql
inside the container. Something like:
docker run -d -p 3306:3306 --name mysql my/mysql
docker run -d -p 8080:8080 --name apache --link mysql:mysql my/apache
Hope it helps.
Containers can interact with each other if they are attached to the same custom docker network. Networks provide "isolation" (i.e., only containers that are connected to the same network can interact), and if you've given your containers a name, containers are able to find each other using the container-name as hostname.
While you can achieve similar results with the legacy
--link
option,--link
For example;
create a network for your application, called "private-net"
Start a MySQL container, named "db", and tell docker to connect it to the "private-net" network. Note that there is no need to "publish" port 3306 for other containers to connect to it; you should only publish (
-p
) a port if you want it to be publicly accessible (for example to make your web server accessible from the internet).Start your application container. Just to demonstrate the principle here; we're using a
mysql
container for this as well, but start it interactively with a shell session, to show how to connect to the database;You can connect/disconnect containers from a network at runtime using the
docker network connect
anddocker network disconnect
commands. Containers can be connected to multiple networks, which can be useful if you have a service that is shared by multiple applications.For more information about Docker networks, read the documentation here: work with network commands