docker.asciidoc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. [[docker]]
  2. === Install Elasticsearch with Docker
  3. Elasticsearch is also available as Docker images.
  4. The images use https://hub.docker.com/_/centos/[centos:7] as the base image and
  5. are available with {xpack-ref}/xpack-introduction.html[X-Pack].
  6. A list of all published Docker images and tags can be found in https://www.docker.elastic.co[www.docker.elastic.co]. The source code can be found
  7. on https://github.com/elastic/elasticsearch-docker/tree/{branch}[GitHub].
  8. ==== Image types
  9. The images are available in three different configurations or "flavors". The
  10. `basic` flavor, which is the default, ships with X-Pack Basic features
  11. pre-installed and automatically activated with a free licence. The `platinum`
  12. flavor features all X-Pack functionally under a 30-day trial licence. The `oss`
  13. flavor does not include X-Pack, and contains only open-source Elasticsearch.
  14. NOTE: {xpack-ref}/xpack-security.html[X-Pack Security] is enabled in the `platinum`
  15. image. To access your cluster, it's necessary to set an initial password for the
  16. `elastic` user. The initial password can be set at start up time via the
  17. `ELASTIC_PASSWORD` environment variable:
  18. ["source","txt",subs="attributes"]
  19. --------------------------------------------
  20. docker run -e ELASTIC_PASSWORD=MagicWord {docker-repo}-platinum:{version}
  21. --------------------------------------------
  22. NOTE: The `platinum` image includes a trial license for 30 days. After that, you
  23. can obtain one of the https://www.elastic.co/subscriptions[available
  24. subscriptions] or revert to a Basic licence. The Basic license is free and
  25. includes a selection of X-Pack features.
  26. Obtaining Elasticsearch for Docker is as simple as issuing a +docker pull+ command against the Elastic Docker registry.
  27. ifeval::["{release-state}"=="unreleased"]
  28. WARNING: Version {version} of Elasticsearch has not yet been released, so no
  29. Docker image is currently available for this version.
  30. endif::[]
  31. ifeval::["{release-state}"!="unreleased"]
  32. Docker images can be retrieved with the following commands:
  33. ["source","sh",subs="attributes"]
  34. --------------------------------------------
  35. docker pull {docker-repo}:{version}
  36. docker pull {docker-repo}-platinum:{version}
  37. docker pull {docker-repo}-oss:{version}
  38. --------------------------------------------
  39. endif::[]
  40. [[docker-cli-run]]
  41. ==== Running Elasticsearch from the command line
  42. [[docker-cli-run-dev-mode]]
  43. ===== Development mode
  44. ifeval::["{release-state}"=="unreleased"]
  45. WARNING: Version {version} of the Elasticsearch Docker image has not yet been released.
  46. endif::[]
  47. ifeval::["{release-state}"!="unreleased"]
  48. Elasticsearch can be quickly started for development or testing use with the following command:
  49. ["source","sh",subs="attributes"]
  50. --------------------------------------------
  51. docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" {docker-image}
  52. --------------------------------------------
  53. endif::[]
  54. [[docker-cli-run-prod-mode]]
  55. ===== Production mode
  56. [[docker-prod-prerequisites]]
  57. [IMPORTANT]
  58. =========================
  59. The `vm.max_map_count` kernel setting needs to be set to at least `262144` for production use.
  60. Depending on your platform:
  61. * Linux
  62. +
  63. The `vm.max_map_count` setting should be set permanently in /etc/sysctl.conf:
  64. +
  65. [source,sh]
  66. --------------------------------------------
  67. $ grep vm.max_map_count /etc/sysctl.conf
  68. vm.max_map_count=262144
  69. ----------------------------------
  70. +
  71. To apply the setting on a live system type: `sysctl -w vm.max_map_count=262144`
  72. +
  73. * macOS with https://docs.docker.com/engine/installation/mac/#/docker-for-mac[Docker for Mac]
  74. +
  75. The `vm.max_map_count` setting must be set within the xhyve virtual machine:
  76. +
  77. ["source","sh"]
  78. --------------------------------------------
  79. $ screen ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/tty
  80. --------------------------------------------
  81. +
  82. Log in with 'root' and no password.
  83. Then configure the `sysctl` setting as you would for Linux:
  84. +
  85. ["source","sh"]
  86. --------------------------------------------
  87. sysctl -w vm.max_map_count=262144
  88. --------------------------------------------
  89. +
  90. * Windows and macOS with https://www.docker.com/products/docker-toolbox[Docker Toolbox]
  91. +
  92. The `vm.max_map_count` setting must be set via docker-machine:
  93. +
  94. ["source","txt"]
  95. --------------------------------------------
  96. docker-machine ssh
  97. sudo sysctl -w vm.max_map_count=262144
  98. --------------------------------------------
  99. =========================
  100. The following example brings up a cluster comprising two Elasticsearch nodes.
  101. To bring up the cluster, use the <<docker-prod-cluster-composefile,`docker-compose.yml`>> and just type:
  102. ifeval::["{release-state}"=="unreleased"]
  103. WARNING: Version {version} of Elasticsearch has not yet been released, so a
  104. `docker-compose.yml` is not available for this version.
  105. endif::[]
  106. ifeval::["{release-state}"!="unreleased"]
  107. ["source","sh"]
  108. --------------------------------------------
  109. docker-compose up
  110. --------------------------------------------
  111. endif::[]
  112. [NOTE]
  113. `docker-compose` is not pre-installed with Docker on Linux.
  114. Instructions for installing it can be found on the
  115. https://docs.docker.com/compose/install/#install-using-pip[Docker Compose webpage].
  116. The node `elasticsearch` listens on `localhost:9200` while `elasticsearch2`
  117. talks to `elasticsearch` over a Docker network.
  118. This example also uses https://docs.docker.com/engine/tutorials/dockervolumes[Docker named volumes], called `esdata1` and `esdata2` which will be created if not already present.
  119. [[docker-prod-cluster-composefile]]
  120. `docker-compose.yml`:
  121. ifeval::["{release-state}"=="unreleased"]
  122. WARNING: Version {version} of Elasticsearch has not yet been released, so a
  123. `docker-compose.yml` is not available for this version.
  124. endif::[]
  125. ifeval::["{release-state}"!="unreleased"]
  126. ["source","yaml",subs="attributes"]
  127. --------------------------------------------
  128. version: 2.2
  129. services:
  130. elasticsearch:
  131. image: {docker-image}
  132. container_name: elasticsearch
  133. environment:
  134. - cluster.name=docker-cluster
  135. - bootstrap.memory_lock=true
  136. - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
  137. ulimits:
  138. memlock:
  139. soft: -1
  140. hard: -1
  141. volumes:
  142. - esdata1:/usr/share/elasticsearch/data
  143. ports:
  144. - 9200:9200
  145. networks:
  146. - esnet
  147. elasticsearch2:
  148. image: {docker-image}
  149. container_name: elasticsearch2
  150. environment:
  151. - cluster.name=docker-cluster
  152. - bootstrap.memory_lock=true
  153. - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
  154. - "discovery.zen.ping.unicast.hosts=elasticsearch"
  155. ulimits:
  156. memlock:
  157. soft: -1
  158. hard: -1
  159. volumes:
  160. - esdata2:/usr/share/elasticsearch/data
  161. networks:
  162. - esnet
  163. volumes:
  164. esdata1:
  165. driver: local
  166. esdata2:
  167. driver: local
  168. networks:
  169. esnet:
  170. --------------------------------------------
  171. endif::[]
  172. To stop the cluster, type `docker-compose down`. Data volumes will persist, so it's possible to start the cluster again with the same data using `docker-compose up`.
  173. To destroy the cluster **and the data volumes**, just type `docker-compose down -v`.
  174. ===== Inspect status of cluster:
  175. ["source","txt"]
  176. --------------------------------------------
  177. curl http://127.0.0.1:9200/_cat/health
  178. 1472225929 15:38:49 docker-cluster green 2 2 4 2 0 0 0 0 - 100.0%
  179. --------------------------------------------
  180. // NOTCONSOLE
  181. Log messages go to the console and are handled by the configured Docker logging driver. By default you can access logs with `docker logs`.
  182. [[docker-configuration-methods]]
  183. ==== Configuring Elasticsearch with Docker
  184. Elasticsearch loads its configuration from files under `/usr/share/elasticsearch/config/`. These configuration files are documented in <<settings>> and <<jvm-options>>.
  185. The image offers several methods for configuring Elasticsearch settings with the conventional approach being to provide customized files, i.e. `elasticsearch.yml`, but it's also possible to use environment variables to set options:
  186. ===== A. Present the parameters via Docker environment variables
  187. For example, to define the cluster name with `docker run` you can pass `-e "cluster.name=mynewclustername"`. Double quotes are required.
  188. ===== B. Bind-mounted configuration
  189. Create your custom config file and mount this over the image's corresponding file.
  190. For example, bind-mounting a `custom_elasticsearch.yml` with `docker run` can be accomplished with the parameter:
  191. ["source","sh"]
  192. --------------------------------------------
  193. -v full_path_to/custom_elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
  194. --------------------------------------------
  195. IMPORTANT: The container **runs Elasticsearch as user `elasticsearch` using uid:gid `1000:1000`**. Bind mounted host directories and files, such as `custom_elasticsearch.yml` above, **need to be accessible by this user**. For the https://www.elastic.co/guide/en/elasticsearch/reference/current/important-settings.html#path-settings[data and log dirs], such as `/usr/share/elasticsearch/data`, write access is required as well. Also see note 1 below.
  196. ===== C. Customized image
  197. In some environments, it may make more sense to prepare a custom image containing your configuration. A `Dockerfile` to achieve this may be as simple as:
  198. ["source","sh",subs="attributes"]
  199. --------------------------------------------
  200. FROM docker.elastic.co/elasticsearch/elasticsearch:{version}
  201. COPY --chown=elasticsearch:elasticsearch elasticsearch.yml /usr/share/elasticsearch/config/
  202. --------------------------------------------
  203. You could then build and try the image with something like:
  204. ["source","sh"]
  205. --------------------------------------------
  206. docker build --tag=elasticsearch-custom .
  207. docker run -ti -v /usr/share/elasticsearch/data elasticsearch-custom
  208. --------------------------------------------
  209. ===== D. Override the image's default https://docs.docker.com/engine/reference/run/#cmd-default-command-or-options[CMD]
  210. Options can be passed as command-line options to the Elasticsearch process by
  211. overriding the default command for the image. For example:
  212. ["source","sh"]
  213. --------------------------------------------
  214. docker run <various parameters> bin/elasticsearch -Ecluster.name=mynewclustername
  215. --------------------------------------------
  216. ==== Notes for production use and defaults
  217. We have collected a number of best practices for production use.
  218. Any Docker parameters mentioned below assume the use of `docker run`.
  219. . By default, Elasticsearch runs inside the container as user `elasticsearch` using uid:gid `1000:1000`.
  220. +
  221. CAUTION: One exception is https://docs.openshift.com/container-platform/3.6/creating_images/guidelines.html#openshift-specific-guidelines[Openshift] which runs containers using an arbitrarily assigned user ID. Openshift will present persistent volumes with the gid set to `0` which will work without any adjustments.
  222. +
  223. If you are bind-mounting a local directory or file, ensure it is readable by this user, while the <<path-settings,data and log dirs>> additionally require write access. A good strategy is to grant group access to gid `1000` or `0` for the local directory. As an example, to prepare a local directory for storing data through a bind-mount:
  224. +
  225. mkdir esdatadir
  226. chmod g+rwx esdatadir
  227. chgrp 1000 esdatadir
  228. +
  229. As a last resort, you can also force the container to mutate the ownership of any bind-mounts used for the <<path-settings,data and log dirs>> through the environment variable `TAKE_FILE_OWNERSHIP`; in this case they will be owned by uid:gid `1000:0` providing read/write access to the elasticsearch process as required.
  230. +
  231. . It is important to ensure increased ulimits for <<setting-system-settings,nofile>> and <<max-number-threads-check,nproc>> are available for the Elasticsearch containers. Verify the https://github.com/moby/moby/tree/ea4d1243953e6b652082305a9c3cda8656edab26/contrib/init[init system] for the Docker daemon is already setting those to acceptable values and, if needed, adjust them in the Daemon, or override them per container, for example using `docker run`:
  232. +
  233. --ulimit nofile=65536:65536
  234. +
  235. NOTE: One way of checking the Docker daemon defaults for the aforementioned ulimits is by running:
  236. +
  237. docker run --rm centos:7 /bin/bash -c 'ulimit -Hn && ulimit -Sn && ulimit -Hu && ulimit -Su'
  238. +
  239. . Swapping needs to be disabled for performance and node stability. This can be
  240. achieved through any of the methods mentioned in the
  241. <<setup-configuration-memory,Elasticsearch docs>>. If you opt for the
  242. `bootstrap.memory_lock: true` approach, apart from defining it through any of
  243. the <<docker-configuration-methods,configuration methods>>, you will
  244. additionally need the `memlock: true` ulimit, either defined in the
  245. https://docs.docker.com/engine/reference/commandline/dockerd/#default-ulimits[Docker
  246. Daemon] or specifically set for the container. This is demonstrated above in the
  247. <<docker-prod-cluster-composefile,docker-compose.yml>>. If using `docker run`:
  248. +
  249. -e "bootstrap.memory_lock=true" --ulimit memlock=-1:-1
  250. +
  251. . The image https://docs.docker.com/engine/reference/builder/#/expose[exposes] TCP ports 9200 and 9300. For clusters it is recommended to randomize the published ports with `--publish-all`, unless you are pinning one container per host.
  252. +
  253. . Use the `ES_JAVA_OPTS` environment variable to set heap size, e.g. to use 16GB
  254. use `-e ES_JAVA_OPTS="-Xms16g -Xmx16g"` with `docker run`.
  255. +
  256. . Pin your deployments to a specific version of the Elasticsearch Docker image, e.g. +docker.elastic.co/elasticsearch/elasticsearch:{version}+.
  257. +
  258. . Always use a volume bound on `/usr/share/elasticsearch/data`, as shown in the <<docker-cli-run-prod-mode,production example>>, for the following reasons:
  259. +
  260. .. The data of your elasticsearch node won't be lost if the container is killed
  261. .. Elasticsearch is I/O sensitive and the Docker storage driver is not ideal for fast I/O
  262. .. It allows the use of advanced https://docs.docker.com/engine/extend/plugins/#volume-plugins[Docker volume plugins]
  263. +
  264. . If you are using the devicemapper storage driver, make sure you are not using
  265. the default `loop-lvm` mode. Configure docker-engine to use
  266. https://docs.docker.com/engine/userguide/storagedriver/device-mapper-driver/#configure-docker-with-devicemapper[direct-lvm]
  267. instead.
  268. +
  269. . Consider centralizing your logs by using a different https://docs.docker.com/engine/admin/logging/overview/[logging driver]. Also note that the default json-file logging driver is not ideally suited for production use.
  270. include::next-steps.asciidoc[]