0

I am using some curl command to get the IP of my host. The curl command is running fine in the terminal itself. Here is the curl command I used:

curl ${ECS_CONTAINER_METADATA_URI_V4} 2>/dev/null | jq -r '.Networks[0].IPv4Addresses[0]'

It returns the IP address of my host.

I have a Dockerfile like this:

FROM haproxy:alpine

ENV CONSUL_TEMPLATE_VERSION=0.24.1

RUN apk --update add wget

ENTRYPOINT ["consul-template","-config=/etc/consul-template/data/haproxy.hcl", "-consul-addr=xx.xx.xx.xx:8500"]

I need to pass the output of the curl to the 'xx.xx.xx.xx' in the ENTRYPOINT section of my Dockerfile. What can I try next?

I tried it passing like this, but it's not working:

-consul-addr=${curl ${ECS_CONTAINER_METADATA_URI_V4} 2>/dev/null | jq -r '.Networks[0].IPv4Addresses[0]'}:8500"
halfer
  • 19,824
  • 17
  • 99
  • 186
Deependra Dangal
  • 1,145
  • 1
  • 13
  • 36
  • Is passing [build args](https://docs.docker.com/engine/reference/commandline/build/#set-build-time-variables---build-arg) to your `docker build` command a viable solution ? Something like `docker build --build-arg IP=xx.xx.xx.xx .`, and putting `-consul-addr=$IP:8500` in your `ENTRYPOINT` ? – norbjd May 14 '21 at 08:39
  • Sorry, I am deploying it in Kubernetes/AWS_ECS not in raw docker command. And the IP itself can only be curled when it starts to deploy in the pod/task. Actually, I am getting the IP of the pod/task from the curl. – Deependra Dangal May 14 '21 at 09:09
  • Try double `$$` instead of `$`. In docker-compose this will escape the `$` and delay evaluation of variables from build time to run time if I remember correctly. This might also work for Dockerfiles. (similar to https://stackoverflow.com/a/40621373/9360161 - not sure if it works in Dockerfiles) -- – E. Körner May 14 '21 at 09:22
  • Another solution are `entrypoint.sh` scripts. You then just write your run/entrypoint commands in a script and there you have more possiblities for variables, sub commands etc. Then call your entrypoint script. Just remember `exec` usage for correct signal handling. – E. Körner May 14 '21 at 09:28
  • Do you want to pass the IP at BUILD time or RUN time ? – Philippe May 14 '21 at 12:48
  • @Philippe I want to pass the IP at the run time. – Deependra Dangal May 14 '21 at 12:55
  • Hey, I'm been stuck on a similar problem for some time did you find the solution for this ? – Juggernaut Jul 21 '21 at 17:21

1 Answers1

0

You can put in Dockerfile :

FROM haproxy:alpine

ENV CONSUL_TEMPLATE_VERSION=0.24.1

RUN apk --update add wget

ENTRYPOINT ["consul-template","-config=/etc/consul-template/data/haproxy.hcl"]

And run with

docker run image-id -consul-addr="${curl ${ECS_CONTAINER_METADATA_URI_V4} 2>/dev/null | jq -r '.Networks[0].IPv4Addresses[0]'}:8500"
Philippe
  • 20,025
  • 2
  • 23
  • 32
  • Hi @Philippe, The problem is that I am running it Kubernetes/AWS_ECS. That's why I wanted to include that -consul-addr parameter on the same entrypoint. – Deependra Dangal May 14 '21 at 13:35