'How to run docker containers in host network mode using docker-compose?

I need to run containers from docker-compose in host network mode. For example, if I run single container, I write:

docker run --net=host my_image

How to do this part --net=host in docker-compose?



Solution 1:[1]

The equivalent in docker-comopose is network_mode: host see the documentation in: https://docs.docker.com/compose/compose-file/

Solution 2:[2]

add directory network_mode of the networks section to your service definition in your docker-compose.yml

also, pay attention network_mode is incompatible with port_bindings:

version: '3.5'

services:
  server:
    image: custome image
    container_name: "sample"
    command: bash -c "python sample.py"
    
    network_mode: host

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 German
Solution 2 Mahdi Khosravi