2

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.

user7131571
  • 239
  • 2
  • 3
  • 12

2 Answers2

1

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

LinPy
  • 16,987
  • 4
  • 43
  • 57
  • Though by giving the container access to /var/run/docker.sock, it is [trivially easy] to break out of the containment provided by docker and gain access to the host machine. Obviously this is potentially dangerous. – Soumen Mukherjee Oct 11 '19 at 10:30
  • 1
    I already added a link to dind Docs "Although running Docker inside Docker is generally not recommended, there are some legitimate use cases, such as development of Docker itself." – LinPy Oct 11 '19 at 10:31
0

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

Adiii
  • 54,482
  • 7
  • 145
  • 148