0

I need to pass the wlan0 ip address as environment variable in Dockerfile. As this on DHCP so the IP address changes some time. I thought of running the below command to get the ip address and then use it in Dockerfile:

ip -4 addr show wlan0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'

Above commands gives me 192.168.1.125. I want to use this ip address to pass it as environment variable. For this, I used:

RUN wlan="$(ip -4 addr show wlan0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}')" && echo $wlan

ENV HOST_WLAN0=$wlan

But it gave me an error Device "wlan0" does not exist.. How can I resolve this.?

1
  • the run command is executed inside the container you are configuring. but i guess wlan0 belongs to the docker host system. I would also guess that you have an issue in the design of your planned solution. the docker container should not know such infrastructur details from the host system. Commented Jan 4, 2018 at 7:09

2 Answers 2

2

You are getting this error because when building the image, the command executed is being "isolated" from the host and runs in a separate namespaced layer.

What you need to do is pass the ip address as a build arg.

Dockerfile:

...
ARG wlan
ENV HOST_WLAN0=$wlan

Build command:

docker build --build-arg wlan=$(ip -4 addr show wlan0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}') ...

This will input the correct IP address into the Docker build.

Sign up to request clarification or add additional context in comments.

7 Comments

While building it says unknown flag: --build-args
Fixed there was a typo.
Okay so I have found an issue with this too. When we run the build command giving the build arguments, it runs sucessfully. Then I start its container with the flag --restart=always becaus I want it to be restarted always. But when the ip address changes as it is on DHCP, it still shows the older one. To make it use new IP address, I had to run the build command again.
In that case pass it as an env variable to the run command: docker run -e HOST_WLAN0=$(ip -4 addr show wlan0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}') ..
Okay if I am doing this then I will have to remove ARG wlan & ENV HOST_WLAN0=$wlan from the dockerfile.?
|
0

The command (ip -4 addr show wlan0 | grep -oP '(?<=inet\s)\d+(.\d+){3}') which provides output is running in Host machine thats why you are getting output of it.

When you try to run the same command through Dockerfile, It will create temporary container start running the RUN command and there it wont find wlan0 device. Because of this reason you are getting the error "device "wlan0" does not exist."

I have a suggestion

To overcome your problem create a cron job in host machine which it updates IPaddress automatically into the file and place it in Volume. Share the volume with the container and access this file from container and use it in your application

1 Comment

Your suggestion is great. I will try it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.