Docker and Kubernetes

Reach your computer's localhost from inside a container

Inside a container, localhost means the container itself. Use host.docker.internal to reach your own machine.

Fill these in once
  1. From inside the container, use host.docker.internal instead of localhost
    Terminal
    curl http://host.docker.internal:3000

    Docker Desktop sets this name up for you.

  1. Start the container with the host name added
    Terminal · Ubuntu / Debian
    docker run --add-host=host.docker.internal:host-gateway -it alpine sh

    In docker-compose.yml, add extra_hosts with host.docker.internal:host-gateway instead.

  2. From inside the container, use host.docker.internal
    Terminal · Ubuntu / Debian
    curl http://host.docker.internal:3000

    Your app on the host must listen on all addresses (0.0.0.0), not just 127.0.0.1.

  1. From inside the container, use host.docker.internal instead of localhost
    PowerShell
    curl http://host.docker.internal:3000

    Docker Desktop sets this name up for you.