github.com/dinever/docker@v1.11.1/docs/admin/systemd.md (about)

     1  <!--[metadata]>
     2  +++
     3  aliases = ["/engine/articles/systemd/"]
     4  title = "Control and configure Docker with systemd"
     5  description = "Controlling and configuring Docker using systemd"
     6  keywords = ["docker, daemon, systemd,  configuration"]
     7  [menu.main]
     8  parent = "engine_admin"
     9  +++
    10  <![end-metadata]-->
    11  
    12  # Control and configure Docker with systemd
    13  
    14  Many Linux distributions use systemd to start the Docker daemon. This document
    15  shows a few examples of how to customize Docker's settings.
    16  
    17  ## Starting the Docker daemon
    18  
    19  Once Docker is installed, you will need to start the Docker daemon.
    20  
    21      $ sudo systemctl start docker
    22      # or on older distributions, you may need to use
    23      $ sudo service docker start
    24  
    25  If you want Docker to start at boot, you should also:
    26  
    27      $ sudo systemctl enable docker
    28      # or on older distributions, you may need to use
    29      $ sudo chkconfig docker on
    30  
    31  ## Custom Docker daemon options
    32  
    33  There are a number of ways to configure the daemon flags and environment variables
    34  for your Docker daemon.
    35  
    36  The recommended way is to use a systemd drop-in file. These are local files in
    37  the `/etc/systemd/system/docker.service.d` directory. This could also be
    38  `/etc/systemd/system/docker.service`, which also works for overriding the
    39  defaults from `/lib/systemd/system/docker.service`.
    40  
    41  However, if you had previously used a package which had an `EnvironmentFile`
    42  (often pointing to `/etc/sysconfig/docker`) then for backwards compatibility,
    43  you drop a file in the `/etc/systemd/system/docker.service.d`
    44  directory including the following:
    45  
    46      [Service]
    47      EnvironmentFile=-/etc/sysconfig/docker
    48      EnvironmentFile=-/etc/sysconfig/docker-storage
    49      EnvironmentFile=-/etc/sysconfig/docker-network
    50      ExecStart=
    51      ExecStart=/usr/bin/docker daemon -H fd:// $OPTIONS \
    52                $DOCKER_STORAGE_OPTIONS \
    53                $DOCKER_NETWORK_OPTIONS \
    54                $BLOCK_REGISTRY \
    55                $INSECURE_REGISTRY
    56  
    57  To check if the `docker.service` uses an `EnvironmentFile`:
    58  
    59      $ systemctl show docker | grep EnvironmentFile
    60      EnvironmentFile=-/etc/sysconfig/docker (ignore_errors=yes)
    61  
    62  Alternatively, find out where the service file is located:
    63  
    64      $ systemctl show --property=FragmentPath docker
    65      FragmentPath=/usr/lib/systemd/system/docker.service
    66      $ grep EnvironmentFile /usr/lib/systemd/system/docker.service
    67      EnvironmentFile=-/etc/sysconfig/docker
    68  
    69  You can customize the Docker daemon options using override files as explained in the
    70  [HTTP Proxy example](#http-proxy) below. The files located in `/usr/lib/systemd/system`
    71  or `/lib/systemd/system` contain the default options and should not be edited.
    72  
    73  ### Runtime directory and storage driver
    74  
    75  You may want to control the disk space used for Docker images, containers
    76  and volumes by moving it to a separate partition.
    77  
    78  In this example, we'll assume that your `docker.service` file looks something like:
    79  
    80      [Unit]
    81      Description=Docker Application Container Engine
    82      Documentation=https://docs.docker.com
    83      After=network.target docker.socket
    84      Requires=docker.socket
    85  
    86      [Service]
    87      Type=notify
    88      ExecStart=/usr/bin/docker daemon -H fd://
    89      LimitNOFILE=1048576
    90      LimitNPROC=1048576
    91      TasksMax=1048576
    92  
    93      [Install]
    94      Also=docker.socket
    95  
    96  This will allow us to add extra flags via a drop-in file (mentioned above) by
    97  placing a file containing the following in the `/etc/systemd/system/docker.service.d`
    98  directory:
    99  
   100      [Service]
   101      ExecStart=
   102      ExecStart=/usr/bin/docker daemon -H fd:// --graph="/mnt/docker-data" --storage-driver=overlay
   103  
   104  You can also set other environment variables in this file, for example, the
   105  `HTTP_PROXY` environment variables described below.
   106  
   107  To modify the ExecStart configuration, specify an empty configuration followed
   108  by a new configuration as follows:
   109  
   110      [Service]
   111      ExecStart=
   112      ExecStart=/usr/bin/docker daemon -H fd:// --bip=172.17.42.1/16
   113  
   114  If you fail to specify an empty configuration, Docker reports an error such as:
   115  
   116      docker.service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.
   117  
   118  ### HTTP proxy
   119  
   120  This example overrides the default `docker.service` file.
   121  
   122  If you are behind a HTTP proxy server, for example in corporate settings,
   123  you will need to add this configuration in the Docker systemd service file.
   124  
   125  First, create a systemd drop-in directory for the docker service:
   126  
   127      mkdir /etc/systemd/system/docker.service.d
   128  
   129  Now create a file called `/etc/systemd/system/docker.service.d/http-proxy.conf`
   130  that adds the `HTTP_PROXY` environment variable:
   131  
   132      [Service]
   133      Environment="HTTP_PROXY=http://proxy.example.com:80/"
   134  
   135  If you have internal Docker registries that you need to contact without
   136  proxying you can specify them via the `NO_PROXY` environment variable:
   137  
   138      Environment="HTTP_PROXY=http://proxy.example.com:80/" "NO_PROXY=localhost,127.0.0.1,docker-registry.somecorporation.com"
   139  
   140  Flush changes:
   141  
   142      $ sudo systemctl daemon-reload
   143  
   144  Verify that the configuration has been loaded:
   145  
   146      $ systemctl show --property=Environment docker
   147      Environment=HTTP_PROXY=http://proxy.example.com:80/
   148  
   149  Restart Docker:
   150  
   151      $ sudo systemctl restart docker
   152  
   153  ## Manually creating the systemd unit files
   154  
   155  When installing the binary without a package, you may want
   156  to integrate Docker with systemd. For this, simply install the two unit files
   157  (service and socket) from [the github
   158  repository](https://github.com/docker/docker/tree/master/contrib/init/systemd)
   159  to `/etc/systemd/system`.