How to use host network for docker compose?
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.