Here's the result when I type docker ps
:
in docker, i have 3 containers , webapps , redis and rabbitmq, i am confusing how to link container webapps to container redis and rabbitmq container ? , in non docker apps, mywebapps can send message to rabbitmq and write/read reddis. i stil don't have any idea how to link 3 those container, i tried using command like this
docker run --name rabbitmq -p 8080:80 --link webapps:nimmis/apache-php7 -d rabbitmq
but it still not work
here is my config.php on webapps when i try to send messages via rabbitmq
define('HOST', 'localhost');
define('PORT', 5672);
i tried to change localhost with hostname
define('HOST', 'rabbitmq');
define('PORT', 5672);
error messages is connection refused... it seems that in my three container needs to configure/mapping/bind network in same ip ?
You need to link rabbitmq and redis to your webapps container and not the other way arround.
First run redis and rabbitmq containers. Then run webapps container with links to the 2 containers.
Now, to configure redis host in the webapps - its easy. You can simply use env variable 'REDIS_PORT_6379_TCP_ADDR'. Because once a container is linked you get its env variables. and redis exports that variable.
Regarding the rabbitmq host - you can get the ip after the rabbit container is up by:
and then pass it in --env when you run the webapps container.For inter-container dependencies and links, you'll want to use
docker-compose
where you can define the links between containers.In your root directory where you store your Docker files, just make a new file called
docker-compose.yml
and here you can define your containers as services which rely on each other like this:so here in the definition of the
webapps
service, you see itlinks
the other two servicesrabbitmq
andredis
. What this means is that when thewebapps
container is build, an entry to it'shosts
file is made such that the domain nameredis
is translated to the IP and port number of the actual container.You have the option to change the name of how this container is address by using the
service:alias
notation, like how I defined therabbitmq
to use the aliasrabmq
inside the containerwebapps
.To now build and start your containers using
docker-compose
just type:So connecting to another container is as simple as using this alias as the name of the host.
Since you are using
docker-compose
in this case, it creates a docker network automatically to connect all the containers so you shouldn't have to worry about that. But for more information have a look at the docs: https://docs.docker.com/compose/networking/#/specifying-custom-networksIn my experience working with declaratives such as docker-compose.yml is okay, but simply you can use
Linking is a legacy feature. Please use "user defined networks":
Then rerun your containers using this network:
Do the same for other containers that you want connected with each other.
Using "user defined networks", you have an "internal name resolution" at your disposal (somewhat like domain name resolution when visiting websites). You can use the names of the container that you want to refer to, in order to resolve the IP addresses of containers, as long as they are running on the same "user defined network". With this, you can resolve the IP address of the
rabbitmq
container with its name, within other containers, on the same network.All containters on the same "user defined network" will have network connectivity. There is no need for "legacy linking".
You can define your services to use a user-defined network in your
docker-compose.yml
Then do:
Check out the full example at https://docs.docker.com/get-started/part3/