Understanding docker port mapping output of docker ps - docker

If I run my docker container with:
docker run -it -p 5432:5432 postgres-words
Output of docker ps
CONTAINER ID IMAGE COMMAND STATUS PORTS NAMES
512416e853e1 postgres-words "docker-entrypoint.s…" Up 5 seconds 80/tcp, 0.0.0.0:5432->5432/tcp, :::5432->5432/tcp busy_chatelet
But with docker run -it -p 0.0.0.0:5432:5432 postgres-words,
docker ps reports:
CONTAINER ID IMAGE COMMAND STATUS PORTS NAMES
44131e2fa6ff postgres-words "docker-entrypoint.s…" Up 4 seconds 80/tcp, 0.0.0.0:5432->5432/tcp festive_chandrasekhar
My question is that what is the significance/meaning of extra :::5432->5432/tcp in the first case.

:::5432->5432/tcp is referring to IPv6. :: in IPv6 has the same meaning as 0.0.0.0 in IPv4, because you can omit zeros in an IPv6 address and replace them with ::. It is also called the unspecified address. For reference you can also look at this question.

Related

"docker ps" does not show PORT details of a Kubernetes-controlled container

I have two Redis containers running on a K8s worker node. One is controlled by a Deployment (redisdeploymet1) and the other is a standalone Docker container that I created locally on worker1 (outside the knowledge of K8s)“:
root#worker1:~# docker ps | head -1 ; docker ps | grep redis | grep -v pause
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bc7b6fd74187 redis "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 6379/tcp nervous_vaughan
3c6fc536e265 redis "docker-entrypoint.s…" 42 minutes ago Up 42 minutes k8s_redis_redisdeploymet1-847d97
Why shouldn’t we see the PORT details on both entries above? I have actually tested them; both are indeed listening on 6379.
My ultimate goal is to identify which ports a specific Pod is listening on. Let's say the Dockerfile is not available.
Thanks
You can use docker port command.
docker port <container_id>: List port mappings or a specific mapping for the container

How to get complete Port details of running Container

I am learning Docker as a beginner and I am finding one info confusing. Here is step details:
Pulling Image from Docker Hub
Running Image
Now, I am seeing any Half port details in CLI due to which I am not able to ger proper port ID.
But when I am running same Image through KITEMATIC and checking the status of the running container then it is showing me properly.
Please refer Screenshot below for details:
First Line in shared Pic is showing complete details of PORTs( started container in KITEMATIC)
Second-line is not showing complete.
I want to know the reason for this difference and how to resolve it.
In first line of docker ps, you publish the port using below command
docker run -it -p 32773:80 -p 32772:443 static-site
That is why you are seeing HOST_PORT->Container_PORT, to see the same response on another container you need to publish port
docker run -it --rm -p 80:80 -p 443:443 your_image
80:80 mean HostPort:ContainerPort.
Images can expose a port. This is documentation from the image creator to those using the image. It tells you which ports the application inside the container are listening on by default. When you run a container with an exposed port, but you do not publish it on the host, you'll see only the container port listed in the docker container ls. This is again only documentation at this point, no docker networking has been configured to use that container port, so docker is letting you know that inside the container that application is likely listening on that port:
$ docker run -d --name unpublished --rm nginx
63291688813a75a8d9f0d383b4fbef30e93be8e89bd22fc80c2953da65d1d5e9
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
63291688813a nginx "nginx -g 'daemon of…" 41 seconds ago Up 39 seconds 80/tcp unpublished
If you publish a container to a specific port, you'll see that listed as desired:
$ docker run -d --name exact -p 8080:80 --rm nginx
10f82a87d8dce2226c030ca5f23e7983b0f60673c0ec614302dc129dad4ba86d
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
10f82a87d8dc nginx "nginx -g 'daemon of…" 14 seconds ago Up 12 seconds 0.0.0.0:8080->80/tcp exact
And it looks like kitematic is publishing all ports with the -P flag (capital) which looks at all exposed ports and maps them to unused high numbered ports:
$ docker run -d --name publish_all -P --rm nginx
982afb237756e543820810cbd6366c8fa8569a386ff581cd7edc63557004e8c4
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
982afb237756 nginx "nginx -g 'daemon of…" 3 seconds ago Up 2 seconds 0.0.0.0:32768->80/tcp publish_all
If you want the know what port was published on the host, particularly when you tell docker to use unused high numbered ports, you can query that with the port command, e.g. for the publish_all container above:
$ docker container port publish_all 80
0.0.0.0:32768
You can see the exposed ports with an inspect of the image:
$ docker image inspect nginx --format '{{json .Config.ExposedPorts}}' | jq .
{
"80/tcp": {}
}

Expose container on random port on internal IP

I'd like to be able to run a container with the -P parameter while having it bind to the internal 10.10.0.0/22 subnet.
By default once do something like this:
docker run -d -P --name=nginx nginx
It will look something like this:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dbb556c99c81 nginx "/usr/sbin/nginx" 2 minutes ago Up 2 minutes 0.0.0.0:32773->80/tcp nginx
Exposing it on 0.0.0.0/0 Is there a way to make this my internal IP?
You can manually bind to a specific interface when selecting individual ports with -p, e.g.:
$ docker run -d -p 127.0.0.1:8080:80/tcp --name test-nginx nginx
2e07ebc61bcdc82a187a27eabca10211a4c9ac09d66e516e5c176d7282cffe2b
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2e07ebc61bcd nginx "nginx -g 'daemon off" 5 seconds ago Up 2 seconds 443/tcp, 127.0.0.1:8080->80/tcp test-nginx
With -P, it uses the interface configured on the daemon (dockerd or docker daemon). By default that is 0.0.0.0, but you can change it to any other IP:
$ dockerd --help
# ...
--ip=0.0.0.0 Default IP when binding container ports
Note that 0.0.0.0 is listening on all interfaces, so the only reason to specify a specific IP is to lock down the container further, not to open it up for your environment.

Docker port not overriden

I want to expose a docker container port on a different host port.
docker run -d --net="host" --name="couchpotato2" -p 5555:5050 ...
However, I don't get the mapping of 5555 -> 5050. Any idea why?
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
114ae1617632 needo/couchpotato "/sbin/my_init" 3 seconds ago Up 2 seconds couchpotato3
Here is the docker image I'm using:
https://github.com/needo37/couchpotato
Your problem is the use of host networking:
--net="host"
Explained in the documentation
Publishing ports and linking to other containers will not work when
--net is anything other than the default (bridge).

Docker port expose

As a follow up to Linking Docker Containers
I've linked both a mongo, redis container with my nodejs app container successfully. My node app is now running inside my container but for some reason I can't access it:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3f8790bb7cc5 mseay/myapp:latest "/bin/bash" 2 minutes ago Up 2 minutes 0.0.0.0:3000->3000/tcp myapp
5470cdd876eb redis:latest "/entrypoint.sh redi About an hour ago Up About an hour 6379/tcp redis
36c61197d8bd mongo:latest "/entrypoint.sh mong About an hour ago Up About an hour 27017/tcp mongo
Inside my container, my node app is running on port 3000. My docker run command specifies to map 3000 in my container to 3000 on my host:
docker run -i -t -p 3000:3000 --name myapp --link mongo:MONGODB --link redis:REDIS mseay/myapp /bin/bash
docker port shows
3000/tcp -> 0.0.0.0:3000
But, when I goto localhost:3000 or try to curl it:
curl: (7) Failed to connect to localhost port 3000: Connection refused
Apologies for being noobish. Any help is always greatly appreciated!
The issue is that you're using boot2docker. The ports are published on the virtual machine running docker. You can get the ip address of that vm by running boot2docker ip.
So, for your service on port 3000, you could run:
curl http://$(boot2docker ip):3000/

Resources