

This approach is very similar to yours, I just added the -address argument: NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE I will briefly describe both solutions to show you how it works.įirst, I prepared postgres: # kubectl get pod,svc If you use the host network mode for a container, that container’s network stack is not isolated from the Docker host (the container shares the host’s networking namespace), and the container does not get its own IP-address allocated. As may be found in the Docker host networking documentation: To solve your problem, you can use the host network instead of the bridge network. All newly-started containers connect to a default bridge network unless otherwise specified.By default, kubectl binds only to localhost and therefore it does not work as you expect (see: Kubectl Reference Docs). You can add the -address argument to the kubectl port-forward command with the IP address of the container running this command.I think there are two possible solutions that may help in your case: What I am not able to do is to connect from outside the container with the exposed and published port So the port forwarding is successful, and I am able to connect to the postgres instance from inside the docker container. Output of the docker run command: Forwarding from 127.0.0.1:2223 -> 5432

However, when I do docker exec -ti to the said container and run the above psql command, I am able to connect to postgres.ĬMD ĭocker Run command: docker run -it -name=k8s-conn-12 -p 2223:2223 my_image_name:latest This probably means the server terminated abnormally Now when I am trying to access the postgres through psql -h localhost -p 2223, I am getting the following error: psql: server closed the connection unexpectedly Then I ran the container by publishing the said port ( -p 2223:2223) In the Dockerfile, I have exposed the relevant port 2223. I have a docker container which is running kubectl port-forward, forwarding the port (5432) of a postgres service running as a k8s service to a local port (2223).
