Access host machine service from within docker container

To access the host machine's service running on a local port from within a docker container, use the special URL host.docker.internal to access the host services.

This soluction works on the following installation types of Docker:

  1.     Docker Engine on Linux
  2.     Docker Desktop on Linux, Mac and Windows
  3.     Colima on Mac 
Example using docker

docker run --add-host=host.docker.internal:host-gateway --rm alpine/curl -fsSL http://host.docker.internal:8080/health-check

Example using docker compose

networks:
  main:
    name: my-docker-compose-network
services:
  curl:
    container_name: curl
    image: "alpine/curl"
    extra_hosts:
      - host.docker.internal:host-gateway
    command: curl -fsSL http://host.docker.internal:8080/health-check
    networks:
      main:
        aliases:
          - curl

Comments

Popular posts from this blog

JSON with curl and jq

Import self signed in Linux for Chrome / Chromium headless testing

Colima - Drop In replacement for Docker Desktop for Mac and Linux