如何利用主机网络进行码头组建?

我想使用码头与主机网络组成。

我有一个访问本地 REST api 的 docker 容器

docker run --net=host -p 18080:8080 -t -i containera

它可以访问运行在 http://127.0.0.1:8080上的主机 REST api。因为我想比例容器 containera我发现码头组成的规模容器。但是来自 文件的 docker 组合文件不工作。Docker 容器不查询 REST API。

我尝试了下面的撰写文件,但属性

version: "3"
services:
web:
image: conatinera:latest
network_mode: "host"
deploy:
replicas: 1
resources:
limits:
cpus: "0.5"
memory: 4G
restart_policy:
condition: on-failure
ports:
- "18080:8080"

但是该消息的属性 network_mode被忽略/不允许

Ignoring unsupported options: network_mode
225191 次浏览

The equivalent configuration for docker-compose v3 is using the network_mode key: https://docs.docker.com/compose/compose-file/compose-file-v3/#network_mode

You should set network_mode to "host" in your docker-compose.yml.

If using docker swarm, see codestation's answer.

Which platform you are on? host mode is working in Linux only AFAIK. If network_mode is not working try network: host?

version: '3.4'
serivces:
some_service:
build:
network: host

i think you should define the docker-compose file like this: This is just an example, please read the docuementation: https://docs.docker.com/compose/compose-file/#network-configuration-reference

version: "3"
services:
web:
image: conatinera:latest
networks:
mynetwork: {}
deploy:
replicas: 1
resources:
limits:
cpus: "0.5"
memory: 4G
restart_policy:
condition: on-failure
ports:
- "18080:8080"
networks:
mynetwork:
external: true
name: host

You are mixing options that are invalid on either compose and swarm deployments.

If you are deploying with docker-compose up then your compose file should be like this:

version: "3"
services:
web:
image: conatinera:latest
network_mode: "host"
restart: on-failure

Te options deploy is ignored on compose mode and the ports option is ignored when using host mode networking. I recommend to don't use host mode networking and use a reverse proxy in another container to balance your scaled containers.


(Feel free to ignore this part of the answer as you clarified that you aren't using swarm deployments).

If you are using swarm deployment then your compose file should be like this:

version: "3.4"
services:
web:
image: conatinera:latest
deploy:
replicas: 1
resources:
limits:
cpus: "0.5"
memory: 4G
restart_policy:
condition: on-failure
networks:
- host


networks:
host:
name: host
external: true

Again, published ports and host mode networking do not mix. Also is probably that your scaling will fail because all the containers will try to bind to the same port. I recommend to don't use host mode networking and let docker load balance your replicas.

I was facing the same problem. I found that when network_mode is set to host, port mapping doesn't work as the container will look for the port of the host. So, removing the port mapping worked for me like the following.

services:
web-abc:
build: ./abc
# ports:
#   - "7000:7000"
volumes:
- .:/code
network_mode: host

network_mode: host is not allowed in swarm mode.

I myself did not have success with networks or network_mode, but if you want to access a network service on the host, you can simply have the host service listen on the docker0 network interface, which is accessible from the container (depending on your network mode) at the same ip address. Proof of concept below.

On the host:

$ ip -4 a | grep docker0
4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0


$ echo hello world | nc -l -p 8888 -s 172.17.0.1

On the container:

$ docker run --rm -it alpine nc -w1 172.17.0.1 8888
hello world

Another way to make a host service accessible to a docker container is to listen on a unix socket, which can be mounted in the container.

In docker compose this worked perfectly

extra_hosts:
- "host.docker.internal:host-gateway"

here is an explanation What is the equivalent of --add-host=host.docker.internal:host-gateway in a Compose file