Docker Community Forums

Share and learn in the Docker community.

docker.errors.InvalidArgument: "host" network_mode is incompatible with port_bindings

Hi,

I am trying to install a Nuclias container running and getting an error on the port binding.
Could anyone suggest what needs to be changed with the network?

Thank you

docker-compose version 1.28.0, build d02a7b1a
docker-py version: 4.4.1
CPython version: 3.9.0
OpenSSL version: OpenSSL 1.1.1d 10 Sep 2019

Client: Docker Engine - Community
Version: 20.10.2
API version: 1.41
Go version: go1.13.15
Git commit: 2291f61
Built: Mon Dec 28 16:17:43 2020
OS/Arch: linux/amd64
Context: default
Experimental: true

Server: Docker Engine - Community
Engine:
Version: 20.10.2
API version: 1.41 (minimum version 1.12)
Go version: go1.13.15
Git commit: 8891c58
Built: Mon Dec 28 16:15:19 2020
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.3
GitCommit: 269548fa27e0089a8b8278fc4fc781d7f65a939b
runc:
Version: 1.0.0-rc92
GitCommit: ff819c7e9184c13b7c2607fe6c30ae19403a7aff
docker-init:
Version: 0.19.0
GitCommit: de40ad0

sudo docker network ls
NETWORK ID NAME DRIVER SCOPE
e411c24d32be bridge bridge local
5f833bbd12a8 host host local
fd2830d3a4b6 none null local

Building with native build. Learn about native build in Compose here: https://docs.docker.com/go/compose-native-build/
Creating volume “nuclias_connect_MONGO-DATA” with default driver
Creating mongo …

ERROR: for mongo “host” network_mode is incompatible with port_bindings

ERROR: for DB “host” network_mode is incompatible with port_bindings
Traceback (most recent call last):
File “docker-compose”, line 3, in
File “compose/cli/main.py”, line 80, in main
File “compose/cli/main.py”, line 192, in perform_command
File “compose/metrics/decorator.py”, line 18, in wrapper
File “compose/cli/main.py”, line 1165, in up
File “compose/cli/main.py”, line 1145, in up
File “compose/project.py”, line 703, in up
File “compose/parallel.py”, line 106, in parallel_execute
File “compose/parallel.py”, line 204, in producer
File “compose/project.py”, line 685, in do
File “compose/service.py”, line 558, in execute_convergence_plan
File “compose/service.py”, line 472, in _execute_convergence_create
File “compose/parallel.py”, line 106, in parallel_execute
File “compose/parallel.py”, line 204, in producer
File “compose/service.py”, line 477, in
File “compose/service.py”, line 456, in create_and_start
File “compose/service.py”, line 329, in create_container
File “compose/service.py”, line 935, in _get_container_create_options
File “compose/service.py”, line 1010, in _get_container_host_config
File “docker/api/container.py”, line 598, in create_host_config
File “docker/types/containers.py”, line 338, in init
docker.errors.InvalidArgument: “host” network_mode is incompatible with port_bindings
[10353] Failed to execute script docker-compose
-e Nuclias Connect services are running…

Hi

not sure how you start it, but i guess its some docker-compose command.
In the docker-compose.yml, there is network_mode: host
either delete that line, or whatever is in the ports: array

network_mode: host tells docker to run the container as if it was running on the server itself, so all ports exports by the container will directly be mapped to the server

I had the same problem with network_mode: 'host'.

When downgrading docker-compose from 1.28 to 1.25.4, it worked fine. Maybe some bug added in new versions? :thinking:

2 Likes

Thanks for responding Martin!

It’s my understanding that the container has to run on the host network as it needs to communicate with physical clients on the same network.

How would I downgrade docker-compose to 1.25 or even 1.23 ?

Thank you

Found a tutorial on how to downgrade here.

Hi

there is no need to downgrade, maybe docker-compose didnt care about ports/network_mode in the past, but that dosnt mean you should downgrade to fix this error.

Your container dosnt have to be on the “host” network in order to talk to the other clients on the network/internet.

But if you prefer to run network_mode: host, you can remove the “ports” part from the docker-compose.
Or if you prefer to use the docker network, remove the “network_mode:” part

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 downgraded docker-compose to 1.25.4 as filipetoyoshima suggested and was able to deploy the container

here is the content of the docker-compose.yml file

version: ‘3’
services:
DB:
container_name: mongo
image: ‘mongo:3.6.11’
restart: ‘always’
ports:
- ‘27010:27010’
volumes:
- ‘MONGO-DATA:/data/db’
- ‘/etc/localtime:/etc/localtime’
- ‘./entrypoint-initdb.sh:/docker-entrypoint-initdb.d/entrypoint-initdb.’
network_mode: host
environment:
- MONGO_INITDB_ROOT_USERNAME=admin# - MONGO_INITDB_ROOT_PASSWORD=admin
command: ‘mongod --port 27010 --auth’

CORE:
environment:
- NODE_ENV=Production_hualian
container_name: nuclias_connect_core
image: ‘nuclias/nuclias_connect_core’
restart: ‘always’
ports:
- ‘8443:8443’
- ‘62992:62992’
volumes:
- ‘./appconfig.json:/app/config/appconfig.json’
- ‘./dbConfig.js:/app/node_modules/DView-SP1-common/db/dbConfig.js’
- ‘./log/core/logFiles:/app/logFiles’
- ‘/etc/localtime:/etc/localtime’
network_mode: host
depends_on:
- DB
command: ‘./nuclias_connect_core’
privileged: true

WEB:
environment:
- NODE_ENV=Production_hualian
container_name: nuclias_connect_web
image: ‘nuclias/nuclias_connect_web’
restart: ‘always’
ports:
- ‘30001:30001’
volumes:
- ‘./config/systemconfig.json:/app/config/systemconfig.json’
- ‘./customer:/app/customer’
- ‘./dbConfig.js:/app/node_modules/DView-SP1-common/db/dbConfig.js’
- ‘./log/web/logFiles:/app/logFiles’
- ‘/etc/localtime:/etc/localtime’
network_mode: host
depends_on:
- CORE
command: ‘./nuclias_connect_web’
privileged: true

volumes:
MONGO-DATA: