Docker配置文件在Ubuntu中不起作用的解决办法

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

环境

1. OS 版本: Ubuntu 16.04 LTS

$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 16.04 LTS
Release:	16.04
Codename:	xenial

2. Docker版本: Docker version 1.11.2, build b9f10c9

$ docker -v
Docker version 1.11.2, build b9f10c9

 

问题描述

最近想使用Docker的Remote API,即使用REST API的形式访问、操作Docker,按照网上的几篇文章配置,发现无法启用Docker的Remote API,于是便有此文。

 

解决办法

1. 设置Docker开机启动(可选)

sudo systemctl enable docker

2. 配置Docker配置文件,Docker配置文件位于 /etc/default/docker ,配置文件如下:

# Customize location of Docker binary (especially for development testing).
#DOCKER="/usr/local/bin/docker"

# Use DOCKER_OPTS to modify the daemon startup options.
#DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4"
DOCKER_OPTS="-H=unix:///var/run/docker.sock -H=0.0.0.0:7654"
# If you need Docker to use an HTTP proxy, it can also be specified here.
#export http_proxy="http://127.0.0.1:3128/"

# This is also a handy place to tweak where Docker's temporary files go.
#export TMPDIR="/mnt/bigdrive/docker-tmp"

注意配置文件中的 

DOCKER_OPTS="-H=unix:///var/run/docker.sock -H=0.0.0.0:7654"

红色部分即为配置启用Docker Remote API,访问端口为7654

3. 确定 docker.service 文件位置

$ systemctl show --property=FragmentPath docker
FragmentPath=/lib/systemd/system/docker.service

 如上,我机器上的docker.service文件位于 /lib/systemd/system/docker.service

4. 修改 docker.service 文件配置

首先查看 docker.service 

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network.target docker.socket
Requires=docker.socket

[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/docker daemon -H fd://
MountFlags=slave
LimitNOFILE=1048576
LimitNPROC=1048576
LimitCORE=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes

我们需要在 [Service] 部分中加入 EnvironmentFile 配置以及给 ExecStart 配置添加 $DOCKER_OPTS 参数,配置后的配置文件如下:

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network.target docker.socket
Requires=docker.socket

[Service]
EnvironmentFile=-/etc/default/docker
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/docker daemon -H fd:// $DOCKER_OPTS
MountFlags=slave
LimitNOFILE=1048576
LimitNPROC=1048576
LimitCORE=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes

5. 重新加载配置文件

systemctl daemon-reload

6. 查看配置参数是否起作用

$ systemctl show docker | grep EnvironmentFile
EnvironmentFile=/etc/default/docker (ignore_errors=yes)

7. 测试 Docker Remote API

curl localhost:7654/info | jq
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1381  100  1381    0     0   196k      0 --:--:-- --:--:-- --:--:--  224k
{
  "ID": "AMPA:7NQC:J6QJ:SMNQ:CFOR:QA5V:NJY5:SJLP:NLFB:EJA7:KHIO:43TO",
  "Containers": 0,
  "ContainersRunning": 0,
  "ContainersPaused": 0,
  "ContainersStopped": 0,
  "Images": 7,
  "Driver": "aufs",
  "DriverStatus": [
    [
      "Root Dir",
      "/var/lib/docker/aufs"
    ],
    [
      "Backing Filesystem",
      "extfs"
    ],
    [
      "Dirs",
      "80"
    ],
    [
      "Dirperm1 Supported",
      "true"
    ]
  ],
  "SystemStatus": null,
  "Plugins": {
    "Volume": [
      "local"
    ],
    "Network": [
      "bridge",
      "null",
      "host"
    ],
    "Authorization": null
  },
  "MemoryLimit": true,
  "SwapLimit": false,
  "KernelMemory": true,
  "CpuCfsPeriod": true,
  "CpuCfsQuota": true,
  "CPUShares": true,
  "CPUSet": true,
  "IPv4Forwarding": true,
  "BridgeNfIptables": true,
  "BridgeNfIp6tables": true,
  "Debug": false,
  "NFd": 14,
  "OomKillDisable": true,
  "NGoroutines": 33,
  "SystemTime": "2016-09-04T21:24:40.951697312+08:00",
  "ExecutionDriver": "",
  "LoggingDriver": "json-file",
  "CgroupDriver": "cgroupfs",
  "NEventsListener": 0,
  "KernelVersion": "4.4.0-28-generic",
  "OperatingSystem": "Ubuntu 16.04 LTS",
  "OSType": "linux",
  "Architecture": "x86_64",
  "IndexServerAddress": "https://index.docker.io/v1/",
  "RegistryConfig": {
    "InsecureRegistryCIDRs": [
      "127.0.0.0/8"
    ],
    "IndexConfigs": {
      "docker.io": {
        "Name": "docker.io",
        "Mirrors": null,
        "Secure": true,
        "Official": true
      }
    },
    "Mirrors": null
  },
  "NCPU": 4,
  "MemTotal": 4125618176,
  "DockerRootDir": "/var/lib/docker",
  "HttpProxy": "",
  "HttpsProxy": "",
  "NoProxy": "",
  "Name": "poet-pc",
  "Labels": null,
  "ExperimentalBuild": false,
  "ServerVersion": "1.11.2",
  "ClusterStore": "",
  "ClusterAdvertise": ""
}

-----------------------------------------------------------------------------------

官方文档链接: https://docs.docker.com/engine/admin/systemd/

转载于:https://my.oschina.net/xuchenglong/blog/742229

你可能感兴趣的:(Docker配置文件在Ubuntu中不起作用的解决办法)