github.com/caseyhadden/docker@v1.6.2/docs/sources/articles/https.md (about)

     1  page_title: Protecting the Docker daemon Socket with HTTPS
     2  page_description: How to setup and run Docker with HTTPS
     3  page_keywords: docker, docs, article, example, https, daemon, tls, ca, certificate
     4  
     5  # Protecting the Docker daemon Socket with HTTPS
     6  
     7  By default, Docker runs via a non-networked Unix socket. It can also
     8  optionally communicate using a HTTP socket.
     9  
    10  If you need Docker to be reachable via the network in a safe manner, you can
    11  enable TLS by specifying the `tlsverify` flag and pointing Docker's
    12  `tlscacert` flag to a trusted CA certificate.
    13  
    14  In the daemon mode, it will only allow connections from clients
    15  authenticated by a certificate signed by that CA. In the client mode,
    16  it will only connect to servers with a certificate signed by that CA.
    17  
    18  > **Warning**:
    19  > Using TLS and managing a CA is an advanced topic. Please familiarize yourself
    20  > with OpenSSL, x509 and TLS before using it in production.
    21  
    22  > **Warning**:
    23  > These TLS commands will only generate a working set of certificates on Linux.
    24  > Mac OS X comes with a version of OpenSSL that is incompatible with the
    25  > certificates that Docker requires.
    26  
    27  ## Create a CA, server and client keys with OpenSSL
    28  
    29  > **Note**: replace all instances of `$HOST` in the following example with the
    30  > DNS name of your Docker daemon's host.
    31  
    32  First generate CA private and public keys:
    33  
    34      $ openssl genrsa -aes256 -out ca-key.pem 2048
    35      Generating RSA private key, 2048 bit long modulus
    36      ......+++
    37      ...............+++
    38      e is 65537 (0x10001)
    39      Enter pass phrase for ca-key.pem:
    40      Verifying - Enter pass phrase for ca-key.pem:
    41      $ openssl req -new -x509 -days 365 -key ca-key.pem -sha256 -out ca.pem
    42      Enter pass phrase for ca-key.pem:
    43      You are about to be asked to enter information that will be incorporated
    44      into your certificate request.
    45      What you are about to enter is what is called a Distinguished Name or a DN.
    46      There are quite a few fields but you can leave some blank
    47      For some fields there will be a default value,
    48      If you enter '.', the field will be left blank.
    49      -----
    50      Country Name (2 letter code) [AU]:
    51      State or Province Name (full name) [Some-State]:Queensland
    52      Locality Name (eg, city) []:Brisbane
    53      Organization Name (eg, company) [Internet Widgits Pty Ltd]:Docker Inc
    54      Organizational Unit Name (eg, section) []:Boot2Docker
    55      Common Name (e.g. server FQDN or YOUR name) []:$HOST
    56      Email Address []:Sven@home.org.au
    57  
    58  Now that we have a CA, you can create a server key and certificate
    59  signing request (CSR). Make sure that "Common Name" (i.e., server FQDN or YOUR
    60  name) matches the hostname you will use to connect to Docker:
    61  
    62  > **Note**: replace all instances of `$HOST` in the following example with the
    63  > DNS name of your Docker daemon's host.
    64  
    65      $ openssl genrsa -out server-key.pem 2048
    66      Generating RSA private key, 2048 bit long modulus
    67      ......................................................+++
    68      ............................................+++
    69      e is 65537 (0x10001)
    70      $ openssl req -subj "/CN=$HOST" -new -key server-key.pem -out server.csr
    71  
    72  Next, we're going to sign the public key with our CA:
    73  
    74  Since TLS connections can be made via IP address as well as DNS name, they need
    75  to be specified when creating the certificate. For example, to allow connections
    76  using `10.10.10.20` and `127.0.0.1`:
    77  
    78      $ echo subjectAltName = IP:10.10.10.20,IP:127.0.0.1 > extfile.cnf
    79  
    80      $ openssl x509 -req -days 365 -in server.csr -CA ca.pem -CAkey ca-key.pem \
    81        -CAcreateserial -out server-cert.pem -extfile extfile.cnf
    82      Signature ok
    83      subject=/CN=your.host.com
    84      Getting CA Private Key
    85      Enter pass phrase for ca-key.pem:
    86  
    87  For client authentication, create a client key and certificate signing
    88  request:
    89  
    90      $ openssl genrsa -out key.pem 2048
    91      Generating RSA private key, 2048 bit long modulus
    92      ...............................................+++
    93      ...............................................................+++
    94      e is 65537 (0x10001)
    95      $ openssl req -subj '/CN=client' -new -key key.pem -out client.csr
    96  
    97  To make the key suitable for client authentication, create an extensions
    98  config file:
    99  
   100      $ echo extendedKeyUsage = clientAuth > extfile.cnf
   101  
   102  Now sign the public key:
   103  
   104      $ openssl x509 -req -days 365 -in client.csr -CA ca.pem -CAkey ca-key.pem \
   105        -CAcreateserial -out cert.pem -extfile extfile.cnf
   106      Signature ok
   107      subject=/CN=client
   108      Getting CA Private Key
   109      Enter pass phrase for ca-key.pem:
   110  
   111  After generating `cert.pem` and `server-cert.pem` you can safely remove the
   112  two certificate signing requests:
   113  
   114      $ rm -v client.csr server.csr
   115  
   116  With a default `umask` of 022, your secret keys will be *world-readable* and
   117  writable for you and your group.
   118  
   119  In order to protect your keys from accidental damage, you will want to remove their
   120  write permissions. To make them only readable by you, change file modes as follows:
   121  
   122      $ chmod -v 0400 ca-key.pem key.pem server-key.pem
   123  
   124  Certificates can be world-readable, but you might want to remove write access to
   125  prevent accidental damage:
   126  
   127      $ chmod -v 0444 ca.pem server-cert.pem cert.pem
   128  
   129  Now you can make the Docker daemon only accept connections from clients
   130  providing a certificate trusted by our CA:
   131  
   132      $ docker -d --tlsverify --tlscacert=ca.pem --tlscert=server-cert.pem --tlskey=server-key.pem \
   133        -H=0.0.0.0:2376
   134  
   135  To be able to connect to Docker and validate its certificate, you now
   136  need to provide your client keys, certificates and trusted CA:
   137  
   138  > **Note**: replace all instances of `$HOST` in the following example with the
   139  > DNS name of your Docker daemon's host.
   140  
   141      $ docker --tlsverify --tlscacert=ca.pem --tlscert=cert.pem --tlskey=key.pem \
   142        -H=$HOST:2376 version
   143  
   144  > **Note**:
   145  > Docker over TLS should run on TCP port 2376.
   146  
   147  > **Warning**:
   148  > As shown in the example above, you don't have to run the `docker` client
   149  > with `sudo` or the `docker` group when you use certificate authentication.
   150  > That means anyone with the keys can give any instructions to your Docker
   151  > daemon, giving them root access to the machine hosting the daemon. Guard
   152  > these keys as you would a root password!
   153  
   154  ## Secure by default
   155  
   156  If you want to secure your Docker client connections by default, you can move
   157  the files to the `.docker` directory in your home directory -- and set the
   158  `DOCKER_HOST` and `DOCKER_TLS_VERIFY` variables as well (instead of passing
   159  `-H=tcp://$HOST:2376` and `--tlsverify` on every call).
   160  
   161      $ mkdir -pv ~/.docker
   162      $ cp -v {ca,cert,key}.pem ~/.docker
   163      $ export DOCKER_HOST=tcp://$HOST:2376 DOCKER_TLS_VERIFY=1
   164  
   165  Docker will now connect securely by default:
   166  
   167      $ docker ps
   168  
   169  ## Other modes
   170  
   171  If you don't want to have complete two-way authentication, you can run
   172  Docker in various other modes by mixing the flags.
   173  
   174  ### Daemon modes
   175  
   176   - `tlsverify`, `tlscacert`, `tlscert`, `tlskey` set: Authenticate clients
   177   - `tls`, `tlscert`, `tlskey`: Do not authenticate clients
   178  
   179  ### Client modes
   180  
   181   - `tls`: Authenticate server based on public/default CA pool
   182   - `tlsverify`, `tlscacert`: Authenticate server based on given CA
   183   - `tls`, `tlscert`, `tlskey`: Authenticate with client certificate, do not
   184     authenticate server based on given CA
   185   - `tlsverify`, `tlscacert`, `tlscert`, `tlskey`: Authenticate with client
   186     certificate and authenticate server based on given CA
   187  
   188  If found, the client will send its client certificate, so you just need
   189  to drop your keys into `~/.docker/{ca,cert,key}.pem`. Alternatively,
   190  if you want to store your keys in another location, you can specify that
   191  location using the environment variable `DOCKER_CERT_PATH`.
   192  
   193      $ export DOCKER_CERT_PATH=~/.docker/zone1/
   194      $ docker --tlsverify ps
   195  
   196  ### Connecting to the Secure Docker port using `curl`
   197  
   198  To use `curl` to make test API requests, you need to use three extra command line
   199  flags:
   200  
   201      $ curl https://$HOST:2376/images/json \
   202        --cert ~/.docker/cert.pem \
   203        --key ~/.docker/key.pem \
   204        --cacert ~/.docker/ca.pem