docker.asciidoc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. [[docker]]
  2. === Install {es} with Docker
  3. {es} is also available as Docker images.
  4. The images use https://hub.docker.com/_/centos/[centos:8] as the base image.
  5. A list of all published Docker images and tags is available at
  6. https://www.docker.elastic.co[www.docker.elastic.co]. The source files
  7. are in
  8. https://github.com/elastic/elasticsearch/blob/{branch}/distribution/docker[Github].
  9. These images are free to use under the Elastic license. They contain open source
  10. and free commercial features and access to paid commercial features.
  11. {kibana-ref}/managing-licenses.html[Start a 30-day trial] to try out all of the
  12. paid commercial features. See the
  13. https://www.elastic.co/subscriptions[Subscriptions] page for information about
  14. Elastic license levels.
  15. ==== Pulling the image
  16. Obtaining {es} for Docker is as simple as issuing a +docker pull+ command
  17. against the Elastic Docker registry.
  18. ifeval::["{release-state}"=="unreleased"]
  19. WARNING: Version {version} of {es} has not yet been released, so no
  20. Docker image is currently available for this version.
  21. endif::[]
  22. ifeval::["{release-state}"!="unreleased"]
  23. [source,sh,subs="attributes"]
  24. --------------------------------------------
  25. docker pull {docker-repo}:{version}
  26. --------------------------------------------
  27. Alternatively, you can download other Docker images that contain only features
  28. available under the Apache 2.0 license. To download the images, go to
  29. https://www.docker.elastic.co[www.docker.elastic.co].
  30. endif::[]
  31. [[docker-cli-run-dev-mode]]
  32. ==== Starting a single node cluster with Docker
  33. ifeval::["{release-state}"=="unreleased"]
  34. WARNING: Version {version} of the {es} Docker image has not yet been released.
  35. endif::[]
  36. ifeval::["{release-state}"!="unreleased"]
  37. To start a single-node {es} cluster for development or testing, specify
  38. <<single-node-discovery,single-node discovery>> to bypass the <<bootstrap-checks,bootstrap checks>>:
  39. [source,sh,subs="attributes"]
  40. --------------------------------------------
  41. docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" {docker-image}
  42. --------------------------------------------
  43. endif::[]
  44. [[docker-compose-file]]
  45. ==== Starting a multi-node cluster with Docker Compose
  46. To get a three-node {es} cluster up and running in Docker,
  47. you can use Docker Compose:
  48. . Create a `docker-compose.yml` file:
  49. ifeval::["{release-state}"=="unreleased"]
  50. +
  51. --
  52. WARNING: Version {version} of {es} has not yet been released, so a
  53. `docker-compose.yml` is not available for this version.
  54. endif::[]
  55. ifeval::["{release-state}"!="unreleased"]
  56. [source,yaml,subs="attributes"]
  57. --------------------------------------------
  58. include::docker-compose.yml[]
  59. --------------------------------------------
  60. endif::[]
  61. This sample Docker Compose file brings up a three-node {es} cluster.
  62. Node `es01` listens on `localhost:9200` and `es02` and `es03` talk to `es01` over a Docker network.
  63. Please note that this configuration exposes port 9200 on all network interfaces, and given how
  64. Docker manipulates `iptables` on Linux, this means that your {es} cluster is publicly accessible,
  65. potentially ignoring any firewall settings. If you don't want to expose port 9200 and instead use
  66. a reverse proxy, replace `9200:9200` with `127.0.0.1:9200:9200` in the docker-compose.yml file.
  67. {es} will then only be accessible from the host machine itself.
  68. The https://docs.docker.com/storage/volumes[Docker named volumes]
  69. `data01`, `data02`, and `data03` store the node data directories so the data persists across restarts.
  70. If they don't already exist, `docker-compose` creates them when you bring up the cluster.
  71. --
  72. . Make sure Docker Engine is allotted at least 4GiB of memory.
  73. In Docker Desktop, you configure resource usage on the Advanced tab in Preference (macOS)
  74. or Settings (Windows).
  75. +
  76. NOTE: Docker Compose is not pre-installed with Docker on Linux.
  77. See docs.docker.com for installation instructions:
  78. https://docs.docker.com/compose/install[Install Compose on Linux]
  79. . Run `docker-compose` to bring up the cluster:
  80. +
  81. [source,sh,subs="attributes"]
  82. --------------------------------------------
  83. docker-compose up
  84. --------------------------------------------
  85. . Submit a `_cat/nodes` request to see that the nodes are up and running:
  86. +
  87. [source,sh]
  88. --------------------------------------------------
  89. curl -X GET "localhost:9200/_cat/nodes?v&pretty"
  90. --------------------------------------------------
  91. // NOTCONSOLE
  92. Log messages go to the console and are handled by the configured Docker logging driver.
  93. By default you can access logs with `docker logs`.
  94. To stop the cluster, run `docker-compose down`.
  95. The data in the Docker volumes is preserved and loaded
  96. when you restart the cluster with `docker-compose up`.
  97. To **delete the data volumes** when you bring down the cluster,
  98. specify the `-v` option: `docker-compose down -v`.
  99. [[next-getting-started-tls-docker]]
  100. ===== Start a multi-node cluster with TLS enabled
  101. See <<configuring-tls-docker>> and
  102. {stack-gs}/get-started-docker.html#get-started-docker-tls[Run the {stack} in Docker with TLS enabled].
  103. [[docker-prod-prerequisites]]
  104. ==== Using the Docker images in production
  105. The following requirements and recommendations apply when running {es} in Docker in production.
  106. ===== Set `vm.max_map_count` to at least `262144`
  107. The `vm.max_map_count` kernel setting must be set to at least `262144` for production use.
  108. How you set `vm.max_map_count` depends on your platform:
  109. * Linux
  110. +
  111. --
  112. The `vm.max_map_count` setting should be set permanently in `/etc/sysctl.conf`:
  113. [source,sh]
  114. --------------------------------------------
  115. grep vm.max_map_count /etc/sysctl.conf
  116. vm.max_map_count=262144
  117. --------------------------------------------
  118. To apply the setting on a live system, run:
  119. [source,sh]
  120. --------------------------------------------
  121. sysctl -w vm.max_map_count=262144
  122. --------------------------------------------
  123. --
  124. * macOS with https://docs.docker.com/docker-for-mac[Docker for Mac]
  125. +
  126. --
  127. The `vm.max_map_count` setting must be set within the xhyve virtual machine:
  128. . From the command line, run:
  129. +
  130. [source,sh]
  131. --------------------------------------------
  132. screen ~/Library/Containers/com.docker.docker/Data/vms/0/tty
  133. --------------------------------------------
  134. . Press enter and use`sysctl` to configure `vm.max_map_count`:
  135. +
  136. [source,sh]
  137. --------------------------------------------
  138. sysctl -w vm.max_map_count=262144
  139. --------------------------------------------
  140. . To exit the `screen` session, type `Ctrl a d`.
  141. --
  142. * Windows and macOS with https://www.docker.com/products/docker-desktop[Docker Desktop]
  143. +
  144. --
  145. The `vm.max_map_count` setting must be set via docker-machine:
  146. [source,sh]
  147. --------------------------------------------
  148. docker-machine ssh
  149. sudo sysctl -w vm.max_map_count=262144
  150. --------------------------------------------
  151. --
  152. * Windows with https://docs.docker.com/docker-for-windows/wsl[Docker Desktop WSL 2 backend]
  153. +
  154. --
  155. The `vm.max_map_count` setting must be set in the docker-desktop container:
  156. [source,sh]
  157. --------------------------------------------
  158. wsl -d docker-desktop
  159. sysctl -w vm.max_map_count=262144
  160. --------------------------------------------
  161. --
  162. ===== Configuration files must be readable by the `elasticsearch` user
  163. By default, {es} runs inside the container as user `elasticsearch` using
  164. uid:gid `1000:0`.
  165. IMPORTANT: One exception is https://docs.openshift.com/container-platform/3.6/creating_images/guidelines.html#openshift-specific-guidelines[Openshift],
  166. which runs containers using an arbitrarily assigned user ID.
  167. Openshift presents persistent volumes with the gid set to `0`, which works without any adjustments.
  168. If you are bind-mounting a local directory or file, it must be readable by the `elasticsearch` user.
  169. In addition, this user must have write access to the <<path-settings,data and log dirs>>.
  170. A good strategy is to grant group access to gid `0` for the local directory.
  171. For example, to prepare a local directory for storing data through a bind-mount:
  172. [source,sh]
  173. --------------------------------------------
  174. mkdir esdatadir
  175. chmod g+rwx esdatadir
  176. chgrp 0 esdatadir
  177. --------------------------------------------
  178. ===== Increase ulimits for nofile and nproc
  179. Increased ulimits for <<setting-system-settings,nofile>> and <<max-number-threads-check,nproc>>
  180. must be available for the {es} containers.
  181. Verify the https://github.com/moby/moby/tree/ea4d1243953e6b652082305a9c3cda8656edab26/contrib/init[init system]
  182. for the Docker daemon sets them to acceptable values.
  183. To check the Docker daemon defaults for ulimits, run:
  184. [source,sh]
  185. --------------------------------------------
  186. docker run --rm centos:8 /bin/bash -c 'ulimit -Hn && ulimit -Sn && ulimit -Hu && ulimit -Su'
  187. --------------------------------------------
  188. If needed, adjust them in the Daemon or override them per container.
  189. For example, when using `docker run`, set:
  190. [source,sh]
  191. --------------------------------------------
  192. --ulimit nofile=65535:65535
  193. --------------------------------------------
  194. ===== Disable swapping
  195. Swapping needs to be disabled for performance and node stability.
  196. For information about ways to do this, see <<setup-configuration-memory>>.
  197. If you opt for the `bootstrap.memory_lock: true` approach,
  198. you also need to define the `memlock: true` ulimit in the
  199. https://docs.docker.com/engine/reference/commandline/dockerd/#default-ulimits[Docker Daemon],
  200. or explicitly set for the container as shown in the <<docker-compose-file, sample compose file>>.
  201. When using `docker run`, you can specify:
  202. -e "bootstrap.memory_lock=true" --ulimit memlock=-1:-1
  203. ===== Randomize published ports
  204. The image https://docs.docker.com/engine/reference/builder/#/expose[exposes]
  205. TCP ports 9200 and 9300. For production clusters, randomizing the
  206. published ports with `--publish-all` is recommended,
  207. unless you are pinning one container per host.
  208. [[docker-set-heap-size]]
  209. ===== Set the heap size
  210. To configure the heap size, you can bind mount a <<jvm-options,JVM options>>
  211. file under `/usr/share/elasticsearch/config/jvm.options.d` that includes your
  212. desired <<heap-size,heap size>> settings. Note that while the default root
  213. `jvm.options` file sets a default heap of 1 GB, any value you set in a
  214. bind-mounted JVM options file will override it.
  215. While setting the heap size via bind-mounted JVM options is the recommended
  216. method, you can also configure this by using the `ES_JAVA_OPTS` environment
  217. variable to set the heap size. For example, to use 16 GB, specify
  218. `-e ES_JAVA_OPTS="-Xms16g -Xmx16g"` with `docker run`. Note that while the
  219. default root `jvm.options` file sets a default heap of 1 GB, any value you set
  220. in `ES_JAVA_OPTS` will override it. The `docker-compose.yml` file above sets the heap size to 512 MB.
  221. IMPORTANT: You must <<heap-size,configure the heap size>> even if you are
  222. https://docs.docker.com/config/containers/resource_constraints/#limit-a-containers-access-to-memory[limiting
  223. memory access] to the container.
  224. ===== Pin deployments to a specific image version
  225. Pin your deployments to a specific version of the {es} Docker image. For
  226. example +docker.elastic.co/elasticsearch/elasticsearch:{version}+.
  227. ===== Always bind data volumes
  228. You should use a volume bound on `/usr/share/elasticsearch/data` for the following reasons:
  229. . The data of your {es} node won't be lost if the container is killed
  230. . {es} is I/O sensitive and the Docker storage driver is not ideal for fast I/O
  231. . It allows the use of advanced
  232. https://docs.docker.com/engine/extend/plugins/#volume-plugins[Docker volume plugins]
  233. ===== Avoid using `loop-lvm` mode
  234. If you are using the devicemapper storage driver, do not use the default `loop-lvm` mode.
  235. Configure docker-engine to use
  236. https://docs.docker.com/engine/userguide/storagedriver/device-mapper-driver/#configure-docker-with-devicemapper[direct-lvm].
  237. ===== Centralize your logs
  238. Consider centralizing your logs by using a different
  239. https://docs.docker.com/engine/admin/logging/overview/[logging driver]. Also
  240. note that the default json-file logging driver is not ideally suited for
  241. production use.
  242. [[docker-configuration-methods]]
  243. ==== Configuring {es} with Docker
  244. When you run in Docker, the <<config-files-location,{es} configuration files>> are loaded from
  245. `/usr/share/elasticsearch/config/`.
  246. To use custom configuration files, you <<docker-config-bind-mount, bind-mount the files>>
  247. over the configuration files in the image.
  248. You can set individual {es} configuration parameters using Docker environment variables.
  249. The <<docker-compose-file, sample compose file>> and the
  250. <<docker-cli-run-dev-mode, single-node example>> use this method.
  251. To use the contents of a file to set an environment variable, suffix the environment
  252. variable name with `_FILE`. This is useful for passing secrets such as passwords to {es}
  253. without specifying them directly.
  254. For example, to set the {es} bootstrap password from a file, you can bind mount the
  255. file and set the `ELASTIC_PASSWORD_FILE` environment variable to the mount location.
  256. If you mount the password file to `/run/secrets/password.txt`, specify:
  257. [source,sh]
  258. --------------------------------------------
  259. -e ELASTIC_PASSWORD_FILE=/run/secrets/bootstrapPassword.txt
  260. --------------------------------------------
  261. You can also override the default command for the image to pass {es} configuration
  262. parameters as command line options. For example:
  263. [source,sh]
  264. --------------------------------------------
  265. docker run <various parameters> bin/elasticsearch -Ecluster.name=mynewclustername
  266. --------------------------------------------
  267. While bind-mounting your configuration files is usually the preferred method in production,
  268. you can also <<_c_customized_image, create a custom Docker image>>
  269. that contains your configuration.
  270. [[docker-config-bind-mount]]
  271. ===== Mounting {es} configuration files
  272. Create custom config files and bind-mount them over the corresponding files in the Docker image.
  273. For example, to bind-mount `custom_elasticsearch.yml` with `docker run`, specify:
  274. [source,sh]
  275. --------------------------------------------
  276. -v full_path_to/custom_elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
  277. --------------------------------------------
  278. IMPORTANT: The container **runs {es} as user `elasticsearch` using
  279. uid:gid `1000:0`**. Bind mounted host directories and files must be accessible by this user,
  280. and the data and log directories must be writable by this user.
  281. [[docker-keystore-bind-mount]]
  282. ===== Mounting an {es} keystore
  283. By default, {es} will auto-generate a keystore file for secure settings. This
  284. file is obfuscated but not encrypted. If you want to encrypt your
  285. <<secure-settings,secure settings>> with a password, you must use the
  286. `elasticsearch-keystore` utility to create a password-protected keystore and
  287. bind-mount it to the container as
  288. `/usr/share/elasticsearch/config/elasticsearch.keystore`. In order to provide
  289. the Docker container with the password at startup, set the Docker environment
  290. value `KEYSTORE_PASSWORD` to the value of your password. For example, a `docker
  291. run` command might have the following options:
  292. [source, sh]
  293. --------------------------------------------
  294. -v full_path_to/elasticsearch.keystore:/usr/share/elasticsearch/config/elasticsearch.keystore
  295. -E KEYSTORE_PASSWORD=mypassword
  296. --------------------------------------------
  297. [[_c_customized_image]]
  298. ===== Using custom Docker images
  299. In some environments, it might make more sense to prepare a custom image that contains
  300. your configuration. A `Dockerfile` to achieve this might be as simple as:
  301. [source,sh,subs="attributes"]
  302. --------------------------------------------
  303. FROM docker.elastic.co/elasticsearch/elasticsearch:{version}
  304. COPY --chown=elasticsearch:elasticsearch elasticsearch.yml /usr/share/elasticsearch/config/
  305. --------------------------------------------
  306. You could then build and run the image with:
  307. [source,sh]
  308. --------------------------------------------
  309. docker build --tag=elasticsearch-custom .
  310. docker run -ti -v /usr/share/elasticsearch/data elasticsearch-custom
  311. --------------------------------------------
  312. Some plugins require additional security permissions.
  313. You must explicitly accept them either by:
  314. * Attaching a `tty` when you run the Docker image and allowing the permissions when prompted.
  315. * Inspecting the security permissions and accepting them (if appropriate) by adding the `--batch` flag to the plugin install command.
  316. See {plugins}/_other_command_line_parameters.html[Plugin management]
  317. for more information.
  318. include::next-steps.asciidoc[]