问题

ERROR: for api-service “host” networkmode is incompatible with portbindings
Traceback (most recent call last):
File “docker-compose”, line 3, in
File “compose/cli/main.py”, line 81, in main
File “compose/cli/main.py”, line 203, in perform_command
File “compose/metrics/decorator.py”, line 18, in wrapper
File “compose/cli/main.py”, line 1186, in up
File “compose/cli/main.py”, line 1182, in up
File “compose/project.py”, line 702, in up
File “compose/parallel.py”, line 108, in parallel_execute
File “compose/parallel.py”, line 206, in producer
File “compose/project.py”, line 688, in do
File “compose/service.py”, line 564, in execute_convergence_plan
File “compose/service.py”, line 480, in _execute_convergence_create
File “compose/parallel.py”, line 108, in parallel_execute
File “compose/parallel.py”, line 206, in producer
File “compose/service.py”, line 478, in
File “compose/service.py”, line 457, in create_and_start
File “compose/service.py”, line 334, in create_container
File “compose/service.py”, line 941, in _get_container_create_options
File “compose/service.py”, line 1073, in _get_container_host_config
File “docker/api/container.py”, line 598, in create_host_config
File “docker/types/containers.py”, line 339, in __init

docker.errors.InvalidArgument: “host” network_mode is incompatible with port_bindings
[10799] Failed to execute script docker-compose

原因

在低版本的docker-compose不会报错,当我在1.29.2版本时,用docker-compose启动报错了,
当使用network_mode: host时,忽略端口映射。
容器将使用主机的网络名称空间,并像机器上的任何其他本地进程一样在网络方面工作。
不涉及端口映射,因为不需要映射到私有网络。
host模式下,内部暴露的端口会自动像宿主机端口那样,所以不需要映射端口,是多此一举。

解决办法

去除ports端口映射关系,或者去除network_mode: host这一行。