Home > Software engineering >  Access localhost from within docker container
Access localhost from within docker container

Time:10-10

I know there are a ton of similar issues, but none of them are working for me!

I'm running docker on Ubuntu 22.04.1 LTS. All I want to do is access a Elasticsearch-Instance that i mapped onto Port localhost:9208 via SSH-Tunnel, without switching the docker containers network mode to host.

Here is my minimal docker-compose.yml example:

version: '3'
services:
  curl_test:
    image: curlimages/curl
    command: "curl http://localhost:9208"

Output:

curl: (7) Failed to connect to localhost port 9208 after 0 ms: Connection refused

I tried:

  • Using the workaround using "host.docker.internal:host-gateway"
  • Specifiying extra_hosts
  • Mapping Port 9208 directly into the container

I know that this minimal example does not work. But all the fixes/changes proposed are not working. So I'm very glad for any suggestions for this problem.

CodePudding user response:

Finally fixed it!

The solution contains two parts:

1. Making hosts localhost accessible to docker container

All it needed was

extra_hosts:
  - "host.docker.internal:host-gateway" 

2. More importantly: An SSH-Tunnel is not a normal localhost per se!

This @felix-k's answer in this post made me realize I had to map the remote port to dockers docker0 interface. All it needed was an additional tunnel:

ssh -N -L 172.17.0.1:9214:localhost:9200 user@remotehost

  • Related