There is no way to get Bind port inside the container itself unless you mount /var/run/docker.sock
. but you should not ignore the hight of risk by doing this so better to go with ENV.
The owner of the docker /var/run/docker.sock
is root of the host where
the container is running, with default group membership to docker
group. That's why mounting var/run/docker.sock
inside another
container gives you root privileges since now you can do anything that
a root user with group membership of docker can.
So I will suggest going through docker-security-best-practices
But still, if you want to go and you should aware of the risk then you can do this to get the bind port.
docker run --rm -it --name test -p 3000:3000 -v /var/run/docker.sock:/var/run/docker.sock alpine:latest sh -c "apk add --no-cache curl jq && sh"
then run
docker exec -it test ash
curl -s --unix-socket /var/run/docker.sock http:/v1.26/containers/$(hostname)/json | jq \'.NetworkSettings | .Ports | keys\'
#output:
[
"3000/tcp"
]