https.rst 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. :title: Docker HTTPS Setup
  2. :description: How to setup docker with https
  3. :keywords: docker, example, https, daemon
  4. .. _running_docker_https:
  5. Running Docker with https
  6. =========================
  7. By default, Docker runs via a non-networked Unix socket. It can also optionally
  8. communicate using a HTTP socket.
  9. If you need Docker reachable via the network in a safe manner, you can enable
  10. TLS by specifying the `tlsverify` flag and pointing Docker's `tlscacert` flag to a
  11. trusted CA certificate.
  12. In daemon mode, it will only allow connections from clients authenticated by a
  13. certificate signed by that CA. In client mode, it will only connect to servers
  14. with a certificate signed by that CA.
  15. .. warning::
  16. Using TLS and managing a CA is an advanced topic. Please make you self familiar
  17. with openssl, x509 and tls before using it in production.
  18. Create a CA, server and client keys with OpenSSL
  19. ------------------------------------------------
  20. First, initialize the CA serial file and generate CA private and public keys:
  21. .. code-block:: bash
  22. $ echo 01 > ca.srl
  23. $ openssl genrsa -des3 -out ca-key.pem
  24. $ openssl req -new -x509 -days 365 -key ca-key.pem -out ca.pem
  25. Now that we have a CA, you can create a server key and certificate signing request.
  26. Make sure that `"Common Name (e.g. server FQDN or YOUR name)"` matches the hostname you will use
  27. to connect to Docker or just use '*' for a certificate valid for any hostname:
  28. .. code-block:: bash
  29. $ openssl genrsa -des3 -out server-key.pem
  30. $ openssl req -new -key server-key.pem -out server.csr
  31. Next we're going to sign the key with our CA:
  32. .. code-block:: bash
  33. $ openssl x509 -req -days 365 -in server.csr -CA ca.pem -CAkey ca-key.pem \
  34. -out server-cert.pem
  35. For client authentication, create a client key and certificate signing request:
  36. .. code-block:: bash
  37. $ openssl genrsa -des3 -out client-key.pem
  38. $ openssl req -new -key client-key.pem -out client.csr
  39. To make the key suitable for client authentication, create a extensions config file:
  40. .. code-block:: bash
  41. $ echo extendedKeyUsage = clientAuth > extfile.cnf
  42. Now sign the key:
  43. .. code-block:: bash
  44. $ openssl x509 -req -days 365 -in client.csr -CA ca.pem -CAkey ca-key.pem \
  45. -out client-cert.pem -extfile extfile.cnf
  46. Finally you need to remove the passphrase from the client and server key:
  47. .. code-block:: bash
  48. $ openssl rsa -in server-key.pem -out server-key.pem
  49. $ openssl rsa -in client-key.pem -out client-key.pem
  50. Now you can make the Docker daemon only accept connections from clients providing
  51. a certificate trusted by our CA:
  52. .. code-block:: bash
  53. $ sudo docker -d --tlsverify --tlscacert=ca.pem --tlscert=server-cert.pem --tlskey=server-key.pem \
  54. -H=0.0.0.0:4243
  55. To be able to connect to Docker and validate its certificate, you now need to provide your client keys,
  56. certificates and trusted CA:
  57. .. code-block:: bash
  58. $ docker --tlsverify --tlscacert=ca.pem --tlscert=client-cert.pem --tlskey=client-key.pem \
  59. -H=dns-name-of-docker-host:4243
  60. .. warning::
  61. As shown in the example above, you don't have to run the ``docker``
  62. client with ``sudo`` or the ``docker`` group when you use
  63. certificate authentication. That means anyone with the keys can
  64. give any instructions to your Docker daemon, giving them root
  65. access to the machine hosting the daemon. Guard these keys as you
  66. would a root password!
  67. Other modes
  68. -----------
  69. If you don't want to have complete two-way authentication, you can run Docker in
  70. various other modes by mixing the flags.
  71. Daemon modes
  72. ~~~~~~~~~~~~
  73. - tlsverify, tlscacert, tlscert, tlskey set: Authenticate clients
  74. - tls, tlscert, tlskey: Do not authenticate clients
  75. Client modes
  76. ~~~~~~~~~~~~
  77. - tls: Authenticate server based on public/default CA pool
  78. - tlsverify, tlscacert: Authenticate server based on given CA
  79. - tls, tlscert, tlskey: Authenticate with client certificate, do not authenticate
  80. server based on given CA
  81. - tlsverify, tlscacert, tlscert, tlskey: Authenticate with client certificate,
  82. authenticate server based on given CA
  83. The client will send its client certificate if found, so you just need to drop
  84. your keys into `~/.docker/<ca, cert or key>.pem`