docker.asciidoc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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:7] 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. {stack-ov}/license-management.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. ===== Configuration files must be readable by the `elasticsearch` user
  153. By default, {es} runs inside the container as user `elasticsearch` using
  154. uid:gid `1000:0`.
  155. IMPORTANT: One exception is https://docs.openshift.com/container-platform/3.6/creating_images/guidelines.html#openshift-specific-guidelines[Openshift],
  156. which runs containers using an arbitrarily assigned user ID.
  157. Openshift presents persistent volumes with the gid set to `0`, which works without any adjustments.
  158. If you are bind-mounting a local directory or file, it must be readable by the `elasticsearch` user.
  159. In addition, this user must have write access to the <<path-settings,data and log dirs>>.
  160. A good strategy is to grant group access to gid `0` for the local directory.
  161. For example, to prepare a local directory for storing data through a bind-mount:
  162. [source,sh]
  163. --------------------------------------------
  164. mkdir esdatadir
  165. chmod g+rwx esdatadir
  166. chgrp 0 esdatadir
  167. --------------------------------------------
  168. ===== Increase ulimits for nofile and nproc
  169. Increased ulimits for <<setting-system-settings,nofile>> and <<max-number-threads-check,nproc>>
  170. must be available for the {es} containers.
  171. Verify the https://github.com/moby/moby/tree/ea4d1243953e6b652082305a9c3cda8656edab26/contrib/init[init system]
  172. for the Docker daemon sets them to acceptable values.
  173. To check the Docker daemon defaults for ulimits, run:
  174. [source,sh]
  175. --------------------------------------------
  176. docker run --rm centos:7 /bin/bash -c 'ulimit -Hn && ulimit -Sn && ulimit -Hu && ulimit -Su'
  177. --------------------------------------------
  178. If needed, adjust them in the Daemon or override them per container.
  179. For example, when using `docker run`, set:
  180. [source,sh]
  181. --------------------------------------------
  182. --ulimit nofile=65535:65535
  183. --------------------------------------------
  184. ===== Disable swapping
  185. Swapping needs to be disabled for performance and node stability.
  186. For information about ways to do this, see <<setup-configuration-memory>>.
  187. If you opt for the `bootstrap.memory_lock: true` approach,
  188. you also need to define the `memlock: true` ulimit in the
  189. https://docs.docker.com/engine/reference/commandline/dockerd/#default-ulimits[Docker Daemon],
  190. or explicitly set for the container as shown in the <<docker-compose-file, sample compose file>>.
  191. When using `docker run`, you can specify:
  192. -e "bootstrap.memory_lock=true" --ulimit memlock=-1:-1
  193. ===== Randomize published ports
  194. The image https://docs.docker.com/engine/reference/builder/#/expose[exposes]
  195. TCP ports 9200 and 9300. For production clusters, randomizing the
  196. published ports with `--publish-all` is recommended,
  197. unless you are pinning one container per host.
  198. [[docker-set-heap-size]]
  199. ===== Set the heap size
  200. To configure the heap size, you can bind mount a <<jvm-options,JVM options>>
  201. file under `/usr/share/elasticsearch/config/jvm.options.d` that includes your
  202. desired <<heap-size,heap size>> settings. Note that while the default root
  203. `jvm.options` file sets a default heap of 1 GB, any value you set in a
  204. bind-mounted JVM options file will override it.
  205. While setting the heap size via bind-mounted JVM options is the recommended
  206. method, you can also configure this by using the `ES_JAVA_OPTS` environment
  207. variable to set the heap size. For example, to use 16 GB, specify
  208. `-e ES_JAVA_OPTS="-Xms16g -Xmx16g"` with `docker run`. Note that while the
  209. default root `jvm.options` file sets a default heap of 1 GB, any value you set
  210. in `ES_JAVA_OPTS` will override it. The `docker-compose.yml` file above sets the heap size to 512 MB.
  211. IMPORTANT: You must <<heap-size,configure the heap size>> even if you are
  212. https://docs.docker.com/config/containers/resource_constraints/#limit-a-containers-access-to-memory[limiting
  213. memory access] to the container.
  214. ===== Pin deployments to a specific image version
  215. Pin your deployments to a specific version of the {es} Docker image. For
  216. example +docker.elastic.co/elasticsearch/elasticsearch:{version}+.
  217. ===== Always bind data volumes
  218. You should use a volume bound on `/usr/share/elasticsearch/data` for the following reasons:
  219. . The data of your {es} node won't be lost if the container is killed
  220. . {es} is I/O sensitive and the Docker storage driver is not ideal for fast I/O
  221. . It allows the use of advanced
  222. https://docs.docker.com/engine/extend/plugins/#volume-plugins[Docker volume plugins]
  223. ===== Avoid using `loop-lvm` mode
  224. If you are using the devicemapper storage driver, do not use the default `loop-lvm` mode.
  225. Configure docker-engine to use
  226. https://docs.docker.com/engine/userguide/storagedriver/device-mapper-driver/#configure-docker-with-devicemapper[direct-lvm].
  227. ===== Centralize your logs
  228. Consider centralizing your logs by using a different
  229. https://docs.docker.com/engine/admin/logging/overview/[logging driver]. Also
  230. note that the default json-file logging driver is not ideally suited for
  231. production use.
  232. [[docker-configuration-methods]]
  233. ==== Configuring {es} with Docker
  234. When you run in Docker, the <<config-files-location,{es} configuration files>> are loaded from
  235. `/usr/share/elasticsearch/config/`.
  236. To use custom configuration files, you <<docker-config-bind-mount, bind-mount the files>>
  237. over the configuration files in the image.
  238. You can set individual {es} configuration parameters using Docker environment variables.
  239. The <<docker-compose-file, sample compose file>> and the
  240. <<docker-cli-run-dev-mode, single-node example>> use this method.
  241. To use the contents of a file to set an environment variable, suffix the environment
  242. variable name with `_FILE`. This is useful for passing secrets such as passwords to {es}
  243. without specifying them directly.
  244. For example, to set the {es} bootstrap password from a file, you can bind mount the
  245. file and set the `ELASTIC_PASSWORD_FILE` environment variable to the mount location.
  246. If you mount the password file to `/run/secrets/password.txt`, specify:
  247. [source,sh]
  248. --------------------------------------------
  249. -e ELASTIC_PASSWORD_FILE=/run/secrets/bootstrapPassword.txt
  250. --------------------------------------------
  251. You can also override the default command for the image to pass {es} configuration
  252. parameters as command line options. For example:
  253. [source,sh]
  254. --------------------------------------------
  255. docker run <various parameters> bin/elasticsearch -Ecluster.name=mynewclustername
  256. --------------------------------------------
  257. While bind-mounting your configuration files is usually the preferred method in production,
  258. you can also <<_c_customized_image, create a custom Docker image>>
  259. that contains your configuration.
  260. [[docker-config-bind-mount]]
  261. ===== Mounting {es} configuration files
  262. Create custom config files and bind-mount them over the corresponding files in the Docker image.
  263. For example, to bind-mount `custom_elasticsearch.yml` with `docker run`, specify:
  264. [source,sh]
  265. --------------------------------------------
  266. -v full_path_to/custom_elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
  267. --------------------------------------------
  268. IMPORTANT: The container **runs {es} as user `elasticsearch` using
  269. uid:gid `1000:0`**. Bind mounted host directories and files must be accessible by this user,
  270. and the data and log directories must be writable by this user.
  271. [[docker-keystore-bind-mount]]
  272. ===== Mounting an {es} keystore
  273. By default, {es} will auto-generate a keystore file for secure settings. This
  274. file is obfuscated but not encrypted. If you want to encrypt your
  275. <<secure-settings,secure settings>> with a password, you must use the
  276. `elasticsearch-keystore` utility to create a password-protected keystore and
  277. bind-mount it to the container as
  278. `/usr/share/elasticsearch/config/elasticsearch.keystore`. In order to provide
  279. the Docker container with the password at startup, set the Docker environment
  280. value `KEYSTORE_PASSWORD` to the value of your password. For example, a `docker
  281. run` command might have the following options:
  282. [source, sh]
  283. --------------------------------------------
  284. -v full_path_to/elasticsearch.keystore:/usr/share/elasticsearch/config/elasticsearch.keystore
  285. -E KEYSTORE_PASSWORD=mypassword
  286. --------------------------------------------
  287. [[_c_customized_image]]
  288. ===== Using custom Docker images
  289. In some environments, it might make more sense to prepare a custom image that contains
  290. your configuration. A `Dockerfile` to achieve this might be as simple as:
  291. [source,sh,subs="attributes"]
  292. --------------------------------------------
  293. FROM docker.elastic.co/elasticsearch/elasticsearch:{version}
  294. COPY --chown=elasticsearch:elasticsearch elasticsearch.yml /usr/share/elasticsearch/config/
  295. --------------------------------------------
  296. You could then build and run the image with:
  297. [source,sh]
  298. --------------------------------------------
  299. docker build --tag=elasticsearch-custom .
  300. docker run -ti -v /usr/share/elasticsearch/data elasticsearch-custom
  301. --------------------------------------------
  302. Some plugins require additional security permissions.
  303. You must explicitly accept them either by:
  304. * Attaching a `tty` when you run the Docker image and allowing the permissions when prompted.
  305. * Inspecting the security permissions and accepting them (if appropriate) by adding the `--batch` flag to the plugin install command.
  306. See {plugins}/_other_command_line_parameters.html[Plugin management]
  307. for more information.
  308. include::next-steps.asciidoc[]