docker.asciidoc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. The https://docs.docker.com/storage/volumes[Docker named volumes]
  64. `data01`, `data02`, and `data03` store the node data directories so the data persists across restarts.
  65. If they don't already exist, `docker-compose` creates them when you bring up the cluster.
  66. --
  67. . Make sure Docker Engine is allotted at least 4GiB of memory.
  68. In Docker Desktop, you configure resource usage on the Advanced tab in Preference (macOS)
  69. or Settings (Windows).
  70. +
  71. NOTE: Docker Compose is not pre-installed with Docker on Linux.
  72. See docs.docker.com for installation instructions:
  73. https://docs.docker.com/compose/install[Install Compose on Linux]
  74. . Run `docker-compose` to bring up the cluster:
  75. +
  76. [source,sh,subs="attributes"]
  77. --------------------------------------------
  78. docker-compose up
  79. --------------------------------------------
  80. . Submit a `_cat/nodes` request to see that the nodes are up and running:
  81. +
  82. [source,sh]
  83. --------------------------------------------------
  84. curl -X GET "localhost:9200/_cat/nodes?v&pretty"
  85. --------------------------------------------------
  86. // NOTCONSOLE
  87. Log messages go to the console and are handled by the configured Docker logging driver.
  88. By default you can access logs with `docker logs`.
  89. To stop the cluster, run `docker-compose down`.
  90. The data in the Docker volumes is preserved and loaded
  91. when you restart the cluster with `docker-compose up`.
  92. To **delete the data volumes** when you bring down the cluster,
  93. specify the `-v` option: `docker-compose down -v`.
  94. [[next-getting-started-tls-docker]]
  95. ===== Start a multi-node cluster with TLS enabled
  96. See <<configuring-tls-docker>> and
  97. {stack-gs}/get-started-docker.html#get-started-docker-tls[Run the {stack} in Docker with TLS enabled].
  98. [[docker-prod-prerequisites]]
  99. ==== Using the Docker images in production
  100. The following requirements and recommendations apply when running {es} in Docker in production.
  101. ===== Set `vm.max_map_count` to at least `262144`
  102. The `vm.max_map_count` kernel setting must be set to at least `262144` for production use.
  103. How you set `vm.max_map_count` depends on your platform:
  104. * Linux
  105. +
  106. --
  107. The `vm.max_map_count` setting should be set permanently in `/etc/sysctl.conf`:
  108. [source,sh]
  109. --------------------------------------------
  110. grep vm.max_map_count /etc/sysctl.conf
  111. vm.max_map_count=262144
  112. --------------------------------------------
  113. To apply the setting on a live system, run:
  114. [source,sh]
  115. --------------------------------------------
  116. sysctl -w vm.max_map_count=262144
  117. --------------------------------------------
  118. --
  119. * macOS with https://docs.docker.com/docker-for-mac[Docker for Mac]
  120. +
  121. --
  122. The `vm.max_map_count` setting must be set within the xhyve virtual machine:
  123. . From the command line, run:
  124. +
  125. [source,sh]
  126. --------------------------------------------
  127. screen ~/Library/Containers/com.docker.docker/Data/vms/0/tty
  128. --------------------------------------------
  129. . Press enter and use`sysctl` to configure `vm.max_map_count`:
  130. +
  131. [source,sh]
  132. --------------------------------------------
  133. sysctl -w vm.max_map_count=262144
  134. --------------------------------------------
  135. . To exit the `screen` session, type `Ctrl a d`.
  136. --
  137. * Windows and macOS with https://www.docker.com/products/docker-desktop[Docker Desktop]
  138. +
  139. --
  140. The `vm.max_map_count` setting must be set via docker-machine:
  141. [source,sh]
  142. --------------------------------------------
  143. docker-machine ssh
  144. sudo sysctl -w vm.max_map_count=262144
  145. --------------------------------------------
  146. --
  147. ===== Configuration files must be readable by the `elasticsearch` user
  148. By default, {es} runs inside the container as user `elasticsearch` using
  149. uid:gid `1000:0`.
  150. IMPORTANT: One exception is https://docs.openshift.com/container-platform/3.6/creating_images/guidelines.html#openshift-specific-guidelines[Openshift],
  151. which runs containers using an arbitrarily assigned user ID.
  152. Openshift presents persistent volumes with the gid set to `0`, which works without any adjustments.
  153. If you are bind-mounting a local directory or file, it must be readable by the `elasticsearch` user.
  154. In addition, this user must have write access to the <<path-settings,data and log dirs>>.
  155. A good strategy is to grant group access to gid `0` for the local directory.
  156. For example, to prepare a local directory for storing data through a bind-mount:
  157. [source,sh]
  158. --------------------------------------------
  159. mkdir esdatadir
  160. chmod g+rwx esdatadir
  161. chgrp 0 esdatadir
  162. --------------------------------------------
  163. As a last resort, you can force the container to mutate the ownership of
  164. any bind-mounts used for the <<path-settings,data and log dirs>> through the
  165. environment variable `TAKE_FILE_OWNERSHIP`. When you do this, they will be owned by
  166. uid:gid `1000:0`, which provides the required read/write access to the {es} process.
  167. ===== Increase ulimits for nofile and nproc
  168. Increased ulimits for <<setting-system-settings,nofile>> and <<max-number-threads-check,nproc>>
  169. must be available for the {es} containers.
  170. Verify the https://github.com/moby/moby/tree/ea4d1243953e6b652082305a9c3cda8656edab26/contrib/init[init system]
  171. for the Docker daemon sets them to acceptable values.
  172. To check the Docker daemon defaults for ulimits, run:
  173. [source,sh]
  174. --------------------------------------------
  175. docker run --rm centos:7 /bin/bash -c 'ulimit -Hn && ulimit -Sn && ulimit -Hu && ulimit -Su'
  176. --------------------------------------------
  177. If needed, adjust them in the Daemon or override them per container.
  178. For example, when using `docker run`, set:
  179. [source,sh]
  180. --------------------------------------------
  181. --ulimit nofile=65535:65535
  182. --------------------------------------------
  183. ===== Disable swapping
  184. Swapping needs to be disabled for performance and node stability.
  185. For information about ways to do this, see <<setup-configuration-memory>>.
  186. If you opt for the `bootstrap.memory_lock: true` approach,
  187. you also need to define the `memlock: true` ulimit in the
  188. https://docs.docker.com/engine/reference/commandline/dockerd/#default-ulimits[Docker Daemon],
  189. or explicitly set for the container as shown in the <<docker-compose-file, sample compose file>>.
  190. When using `docker run`, you can specify:
  191. -e "bootstrap.memory_lock=true" --ulimit memlock=-1:-1
  192. ===== Randomize published ports
  193. The image https://docs.docker.com/engine/reference/builder/#/expose[exposes]
  194. TCP ports 9200 and 9300. For production clusters, randomizing the
  195. published ports with `--publish-all` is recommended,
  196. unless you are pinning one container per host.
  197. ===== Set the heap size
  198. Use the `ES_JAVA_OPTS` environment variable to set the heap size.
  199. For example, to use 16GB, specify `-e ES_JAVA_OPTS="-Xms16g -Xmx16g"` with `docker run`.
  200. IMPORTANT: You must <<heap-size,configure the heap size>> even if you are
  201. https://docs.docker.com/config/containers/resource_constraints/#limit-a-containers-access-to-memory[limiting
  202. memory access] to the container.
  203. ===== Pin deployments to a specific image version
  204. Pin your deployments to a specific version of the {es} Docker image. For
  205. example +docker.elastic.co/elasticsearch/elasticsearch:{version}+.
  206. ===== Always bind data volumes
  207. You should use a volume bound on `/usr/share/elasticsearch/data` for the following reasons:
  208. . The data of your {es} node won't be lost if the container is killed
  209. . {es} is I/O sensitive and the Docker storage driver is not ideal for fast I/O
  210. . It allows the use of advanced
  211. https://docs.docker.com/engine/extend/plugins/#volume-plugins[Docker volume plugins]
  212. ===== Avoid using `loop-lvm` mode
  213. If you are using the devicemapper storage driver, do not use the default `loop-lvm` mode.
  214. Configure docker-engine to use
  215. https://docs.docker.com/engine/userguide/storagedriver/device-mapper-driver/#configure-docker-with-devicemapper[direct-lvm].
  216. ===== Centralize your logs
  217. Consider centralizing your logs by using a different
  218. https://docs.docker.com/engine/admin/logging/overview/[logging driver]. Also
  219. note that the default json-file logging driver is not ideally suited for
  220. production use.
  221. [[docker-configuration-methods]]
  222. ==== Configuring {es} with Docker
  223. When you run in Docker, the <<config-files-location,{es} configuration files>> are loaded from
  224. `/usr/share/elasticsearch/config/`.
  225. To use custom configuration files, you <<docker-config-bind-mount, bind-mount the files>>
  226. over the configuration files in the image.
  227. You can set individual {es} configuration parameters using Docker environment variables.
  228. The <<docker-compose-file, sample compose file>> and the
  229. <<docker-cli-run-dev-mode, single-node example>> use this method.
  230. To use the contents of a file to set an environment variable, suffix the environment
  231. variable name with `_FILE`. This is useful for passing secrets such as passwords to {es}
  232. without specifying them directly.
  233. For example, to set the {es} bootstrap password from a file, you can bind mount the
  234. file and set the `ELASTIC_PASSWORD_FILE` environment variable to the mount location.
  235. If you mount the password file to `/run/secrets/password.txt`, specify:
  236. [source,sh]
  237. --------------------------------------------
  238. -e ELASTIC_PASSWORD_FILE=/run/secrets/bootstrapPassword.txt
  239. --------------------------------------------
  240. You can also override the default command for the image to pass {es} configuration
  241. parameters as command line options. For example:
  242. [source,sh]
  243. --------------------------------------------
  244. docker run <various parameters> bin/elasticsearch -Ecluster.name=mynewclustername
  245. --------------------------------------------
  246. While bind-mounting your configuration files is usually the preferred method in production,
  247. you can also <<_c_customized_image, create a custom Docker image>>
  248. that contains your configuration.
  249. [[docker-config-bind-mount]]
  250. ===== Mounting {es} configuration files
  251. Create custom config files and bind-mount them over the corresponding files in the Docker image.
  252. For example, to bind-mount `custom_elasticsearch.yml` with `docker run`, specify:
  253. [source,sh]
  254. --------------------------------------------
  255. -v full_path_to/custom_elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
  256. --------------------------------------------
  257. IMPORTANT: The container **runs {es} as user `elasticsearch` using
  258. uid:gid `1000:0`**. Bind mounted host directories and files must be accessible by this user,
  259. and the data and log directories must be writable by this user.
  260. [[_c_customized_image]]
  261. ===== Using custom Docker images
  262. In some environments, it might make more sense to prepare a custom image that contains
  263. your configuration. A `Dockerfile` to achieve this might be as simple as:
  264. [source,sh,subs="attributes"]
  265. --------------------------------------------
  266. FROM docker.elastic.co/elasticsearch/elasticsearch:{version}
  267. COPY --chown=elasticsearch:elasticsearch elasticsearch.yml /usr/share/elasticsearch/config/
  268. --------------------------------------------
  269. You could then build and run the image with:
  270. [source,sh]
  271. --------------------------------------------
  272. docker build --tag=elasticsearch-custom .
  273. docker run -ti -v /usr/share/elasticsearch/data elasticsearch-custom
  274. --------------------------------------------
  275. Some plugins require additional security permissions.
  276. You must explicitly accept them either by:
  277. * Attaching a `tty` when you run the Docker image and allowing the permissions when prompted.
  278. * Inspecting the security permissions and accepting them (if appropriate) by adding the `--batch` flag to the plugin install command.
  279. See {plugins}/_other_command_line_parameters.html[Plugin management]
  280. for more information.
  281. include::next-steps.asciidoc[]