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?
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.
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