user7131571
user7131571

Reputation: 289

How to read docker run arguments (-p in specific) inside docker container

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

Answers (2)

Adiii
Adiii

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 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"
]

Upvotes: 0

LinPy
LinPy

Reputation: 18598

The only way for now is to mount /var/run/docker.sock in your docker aka dind.

maybe this will be implemented someday , but the issue is from 2014 :). here

Upvotes: 1

Related Questions