I find `kubectl port-forward` really helpful for accessing internal services in Kubernetes temporarily. I'm looking for a clean equivalent in Docker or Docker Compose that can achieve the same temporary exposure of an internal service. Any suggestions?
3 Answers
You don't really have a direct equivalent command in Docker like `kubectl port-forward`. However, you can run a separate Nginx container to act as a proxy for your existing container by using something like:
alias docker-proxy='docker run --rm -p 8080:80 nginx --network'
Then, just point it to the container ID you want to proxy. It's not as direct, but it works!
Typically, any service running inside a Docker container should be accessible on the host directly. You can use `docker inspect` to find the container's IP address and just connect using the service's port. If you expose the port, it’ll be available on `127.0.0.1` by default, too.
To mimic `kubectl port-forward`, you can use `docker run --rm -it --network container: nicolaka/netshoot` to create a debug container in the same network namespace. This way, you can use curl or nc directly to localhost and the service you need without any port mappings.

Interesting! But is that an SSH tunnel? I was hoping for something simpler to just expose a port for accessing it through my local browser.