To assign a static IP address to a Docker container on CentOS 7 using pipework, begin by setting up a Linux bridge.
First, modify the physical interface configuration to attach it to a bridge. Edit /etc/sysconfig/network-scripts/ifcfg-ens33:
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=ens33
DEVICE=ens33
ONBOOT=yes
BRIDGE=br0
Next, create the bridge interface configuration at /etc/sysconfig/network-scripts/ifcfg-br0:
TYPE=Bridge
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPADDR=192.168.1.151
GATEWAY=192.168.1.1
PREFIX=24
DNS1=114.114.114.114
DEVICE=br0
ONBOOT=yes
Key points:
TYPE=Bridgeis required for the bridge enterface.BOOTPROTO=staticenables manual IP assignment.BRIDGE=br0in the physical interface links it to the bridge.
After saving both files, activate the bridge without restarting the entire network stack:
ifup br0
Install bridge-utils if not present:
yum install -y bridge-utils
Verify the bridge and its attached interfaces:
brctl show
Expected output should list ens33 under br0.
Proceed to install pipework:
yum install -y git
git clone https://github.com/jpetazzo/pipework.git
cp pipework/pipework /usr/local/bin/
Launch a container in detached mode:
docker run -itd --name="host-152" lys/v1.0 /bin/bash
Assign a static IP on the bridge subnet using pipework:
pipework br0 host-152 192.168.1.152/24@192.168.1.1
This command creates a veth pair: one end attaches to br0, the other appears in side the container as eth1 with the specified IP and gateway.
Confirm the new interface inside the container:
docker exec -it host-152 ip addr show
The output will include an additional interface (eth1) with the assigned static IP 192.168.1.152/24.