docker.asciidoc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. [[docker]]
  2. === Install {es} with Docker
  3. {es} is also available as Docker images. Starting with version 8.0.0, these
  4. are based upon a tiny core of essential files. Prior versions used
  5. https://hub.docker.com/_/centos/[centos:8] as the base image.
  6. A list of all published Docker images and tags is available at
  7. https://www.docker.elastic.co[www.docker.elastic.co]. The source files
  8. are in
  9. https://github.com/elastic/elasticsearch/blob/{branch}/distribution/docker[Github].
  10. include::license.asciidoc[]
  11. ==== Pulling the image
  12. Obtaining {es} for Docker is as simple as issuing a +docker pull+ command
  13. against the Elastic Docker registry.
  14. ifeval::["{release-state}"=="unreleased"]
  15. WARNING: Version {version} of {es} has not yet been released, so no
  16. Docker image is currently available for this version.
  17. endif::[]
  18. ifeval::["{release-state}"!="unreleased"]
  19. [source,sh,subs="attributes"]
  20. --------------------------------------------
  21. docker pull {docker-repo}:{version}
  22. --------------------------------------------
  23. endif::[]
  24. [[docker-cli-run-dev-mode]]
  25. ==== Starting a single node cluster with Docker
  26. ifeval::["{release-state}"=="unreleased"]
  27. WARNING: Version {version} of the {es} Docker image has not yet been released.
  28. endif::[]
  29. ifeval::["{release-state}"!="unreleased"]
  30. To start a single-node {es} cluster for development or testing, specify
  31. <<single-node-discovery,single-node discovery>> to bypass the <<bootstrap-checks,bootstrap checks>>:
  32. [source,sh,subs="attributes"]
  33. --------------------------------------------
  34. docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" {docker-image}
  35. --------------------------------------------
  36. endif::[]
  37. [[docker-compose-file]]
  38. ==== Starting a multi-node cluster with Docker Compose
  39. To get a three-node {es} cluster up and running in Docker,
  40. you can use Docker Compose:
  41. . Create a `docker-compose.yml` file:
  42. ifeval::["{release-state}"=="unreleased"]
  43. +
  44. --
  45. WARNING: Version {version} of {es} has not yet been released, so a
  46. `docker-compose.yml` is not available for this version.
  47. endif::[]
  48. ifeval::["{release-state}"!="unreleased"]
  49. [source,yaml,subs="attributes"]
  50. --------------------------------------------
  51. include::docker-compose.yml[]
  52. --------------------------------------------
  53. endif::[]
  54. NOTE: This sample `docker-compose.yml` file uses the `ES_JAVA_OPTS`
  55. environment variable to manually set the heap size to 512MB. We do not recommend
  56. using `ES_JAVA_OPTS` in production. See <<docker-set-heap-size>>.
  57. This sample Docker Compose file brings up a three-node {es} cluster.
  58. Node `es01` listens on `localhost:9200` and `es02` and `es03` talk to `es01` over a Docker network.
  59. Please note that this configuration exposes port 9200 on all network interfaces, and given how
  60. Docker manipulates `iptables` on Linux, this means that your {es} cluster is publicly accessible,
  61. potentially ignoring any firewall settings. If you don't want to expose port 9200 and instead use
  62. a reverse proxy, replace `9200:9200` with `127.0.0.1:9200:9200` in the docker-compose.yml file.
  63. {es} will then only be accessible from the host machine itself.
  64. The https://docs.docker.com/storage/volumes[Docker named volumes]
  65. `data01`, `data02`, and `data03` store the node data directories so the data persists across restarts.
  66. If they don't already exist, `docker-compose` creates them when you bring up the cluster.
  67. --
  68. . Make sure Docker Engine is allotted at least 4GiB of memory.
  69. In Docker Desktop, you configure resource usage on the Advanced tab in Preference (macOS)
  70. or Settings (Windows).
  71. +
  72. NOTE: Docker Compose is not pre-installed with Docker on Linux.
  73. See docs.docker.com for installation instructions:
  74. https://docs.docker.com/compose/install[Install Compose on Linux]
  75. . Run `docker-compose` to bring up the cluster:
  76. +
  77. [source,sh,subs="attributes"]
  78. --------------------------------------------
  79. docker-compose up
  80. --------------------------------------------
  81. . Submit a `_cat/nodes` request to see that the nodes are up and running:
  82. +
  83. [source,sh]
  84. --------------------------------------------------
  85. curl -X GET "localhost:9200/_cat/nodes?v=true&pretty"
  86. --------------------------------------------------
  87. // NOTCONSOLE
  88. Log messages go to the console and are handled by the configured Docker logging driver.
  89. By default you can access logs with `docker logs`. If you would prefer the {es}
  90. container to write logs to disk, set the `ES_LOG_STYLE` environment variable to `file`.
  91. This causes {es} to use the same logging configuration as other {es} distribution formats.
  92. To stop the cluster, run `docker-compose down`.
  93. The data in the Docker volumes is preserved and loaded
  94. when you restart the cluster with `docker-compose up`.
  95. To **delete the data volumes** when you bring down the cluster,
  96. specify the `-v` option: `docker-compose down -v`.
  97. [[next-getting-started-tls-docker]]
  98. ===== Start a multi-node cluster with TLS enabled
  99. See <<configuring-tls-docker>> and
  100. {stack-gs}/get-started-docker.html#get-started-docker-tls[Run the {stack} in Docker with TLS enabled].
  101. [[docker-prod-prerequisites]]
  102. ==== Using the Docker images in production
  103. The following requirements and recommendations apply when running {es} in Docker in production.
  104. ===== Set `vm.max_map_count` to at least `262144`
  105. The `vm.max_map_count` kernel setting must be set to at least `262144` for production use.
  106. How you set `vm.max_map_count` depends on your platform:
  107. * Linux
  108. +
  109. --
  110. The `vm.max_map_count` setting should be set permanently in `/etc/sysctl.conf`:
  111. [source,sh]
  112. --------------------------------------------
  113. grep vm.max_map_count /etc/sysctl.conf
  114. vm.max_map_count=262144
  115. --------------------------------------------
  116. To apply the setting on a live system, run:
  117. [source,sh]
  118. --------------------------------------------
  119. sysctl -w vm.max_map_count=262144
  120. --------------------------------------------
  121. --
  122. * macOS with https://docs.docker.com/docker-for-mac[Docker for Mac]
  123. +
  124. --
  125. The `vm.max_map_count` setting must be set within the xhyve virtual machine:
  126. . From the command line, run:
  127. +
  128. [source,sh]
  129. --------------------------------------------
  130. screen ~/Library/Containers/com.docker.docker/Data/vms/0/tty
  131. --------------------------------------------
  132. . Press enter and use`sysctl` to configure `vm.max_map_count`:
  133. +
  134. [source,sh]
  135. --------------------------------------------
  136. sysctl -w vm.max_map_count=262144
  137. --------------------------------------------
  138. . To exit the `screen` session, type `Ctrl a d`.
  139. --
  140. * Windows and macOS with https://www.docker.com/products/docker-desktop[Docker Desktop]
  141. +
  142. --
  143. The `vm.max_map_count` setting must be set via docker-machine:
  144. [source,sh]
  145. --------------------------------------------
  146. docker-machine ssh
  147. sudo sysctl -w vm.max_map_count=262144
  148. --------------------------------------------
  149. --
  150. * Windows with https://docs.docker.com/docker-for-windows/wsl[Docker Desktop WSL 2 backend]
  151. +
  152. --
  153. The `vm.max_map_count` setting must be set in the docker-desktop container:
  154. [source,sh]
  155. --------------------------------------------
  156. wsl -d docker-desktop
  157. sysctl -w vm.max_map_count=262144
  158. --------------------------------------------
  159. --
  160. ===== Configuration files must be readable by the `elasticsearch` user
  161. By default, {es} runs inside the container as user `elasticsearch` using
  162. uid:gid `1000:0`.
  163. IMPORTANT: One exception is https://docs.openshift.com/container-platform/3.6/creating_images/guidelines.html#openshift-specific-guidelines[Openshift],
  164. which runs containers using an arbitrarily assigned user ID.
  165. Openshift presents persistent volumes with the gid set to `0`, which works without any adjustments.
  166. If you are bind-mounting a local directory or file, it must be readable by the `elasticsearch` user.
  167. In addition, this user must have write access to the <<path-settings,config, data and log dirs>>
  168. ({es} needs write access to the `config` directory so that it can generate a keystore).
  169. A good strategy is to grant group access to gid `0` for the local directory.
  170. For example, to prepare a local directory for storing data through a bind-mount:
  171. [source,sh]
  172. --------------------------------------------
  173. mkdir esdatadir
  174. chmod g+rwx esdatadir
  175. chgrp 0 esdatadir
  176. --------------------------------------------
  177. You can also run an {es} container using both a custom UID and GID. Unless you
  178. bind-mount each of the `config`, `data` and `logs` directories, you must pass
  179. the command line option `--group-add 0` to `docker run`. This ensures that the user
  180. under which {es} is running is also a member of the `root` (GID 0) group inside the
  181. container.
  182. ===== Increase ulimits for nofile and nproc
  183. Increased ulimits for <<setting-system-settings,nofile>> and <<max-number-threads-check,nproc>>
  184. must be available for the {es} containers.
  185. Verify the https://github.com/moby/moby/tree/ea4d1243953e6b652082305a9c3cda8656edab26/contrib/init[init system]
  186. for the Docker daemon sets them to acceptable values.
  187. To check the Docker daemon defaults for ulimits, run:
  188. [source,sh]
  189. --------------------------------------------
  190. docker run --rm centos:8 /bin/bash -c 'ulimit -Hn && ulimit -Sn && ulimit -Hu && ulimit -Su'
  191. --------------------------------------------
  192. If needed, adjust them in the Daemon or override them per container.
  193. For example, when using `docker run`, set:
  194. [source,sh]
  195. --------------------------------------------
  196. --ulimit nofile=65535:65535
  197. --------------------------------------------
  198. ===== Disable swapping
  199. Swapping needs to be disabled for performance and node stability.
  200. For information about ways to do this, see <<setup-configuration-memory>>.
  201. If you opt for the `bootstrap.memory_lock: true` approach,
  202. you also need to define the `memlock: true` ulimit in the
  203. https://docs.docker.com/engine/reference/commandline/dockerd/#default-ulimits[Docker Daemon],
  204. or explicitly set for the container as shown in the <<docker-compose-file, sample compose file>>.
  205. When using `docker run`, you can specify:
  206. -e "bootstrap.memory_lock=true" --ulimit memlock=-1:-1
  207. ===== Randomize published ports
  208. The image https://docs.docker.com/engine/reference/builder/#/expose[exposes]
  209. TCP ports 9200 and 9300. For production clusters, randomizing the
  210. published ports with `--publish-all` is recommended,
  211. unless you are pinning one container per host.
  212. [[docker-set-heap-size]]
  213. ===== Manually set the heap size
  214. By default, {es} automatically sizes JVM heap based on a nodes's
  215. <<node-roles,roles>> and the total memory available to the node's container. We
  216. recommend this default sizing for most production environments. If needed, you
  217. can override default sizing by manually setting JVM heap size.
  218. To manually set the heap size in production, bind mount a <<set-jvm-options,JVM
  219. options>> file under `/usr/share/elasticsearch/config/jvm.options.d` that
  220. includes your desired <<set-jvm-heap-size,heap size>> settings.
  221. For testing, you can also manually set the heap size using the `ES_JAVA_OPTS`
  222. environment variable. For example, to use 16GB, specify `-e
  223. ES_JAVA_OPTS="-Xms16g -Xmx16g"` with `docker run`. The `ES_JAVA_OPTS` variable
  224. overrides all other JVM options. The `ES_JAVA_OPTS` variable overrides all other
  225. JVM options. We do not recommend using `ES_JAVA_OPTS` in production. The
  226. `docker-compose.yml` file above sets the heap size to 512MB.
  227. ===== Pin deployments to a specific image version
  228. Pin your deployments to a specific version of the {es} Docker image. For
  229. example +docker.elastic.co/elasticsearch/elasticsearch:{version}+.
  230. ===== Always bind data volumes
  231. You should use a volume bound on `/usr/share/elasticsearch/data` for the following reasons:
  232. . The data of your {es} node won't be lost if the container is killed
  233. . {es} is I/O sensitive and the Docker storage driver is not ideal for fast I/O
  234. . It allows the use of advanced
  235. https://docs.docker.com/engine/extend/plugins/#volume-plugins[Docker volume plugins]
  236. ===== Avoid using `loop-lvm` mode
  237. If you are using the devicemapper storage driver, do not use the default `loop-lvm` mode.
  238. Configure docker-engine to use
  239. https://docs.docker.com/engine/userguide/storagedriver/device-mapper-driver/#configure-docker-with-devicemapper[direct-lvm].
  240. ===== Centralize your logs
  241. Consider centralizing your logs by using a different
  242. https://docs.docker.com/engine/admin/logging/overview/[logging driver]. Also
  243. note that the default json-file logging driver is not ideally suited for
  244. production use.
  245. [[docker-configuration-methods]]
  246. ==== Configuring {es} with Docker
  247. When you run in Docker, the <<config-files-location,{es} configuration files>> are loaded from
  248. `/usr/share/elasticsearch/config/`.
  249. To use custom configuration files, you <<docker-config-bind-mount, bind-mount the files>>
  250. over the configuration files in the image.
  251. You can set individual {es} configuration parameters using Docker environment variables.
  252. The <<docker-compose-file, sample compose file>> and the
  253. <<docker-cli-run-dev-mode, single-node example>> use this method. You can
  254. use the setting name directly as the environment variable name. If
  255. you cannot do this, for example because your orchestration platform forbids
  256. periods in environment variable names, then you can use an alternative
  257. style by converting the setting name as follows.
  258. . Change the setting name to uppercase
  259. . Prefix it with `ES_SETTING_`
  260. . Escape any underscores (`_`) by duplicating them
  261. . Convert all periods (`.`) to underscores (`_`)
  262. For example, `-e bootstrap.memory_lock=true` becomes
  263. `-e ES_SETTING_BOOTSTRAP_MEMORY__LOCK=true`.
  264. You can use the contents of a file to set the value of the
  265. `ELASTIC_PASSWORD` or `KEYSTORE_PASSWORD` environment variables, by
  266. suffixing the environment variable name with `_FILE`. This is useful for
  267. passing secrets such as passwords to {es} without specifying them directly.
  268. For example, to set the {es} bootstrap password from a file, you can bind mount the
  269. file and set the `ELASTIC_PASSWORD_FILE` environment variable to the mount location.
  270. If you mount the password file to `/run/secrets/bootstrapPassword.txt`, specify:
  271. [source,sh]
  272. --------------------------------------------
  273. -e ELASTIC_PASSWORD_FILE=/run/secrets/bootstrapPassword.txt
  274. --------------------------------------------
  275. You can override the default command for the image to pass {es} configuration
  276. parameters as command line options. For example:
  277. [source,sh]
  278. --------------------------------------------
  279. docker run <various parameters> bin/elasticsearch -Ecluster.name=mynewclustername
  280. --------------------------------------------
  281. While bind-mounting your configuration files is usually the preferred method in production,
  282. you can also <<_c_customized_image, create a custom Docker image>>
  283. that contains your configuration.
  284. [[docker-config-bind-mount]]
  285. ===== Mounting {es} configuration files
  286. Create custom config files and bind-mount them over the corresponding files in the Docker image.
  287. For example, to bind-mount `custom_elasticsearch.yml` with `docker run`, specify:
  288. [source,sh]
  289. --------------------------------------------
  290. -v full_path_to/custom_elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
  291. --------------------------------------------
  292. IMPORTANT: The container **runs {es} as user `elasticsearch` using
  293. uid:gid `1000:0`**. Bind mounted host directories and files must be accessible by this user,
  294. and the data and log directories must be writable by this user.
  295. [[docker-keystore-bind-mount]]
  296. ===== Create an encrypted {es} keystore
  297. By default, {es} will auto-generate a keystore file for <<secure-settings,secure
  298. settings>>. This file is obfuscated but not encrypted.
  299. To encrypt your secure settings with a password and have them persist outside
  300. the container, use a `docker run` command to manually create the keystore
  301. instead. The command must:
  302. * Bind-mount the `config` directory. The command will create an
  303. `elasticsearch.keystore` file in this directory. To avoid errors, do
  304. not directly bind-mount the `elasticsearch.keystore` file.
  305. * Use the `elasticsearch-keystore` tool with the `create -p` option. You'll be
  306. prompted to enter a password for the keystore.
  307. ifeval::["{release-state}"!="unreleased"]
  308. For example:
  309. [source,sh,subs="attributes"]
  310. ----
  311. docker run -it --rm \
  312. -v full_path_to/config:/usr/share/elasticsearch/config \
  313. docker.elastic.co/elasticsearch/elasticsearch:{version} \
  314. bin/elasticsearch-keystore create -p
  315. ----
  316. You can also use a `docker run` command to add or update secure settings in the
  317. keystore. You'll be prompted to enter the setting values. If the keystore is
  318. encrypted, you'll also be prompted to enter the keystore password.
  319. [source,sh,subs="attributes"]
  320. ----
  321. docker run -it --rm \
  322. -v full_path_to/config:/usr/share/elasticsearch/config \
  323. docker.elastic.co/elasticsearch/elasticsearch:{version} \
  324. bin/elasticsearch-keystore \
  325. add my.secure.setting \
  326. my.other.secure.setting
  327. ----
  328. endif::[]
  329. If you've already created the keystore and don't need to update it, you can
  330. bind-mount the `elasticsearch.keystore` file directly. For example, you can
  331. add the following to `docker-compose.yml`:
  332. [source,yaml]
  333. ----
  334. ...
  335. volumes:
  336. ...
  337. - type: bind
  338. source: full_path_to/config/elasticsearch.keystore
  339. target: /usr/share/elasticsearch/config/elasticsearch.keystore
  340. ----
  341. [[_c_customized_image]]
  342. ===== Using custom Docker images
  343. In some environments, it might make more sense to prepare a custom image that contains
  344. your configuration. A `Dockerfile` to achieve this might be as simple as:
  345. [source,sh,subs="attributes"]
  346. --------------------------------------------
  347. FROM docker.elastic.co/elasticsearch/elasticsearch:{version}
  348. COPY --chown=elasticsearch:elasticsearch elasticsearch.yml /usr/share/elasticsearch/config/
  349. --------------------------------------------
  350. You could then build and run the image with:
  351. [source,sh]
  352. --------------------------------------------
  353. docker build --tag=elasticsearch-custom .
  354. docker run -ti -v /usr/share/elasticsearch/data elasticsearch-custom
  355. --------------------------------------------
  356. Some plugins require additional security permissions.
  357. You must explicitly accept them either by:
  358. * Attaching a `tty` when you run the Docker image and allowing the permissions when prompted.
  359. * Inspecting the security permissions and accepting them (if appropriate) by adding the `--batch` flag to the plugin install command.
  360. See {plugins}/_other_command_line_parameters.html[Plugin management]
  361. for more information.
  362. The {es} Docker image only includes what is required to run {es}, and does
  363. not provide a package manager. It is possible to add additional utilities
  364. with a multi-phase Docker build. You must also copy any dependencies, for
  365. example shared libraries.
  366. [source,sh,subs="attributes"]
  367. --------------------------------------------
  368. FROM centos:8 AS builder
  369. yum install -y some-package
  370. FROM docker.elastic.co/elasticsearch/elasticsearch:{version}
  371. COPY --from=builder /usr/bin/some-utility /usr/bin/
  372. COPY --from=builder /usr/lib/some-lib.so /usr/lib/
  373. --------------------------------------------
  374. You should use `centos:8` as a base in order to avoid incompatibilities.
  375. Use http://man7.org/linux/man-pages/man1/ldd.1.html[`ldd`] to list the
  376. shared libraries required by a utility.
  377. [[troubleshoot-docker-errors]]
  378. ==== Troubleshoot Docker errors for {es}
  379. Here’s how to resolve common errors when running {es} with Docker.
  380. ===== elasticsearch.keystore is a directory
  381. [source,txt]
  382. ----
  383. Exception in thread "main" org.elasticsearch.bootstrap.BootstrapException: java.io.IOException: Is a directory: SimpleFSIndexInput(path="/usr/share/elasticsearch/config/elasticsearch.keystore") Likely root cause: java.io.IOException: Is a directory
  384. ----
  385. A <<docker-keystore-bind-mount,keystore-related>> `docker run` command attempted
  386. to directly bind-mount an `elasticsearch.keystore` file that doesn't exist. If
  387. you use the `-v` or `--volume` flag to mount a file that doesn't exist, Docker
  388. instead creates a directory with the same name.
  389. To resolve this error:
  390. . Delete the `elasticsearch.keystore` directory in the `config` directory.
  391. . Update the `-v` or `--volume` flag to point to the `config` directory path
  392. rather than the keystore file's path. For an example, see
  393. <<docker-keystore-bind-mount>>.
  394. . Retry the command.
  395. ===== elasticsearch.keystore: Device or resource busy
  396. [source,txt]
  397. ----
  398. Exception in thread "main" java.nio.file.FileSystemException: /usr/share/elasticsearch/config/elasticsearch.keystore.tmp -> /usr/share/elasticsearch/config/elasticsearch.keystore: Device or resource busy
  399. ----
  400. A <<docker-keystore-bind-mount,keystore-related>> `docker run` command attempted
  401. to directly bind-mount the `elasticsearch.keystore` file. To update the
  402. keystore, the container requires access to other files in the `config`
  403. directory, such as `keystore.tmp`.
  404. To resolve this error:
  405. . Update the `-v` or `--volume` flag to point to the `config` directory
  406. path rather than the keystore file's path. For an example, see
  407. <<docker-keystore-bind-mount>>.
  408. . Retry the command.
  409. include::next-steps.asciidoc[]