I want to get hardware specific info from host into the docker container.
I have 2 Docker web apps client
and server
being deployed to a Docker swarm. I deploy server
as a single container that connects to a database, and I deploy client
as a replicated service to each worker node in cluster. client
sends data back to server
, and server
keeps a database of tables that track data sent from the clients
by unique identifier (MAC address).
I start the client
python app in the Dockerfile as follows, so that it knows its MAC address when it sends data back to server
.
python app.py --mac-address=12345
client
currently starts up using a hard coded mac-address
that I manually type in. I develop on Mac, but deploy to Linux boxes, so some of the workarounds for giving containers access to the host machines' network interfaces does not seem to work in my development environment.
Ideally, I would like to pipe this value (any unique identifier) in from some bash command / script. I have the following script to capture MAC address:
mac=$(ifconfig eth0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}')
echo $mac > 'mac.txt'
But, this has to be run on the host.
I have come across solutions such as:
docker run -e HOST_MAC=$(ifconfig -a | grep -Po 'HWaddr \K.*$') image
And then accessing the environment variable within the python application.
Or this solution:
docker run --rm -v $(pwd)/mybashscript.sh:/mybashscript.sh ubuntu bash /mybashscript.sh
Each solution is running the container individually. I am using docker-compose
or docker stack deploy
from a single manager. So, I do not want to assume that script file is already present on each host machine (worker node).
I lastly thought of "SCPI-ing" the file out of the container to the host, then "SSH-ing" from container to host, executing the script, and capturing the output. But, I have not found the syntax for this. Something like:
scp mac_address.sh user@host_hostname:
max=$(ssh user@host_hostname "mac_address.sh")
I don't care if the unique identifier is MAC address, or some other identifier; but, essentially I would like this unique identifier to persist even if a container goes down and a new one comes up.
Has anyone done something similar?