I want to use docker compose with the host network.
I have a docker container that access a local REST api. Usually I run
docker run --net=host -p 18080:8080 -t -i containera
which can access the host REST api which runs at http://127.0.0.1:8080
. Since I want to scale the container containera
I found docker compose to scale the container. But the docker compose file from the documentation does not work. The docker container does not query the REST API.
I tried the following compose file but the property
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"
but the property network_mode
is ignored/not allowed. with the message
Ignoring unsupported options: network_mode
If you specify the --net=host option to the docker create or docker run commands, Docker uses the host's network stack for the container. The network configuration of the container is the same as that of the host and the container shares the service ports that are available to the host.
Accessing the Host With the Default Bridge Mode You just need to reference it by its Docker network IP, instead of localhost or 127.0. 0.1 . Your host's Docker IP will be shown on the inet line. Connect to this IP address from within your containers to successfully access the services running on your host.
Docker Compose sets up a single network for your application(s) by default, adding each container for a service to the default network. Containers on a single network can reach and discover every other container on the network.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With