Using the host network and additional networks in docker

I am trying to set up a dev environment for my project.

I have a container (ms1) that needs to be placed on its own network ("services" in my case) and a container (apigateway) that needs to access this network while exposing the http port to the host network.

Ideally, my docker build file would look like this:

version: '2' services: ms1: expose: - "13010" networks: services: aliases: - ms1 apigateway: networks: services: aliases: - api network_mode: "host" networks: services: 

docker-compose does not allow network_mode and networks to be used simultaneously.

Do I have any other alternatives?

I am currently using this:

  apigateway: networks: services: aliases: - api ports: - "127.0.0.1:10000:13010" 

and then the apigateway container listens on 0.0.0.0:13010. It works, but it is slow, and it freezes if the connection to the Internet site is down.

In addition, I plan to use the tramp in the future on the docker, does this solve in a clean way?

+12
docker vagrant networking docker-compose devops
source share
3 answers

I would try this:

1 / Find the host docker network ls

2 / Use this docker file

  services: ms1: ports: - "13010" networks: - service apigateway: networks: - front - service networks: front: service: external: name: "<ID of the network>" 
0
source share

In docker 1.13, you must create a service to connect between two networks. I am using something similar to fix another problem , and I think it can also help here:

 docker service create \ --name proxy \ --network proxy \ --publish mode=host,target=80,published=80 \ --publish mode=host,target=443,published=443 \ --constraint 'node.hostname == myproxynode' \ --replicas 1 \ letsnginx 
0
source share

expose in docker-compose does not publish the port on the host . Since you probably no longer need a service binding (you should rely on the Docker network instead, as you do), this option has limited value in general and does not seem to give you any value in your scenario.

I suspect that you came to use it by mistake, and realizing that it does not seem to have any effect on its own, I came across the fact that using the host network driver "would make it work." Keep in mind that this has nothing to do with the expose property. It is simply that the network driver allows the related processes to communicate directly with the host network interface. Thanks to this, you can communicate with the API gateway from the side. You can remove the expose property and it will work anyway.

If this is the only reason you chose the network driver, then you are the victim of the XY problem:

(Tl; dg) You never need to use the network driver in normal situations, the default network bridge driver works very well. What you are looking for is a ports property, not an expose . This sets up the appropriate port forwarding behind the scenes.

0
source share

All Articles