Docker配置文件在Ubuntu中不起做用的解決辦法

環境

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,因而便有此文。linux

 

解決辦法

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

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

紅色部分即爲配置啓用Docker Remote API,訪問端口爲7654bash

3. 肯定 docker.service 文件位置

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

 如上,我機器上的docker.service文件位於 /lib/systemd/system/docker.servicecurl

4. 修改 docker.service 文件配置

首先查看 docker.service socket

[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": ""
}

-----------------------------------------------------------------------------------ui

官方文檔連接: https://docs.docker.com/engine/admin/systemd/url

相關文章
相關標籤/搜索