一看必会系列:DOCKER_OPTS参数不生效问题

No Comments Docker

 

docker.service

配置docker.service的时候,EnvironmentFile的文件默认配置了/etc/sysconfig/docker(基本配置)、/etc/sysconfig/docker-storage(存储)、/etc/sysconfig/docker-network(网络),我们想要/etc/default/docker 生效,我们就需要添加EnvironmentFile=-/etc/default/docker,让后在ExecStart这个配置中,添加引用的参数$DOCKER_OPTS,下面就是我的配置文件/usr/lib/systemd/system/docker.service

#修改配置文件
vi /usr/lib/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=http://docs.docker.com
After=network.target
Wants=docker-storage-setup.service
Requires=docker-cleanup.timer

[Service]
Type=notify
NotifyAccess=all
KillMode=process
#添加我们自定义的配置文件
EnvironmentFile=-/etc/default/docker #添加配置文件,(-代表ignore error)
EnvironmentFile=-/etc/sysconfig/docker
EnvironmentFile=-/etc/sysconfig/docker-storage
EnvironmentFile=-/etc/sysconfig/docker-network
Environment=GOTRACEBACK=crash
Environment=DOCKER_HTTP_HOST_COMPAT=1
Environment=PATH=/usr/libexec/docker:/usr/bin:/usr/sbin
ExecStart=/usr/bin/dockerd-current \
          –add-runtime docker-runc=/usr/libexec/docker/docker-runc-current \
          –default-runtime=docker-runc \
          –exec-opt native.cgroupdriver=systemd \
          –userland-proxy-path=/usr/libexec/docker/docker-proxy-current \
          $OPTIONS \
          $DOCKER_STORAGE_OPTIONS \
          $DOCKER_NETWORK_OPTIONS \
          $ADD_REGISTRY \
          $BLOCK_REGISTRY \
          $INSECURE_REGISTRY \
          $DOCKER_OPTS #需要引用的参数,也是网卡设定参数
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=1048576
LimitNPROC=1048576
LimitCORE=infinity
TimeoutStartSec=0
Restart=on-abnormal
MountFlags=slave

[Install]
WantedBy=multi-user.target
EnvironmentFile=-/etc/default/docker

修改以后,需要重载,然后重启服务才可以使用/etc/default/docker里定义的DOCKER_OPTS参数

#重载
systemctl daemon-reload
#重启docker服务
service docker restart

docker环境配置文件

vi /etc/sysconfig/docker
DOCKER_OPTS="-b=br0"

#或者直接写数据
echo ‘DOCKER_OPTS="-b=br0"’ >> /etc/default/docker
docker自定义网桥

#安装网桥工具
yum install bridge-utils 

#添加网桥
brctl addbr br0

------中间广告---------

#查看网桥
brctl show

#设定网桥地址和子网掩码
ifconfig br0 192.168.110.1 netmask 255.255.255.0

#设定网桥
echo ‘DOCKER_OPTS="-b=br0"’ >> /etc/default/docker

#配置docker
vi /usr/lib/systemd/system/docker.service
#添加我们自己的配置文件
EnvironmentFile=-/etc/sysconfig/docker
#应用参数
ExecStart=/usr/bin/dockerd-current \
               –add-runtime docker-runc=/usr/libexec/docker/docker-runc-current \
                –default-runtime=docker-runc \
                –exec-opt native.cgroupdriver=systemd \
                –userland-proxy-path=/usr/libexec/docker/docker-proxy-current \
                $OPTIONS \
                $DOCKER_STORAGE_OPTIONS \
                $DOCKER_NETWORK_OPTIONS \
                $ADD_REGISTRY \
                $BLOCK_REGISTRY \
                $INSECURE_REGISTRY \
                $DOCKER_OPTS #添加网桥的参数

#重载
systemctl daemon-reload
#重启docker服务
service docker restart

修改成功 后的网桥信息

查看网桥数据

 

------------以下是正确姿势

网桥修改成功后,网卡的网段和子网掩码都修改了

root@docker:~# docker run –rm  –name b5 busybox ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
104: eth0@if105: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
    link/ether 02:42:c0:a9:6e:02 brd ff:ff:ff:ff:ff:ff
    inet 192.169.110.2/24 brd 192.169.110.255 scope global eth0
       valid_lft forever preferred_lft forever
root@docker:~#

root@docker:~# !ps
ps -ef |grep docker
root     26972     1  0 02:02 ?        00:00:00 /usr/bin/dockerd -H fd:// -b=br0                                                      
root     27376 19710  0 02:05 pts/0    00:00:00 grep –color=auto docker                                                              
root@docker:~#

vim "/lib/systemd/system/docker.service"
[Service]                                                                                                                             
Type=notify                                                                                                                           
# the default is not to use systemd for cgroups because the delegate issues still                                                     
# exists and systemd currently does not support the cgroup feature set required                                                       
# for containers run by docker                                                                                                        
#ExecStart=/usr/bin/dockerd -H fd://                                                                                                  
EnvironmentFile=-/etc/default/docker                                                                                                  
ExecStart=/usr/bin/dockerd -H fd:// $DOCKER_OPTS                                                                                      
ExecReload=/bin/kill -s HUP $MAINPID                                                                                                  
TimeoutSec=0                                                                                                                          
RestartSec=2                                                                                                                          
Restart=always  

发表评论