SmxCde
SmxCde

Reputation: 5403

Docker: Access a service on host from a container

I need to access a service running on host machine from a docker container using docker-compose setup file.

Plus at this point I do not know if I will be able to change hostname that app in the container uses so service must be available on 'localhost' host.

I know how to do it with docker run: docker run --net="host" ... - it works exactly as I need: for example i can wget http://localhost from the container and I will receive a file served by host's web server.

What I do not know is how to configure same thing in docker-compose.yml

I tried this

version: '3'

services:

  nginx:
    container_name: cont
    image: ubuntu
    command: sleep 10000000
    networks:
      - host

networks:
  host:

But was not able to reproduce same functionality. How do I do that? Thanks!

Upvotes: 2

Views: 1523

Answers (1)

nickgryg
nickgryg

Reputation: 28593

According to docker-compose documentation you should use network_mode: "host" statement.

The final docker-compose.yaml is:

version: '3'
services:
  nginx:
    container_name: cont
    image: ubuntu
    command: sleep 10000000
    network_mode: "host"

Upvotes: 3

Related Questions