Reputation: 289
I have a requirement where I have to read docker run arguments in my entry command.
docker run -d -p 4058:4058 -p 5800-5805:5800-5805 image_name
Dockerfile :
FROM alpine
# some logic in here
CMD ["/bin/sh", "-c", "sh start.sh"]
In my start.sh file, I want to read the -p value( 4058:4058 5800-5805:5800-5805) of docker run.
Apart from using environment variable in docker run, Is there any way to read -p argument in start.sh file.
Upvotes: 2
Views: 653
Reputation: 60066
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 mountingvar/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"
]
Upvotes: 0