docker.asciidoc 18 KB

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