Search code examples
dockerdockerfiledocker-build

Is it possible to set a MAC address for `docker build`?


With docker run, it’s possible to fix the MAC address with the --mac-address option. I’ve looked, and I can’t find a way to fix the MAC address with docker build. I am wanting to dockerize software that has a license fixed to a MAC address (I’m not trying to get around the license; I’m trying to have a more reproducible system architecture).

Thanks!


Solution

  • Let's consider the below Dockerfile

    FROM alpine
    RUN ifconfig | grep -i hwaddr
    

    If you build it using

    docker build .
    

    You get

    Sending build context to Docker daemon  2.048kB
    Step 1/2 : FROM alpine
     ---> 7328f6f8b418
    Step 2/2 : RUN ifconfig | grep -i hwaddr
     ---> Running in c092838dbe31
    eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:02
    Removing intermediate container c092838dbe31
     ---> 7038787f51b8
    

    Now we can't control Mac address of docker build, but we can control the network of build and we can control mac address of a container. So let us launch a container with our mac address

    $ docker run --name mac1234deb06b61 --mac-address="12:34:de:b0:6b:61" -d alpine tail -f /dev/null
    c3579e4685933b757f51c5f9e36d620dbe3a62abd0e0d6a421b5f1c04045061c
    
    $ docker build --network container:mac1234deb06b61 --no-cache .
    Sending build context to Docker daemon  2.048kB
    Step 1/2 : FROM alpine
     ---> 7328f6f8b418
    Step 2/2 : RUN ifconfig | grep -i hwaddr
     ---> Running in 4390f13cbe8f
    eth0      Link encap:Ethernet  HWaddr 12:34:DE:B0:6B:61
    Removing intermediate container 4390f13cbe8f
     ---> b0b5f7321921
    Successfully built b0b5f7321921
    

    As you can see, now the docker build takes a updated mac address