Linking 2 docker container together via creating services (swarm mode) by using docker api

Linking 2 docker container together via creating services (swarm mode) by using docker api



I am using docker api to create containers, networks, services etc but my requirement is that I want my public facing container to communicate with another container running on the same network. For that to happen I can use container names as the hostname along with the port through which I am trying to connect my non-public facing container.



I am not able to find the name of the container via any service related api.
I can definitely use docker-compose's link feature but I am trying to do that via a API. Can anyone help me with this?






I'd set the full URL to the other service in an environment variable on the client container. In a Docker Compose context you'd know its hostname, but this is a setup that transplants well to local development, Kubernetes, etc.

– David Maze
Sep 13 '18 at 21:00






Yep, I used the service name:PORT in an environment variable and it worked. But both the containers should be lifted within the same network.

– Mudit Juneja
Sep 16 '18 at 0:15




1 Answer
1



You can define the requirements for network in docker-compose.yml. See the example in https://docs.docker.com/compose/compose-file/#aliases. In your case, you may not need aliases, but the example in that section has an example.



By default, all the services defined in the compose file will be in one network.



If you want to do it by hand, https://docs.docker.com/engine/reference/commandline/network/ is the command you want. Use docker network create and multiple docker network connect for each of the containers.


docker network create


docker network connect






This is what I want but If I have ssh I can always use docker-compose.yml and start the app but I want to do the entire thing via calling the docker remote api.

– Mudit Juneja
Sep 16 '18 at 0:12



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế