Docker: How to curl local server xyz.example.com running on host machine from within a docker container

On my host machine, I have a server xyz.example.com running on nginx. It is not dockerized and not accessible to the outside world, it is simply used for local dev environment.

I would like to be able to access it from within a docker container.

  • gcloud ERROR: (gcloud.app.deploy) Error Response: [3]
  • Pulling images from private registry in Kubernetes
  • How can I get the IP address of a unit started on a machine and use it in another unit with fleet?
  • How to know a process is running under docker
  • Granularity of Docker containers
  • Writing a dockerfile containing a custon mysql installation
  • For example, I’d like to be able to do the following from within my docker container:

    curl xyz.example.com
    

    and to see the same output I see when I run the command on my host machine.

    Also, I am using docker-compose.

  • access docker VM in private network (docker-machine)
  • Functional tests with vagrant in docker
  • Docker Layer's On Disk Location
  • How can I redirect a single port in a docker container to the container's host?
  • Error connecting to RDS Postgreql DB from inside Docker container
  • Nginx SSL inside a docker container
  • 2 Solutions collect form web for “Docker: How to curl local server xyz.example.com running on host machine from within a docker container”

    It’s probably not routing directly to the host’s network. This question may help.

    I guess your container doesn’t know about your xyz.example.com because It is declared in your host’s /etc/host (you said it is a private server)?

    Then you could add the host as extra_host:

    services:
      some-service:
        extra_hosts:
          - "xyz.example.com:HOST_BRIDGE_IP"
    

    The extra_host will add hostname mappings in container’s /etc/host.

    And run your docker-compose.yml replacing HOST_BRIDGE_IP with it’s ip in docker bridge:

    HOST_BRIDGE_IP=$(docker network inspect bridge \
      --format '{{range .IPAM.Config}}{{.Gateway}}{{end}}') \
    && sed -e "s/HOST_BRIDGE_IP/${HOST_BRIDGE_IP}/g" \
    docker-compose.yml | docker-compose --file - up
    

    This would allow virtual host resolution based on DNS in your server.

    Edit: This seems a bit overkill to me, anyone has a better suggestion?

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.