Docker: Install and run with custom bridge interface

Docker installation and configuration with custom name of the bridge and IP address.

More information about Docker you can find on official web site: www.docker.com. Also you can got images from DockerHub web site.

Overview

All steps described in this article made on DigitalOcean VPS, CentOS6.8. I use docker version 1.7.1 because of latest version of it does not support CentOS6 (kernel) anymore.

For my docker guests containers I will use 172.31.128.0/24 network. The IP address 172.31.128.1 I using as my bridge interface IP.

Installation

Add official docker repo for yours docker installation. See docker documentation for more info.

Remember that I use CentOS6 in this article so if you are using CentOS7 you will need to change repo baseurl!

cat << EOF > /etc/yum.repos.d/docker.repo  
[dockerrepo]
name=Docker Repository  
baseurl=https://yum.dockerproject.org/repo/main/centos/6/  
enabled=1  
gpgcheck=1  
gpgkey=https://yum.dockerproject.org/gpg  
EOF  

And now just install Docker.

yum install docker-engine -y  

You can verify installation and list docker processes

docker version  
docker ps -a  

Configuration

At first stop the docker engine

service docker stop  

Find and delete existing docker bridge interface

brctl show  
ip link set dev docker0 down  
brctl delbr docker0  

Create new bridge configuration file and up new interface

cat << EOF > /etc/sysconfig/network-scripts/ifcfg-br0  
DEVICE=br0  
STP=no  
TYPE=Bridge  
BOOTPROTO=none  
IPV4_FAILURE_FATAL=no  
IPV6INIT=no  
NAME=br0  
ONBOOT=yes  
IPADDR=172.31.128.1  
PREFIX=24  
DELAY=0  
NM_CONTROLLED=no  
EOF  
ifup br0  

Also we need to update iptables rules because we are changed the Docker network configuration. Do not forget to reload iptables!

sed -i 's/-s [0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\/16 \! -o/-s 172.31.128.0\/24 \! -o/' /etc/sysconfig/iptables  
sed -i 's/docker0/br0/g' /etc/sysconfig/iptables  
service iptables reload  

Warning: If you are not sure or not understood previously 'sed' example -- please edit all files by hand!

Next step is a configuration of the Docker startup args and start docker engine.

sed -i 's/other_args=""/other_args="--fixed-cidr=172.31.128.0\/24 --bridge=br0"/g' /etc/sysconfig/docker  
service docker start  

Start Container

When docker engine has started you can try to run bash from container and ping some hosts.

docker run -it centos /bin/bash  

[ ! ] To run container as a daemonized using -d option, e.g:

docker run --name centos-linux -d centos /bin/sh -c "while true; do ping 8.8.8.8; done"  

Also you can use docker exec to reattach to your container interactive shell or run some command

docker exec -it centos-linux /bin/bash  
docker exec -it centos-linux ip add show  

When you finish just use docker stop to stop your container.

Tips

You can map some directory or file into container -- .bash_histroy for example.

docker run --rm -it -v ~/.bash_history:/root/.bash_history centos-linux /bin/bash  

Also now you can remove the Docker container and image...

docker rm d38a6162c6b8  
docker rmi centos