docker.asciidoc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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. Starting in {es} 8.0, security is enabled by default. With security enabled,
  12. {stack} {security-features} require TLS encryption for the transport networking
  13. layer, or your cluster will fail to start.
  14. ==== Install Docker Desktop or Docker Engine
  15. Install the appropriate https://docs.docker.com/get-docker/[Docker application]
  16. for your operating system.
  17. NOTE: Make sure that Docker is allotted at least 4GiB of memory. In Docker
  18. Desktop, you configure resource usage on the Advanced tab in Preference (macOS)
  19. or Settings (Windows).
  20. ==== Pull the {es} Docker image
  21. Obtaining {es} for Docker is as simple as issuing a `docker pull` command
  22. against the Elastic Docker registry.
  23. ifeval::["{release-state}"=="unreleased"]
  24. WARNING: Version {version} of {es} has not yet been released, so no
  25. Docker image is currently available for this version.
  26. endif::[]
  27. ifeval::["{release-state}"!="unreleased"]
  28. [source,sh,subs="attributes"]
  29. ----
  30. docker pull {docker-repo}:{version}
  31. ----
  32. endif::[]
  33. Now that you have the {es} Docker image, you can start a
  34. <<docker-cli-run-dev-mode,single-node>> or <<docker-compose-file,multi-node>>
  35. cluster.
  36. [[docker-cli-run-dev-mode]]
  37. ==== Start a single-node cluster with Docker
  38. ifeval::["{release-state}"=="unreleased"]
  39. WARNING: Version {version} of the {es} Docker image has not yet been released.
  40. endif::[]
  41. If you're starting a single-node {es} cluster in a Docker container, security
  42. will be automatically enabled and configured for you. When you start {es} for
  43. the first time, the following security configuration occurs automatically:
  44. * <<elasticsearch-security-certificates,Certificates and keys>> are generated
  45. for the transport and HTTP layers.
  46. * The Transport Layer Security (TLS) configuration settings are written to
  47. `elasticsearch.yml`.
  48. * A password is generated for the `elastic` user.
  49. * An enrollment token is generated for {kib}.
  50. You can then {kibana-ref}/docker.html[start {kib}] and enter the enrollment
  51. token, which is valid for 30 minutes. This token automatically applies the
  52. security settings from your {es} cluster, authenticates to {es} with the
  53. `kibana_system` user, and writes the security configuration to `kibana.yml`.
  54. The following command starts a single-node {es} cluster for development or
  55. testing.
  56. . Start {es} in Docker. A password is generated for the `elastic` user and
  57. output to the terminal, plus an enrollment token for enrolling {kib}.
  58. +
  59. --
  60. ifeval::["{release-state}"!="unreleased"]
  61. [source,sh,subs="attributes"]
  62. ----
  63. docker run --name es-node01 -p 9200:9200 -p 9300:9300 -it {docker-image}
  64. ----
  65. endif::[]
  66. --
  67. +
  68. TIP: You might need to scroll back a bit in the terminal to view the password
  69. and enrollment token.
  70. . Copy the generated password and enrollment token and save them in a secure
  71. location. These values are shown only when you start {es} for the first time.
  72. +
  73. [NOTE]
  74. ====
  75. If you need to reset the password for the `elastic` user or other
  76. built-in users, run the <<reset-password,`elasticsearch-reset-password`>> tool.
  77. This tool is available in the {es} `/bin` directory of the Docker container.
  78. For example:
  79. [source,sh]
  80. ----
  81. docker exec -it es-node01 /usr/share/elasticsearch/bin/reset-elastic-password
  82. ----
  83. ====
  84. . Copy the `http_ca.crt` security certificate from your Docker container to
  85. your local machine.
  86. +
  87. [source,sh]
  88. ----
  89. docker cp es-node01:/usr/share/elasticsearch/config/tls_auto_config_*/http_ca.crt .
  90. ----
  91. . Open a new terminal and verify that you can connect to your {es} cluster by
  92. making an authenticated call, using the `http_ca.crt` file that you copied from
  93. your Docker container. Enter the password for the `elastic` user when prompted.
  94. +
  95. [source,sh]
  96. ----
  97. curl --cacert http_ca.crt -u elastic https://localhost:9200
  98. ----
  99. // NOTCONSOLE
  100. ===== Next steps
  101. You now have a test {es} environment set up. Before you start
  102. serious development or go into production with {es}, review the
  103. <<docker-prod-prerequisites,requirements and recommendations>> to apply when running {es} in Docker in production.
  104. [[elasticsearch-security-certificates]]
  105. ===== Security certificates and keys
  106. When you start {es} for the first time, the following certificates and keys are
  107. generated in the
  108. `/usr/share/elasticsearch/config/tls_auto_config_initial_node_<timestamp>`
  109. directory in the Docker container, and allow you to connect a {kib} instance
  110. to your secured {es} cluster and encrypt internode communication. The files are
  111. listed here for reference.
  112. `http_ca.crt`::
  113. The CA certificate that is used to sign the certificates for the HTTP layer of
  114. this {es} cluster.
  115. `http_keystore_local_node.p12`::
  116. Keystore that contains the key and certificate for the HTTP layer for this node.
  117. `transport_keystore_all_nodes.p12`::
  118. Keystore that contains the key and certificate for the transport layer for all
  119. the nodes in your cluster.
  120. [[docker-compose-file]]
  121. ==== Start a multi-node cluster with Docker Compose
  122. When defining multiple nodes in a `docker-compose.yml` file, you'll need to
  123. explicitly enable and configure security so that {es} doesn't try to generate a
  124. password for the `elastic` user on every node.
  125. ===== Prepare the environment
  126. The following example uses Docker Compose to start a three-node {es} cluster.
  127. Create each of the following files inside of a new directory. Copy and paste the
  128. contents of each example into the appropriate file as described in the
  129. following sections:
  130. * <<docker-instances-yml,`instances.yml`>>
  131. * <<docker-env,`.env`>>
  132. * <<docker-create-certs,`create-certs.yml`>>
  133. * <<docker-docker-compose,`docker-compose.yml`>>
  134. [[docker-instances-yml]]
  135. [discrete]
  136. ===== `instances.yml`
  137. When you run the example, {es} uses this file to create a three-node cluster.
  138. The nodes are named `es01`, `es02`,and `es03`.
  139. ifeval::["{release-state}"=="unreleased"]
  140. +
  141. --
  142. WARNING: Version {version} of {es} has not yet been released, so a
  143. `docker-compose.yml` is not available for this version.
  144. endif::[]
  145. ifeval::["{release-state}"!="unreleased"]
  146. [source,yaml,subs="attributes"]
  147. ----
  148. include::instances.yml[]
  149. ----
  150. endif::[]
  151. --
  152. [[docker-env]]
  153. [discrete]
  154. ===== `.env`
  155. The `.env` file sets environment variables that are used when you run the
  156. example. Ensure that you specify a strong password for the `elastic` user with
  157. the `ELASTIC_PASSWORD` variable. This variable is referenced by the
  158. `docker-compose.yml` file.
  159. ifeval::["{release-state}"=="unreleased"]
  160. +
  161. --
  162. WARNING: Version {version} of {es} has not yet been released, so a
  163. `docker-compose.yml` is not available for this version.
  164. endif::[]
  165. ifeval::["{release-state}"!="unreleased"]
  166. [source,yaml,subs="attributes"]
  167. ----
  168. include::.env[]
  169. ----
  170. endif::[]
  171. --
  172. `COMPOSE_PROJECT_NAME`:: Adds an `es_` prefix for all volumes and networks
  173. created by `docker-compose`.
  174. `CERTS_DIR`:: Specifies the path inside the Docker image where {es} expects the
  175. security certificates.
  176. `ELASTIC_PASSWORD`:: Sets the initial password for the `elastic` user.
  177. [discrete]
  178. [[docker-create-certs]]
  179. ===== `create-certs.yml`
  180. The `create-certs.yml` file includes a script that generates node certificates
  181. and a certificate authority (CA) certificate and key where {es} expects them.
  182. These certificates and key are placed in a Docker volume named `es_certs`.
  183. ifeval::["{release-state}"=="unreleased"]
  184. +
  185. --
  186. WARNING: Version {version} of {es} has not yet been released, so a
  187. `docker-compose.yml` is not available for this version.
  188. endif::[]
  189. ifeval::["{release-state}"!="unreleased"]
  190. [source,yaml,subs="attributes"]
  191. ----
  192. include::create-certs.yml[]
  193. ----
  194. endif::[]
  195. --
  196. [[docker-docker-compose]]
  197. [discrete]
  198. ===== `docker-compose.yml`
  199. The `docker-compose.yml` file defines configuration settings for each of your
  200. {es} nodes.
  201. NOTE: This sample `docker-compose.yml` file uses the `ES_JAVA_OPTS`
  202. environment variable to manually set the heap size to 512MB. We do not recommend
  203. using `ES_JAVA_OPTS` in production.
  204. See <<docker-set-heap-size,manually set the heap size>>.
  205. This configuration exposes port `9200` on all network interfaces. Given how
  206. Docker manipulates `iptables` on Linux, this means that your {es} cluster is
  207. publicly accessible, potentially ignoring any firewall settings. If you don't
  208. want to expose port `9200` and instead use a reverse proxy, replace `9200:9200`
  209. with `127.0.0.1:9200:9200` in the `docker-compose.yml` file. {es} will then only
  210. be accessible from the host machine itself.
  211. ifeval::["{release-state}"=="unreleased"]
  212. +
  213. --
  214. WARNING: Version {version} of {es} has not yet been released, so a
  215. `docker-compose.yml` is not available for this version.
  216. endif::[]
  217. ifeval::["{release-state}"!="unreleased"]
  218. [source,yaml,subs="attributes"]
  219. ----
  220. include::docker-compose.yml[]
  221. ----
  222. endif::[]
  223. --
  224. ===== Start your cluster with security enabled and configured
  225. This sample Docker Compose file starts a three-node {es} cluster.
  226. The https://docs.docker.com/storage/volumes[Docker named volumes]
  227. `data01`, `data02`, and `data03` store the node data directories so that the
  228. data persists across restarts. If they don't already exist, running
  229. `docker-compose` creates these volumes.
  230. [[docker-generate-certificates]]
  231. . Generate the certificates. You only need to run this command one time:
  232. +
  233. ["source","sh"]
  234. ----
  235. docker-compose -f create-certs.yml run --rm create_certs
  236. ----
  237. . Start your {es} nodes with TLS configured on the transport layer:
  238. +
  239. ["source","sh"]
  240. ----
  241. docker-compose up -d
  242. ----
  243. +
  244. Node `es01` listens on `localhost:9200` and `es02` and `es03` talk to `es01`
  245. over a Docker network.
  246. . Access the {es} API over TLS using the bootstrapped password for the `elastic`
  247. user that you specified in the `.env` file:
  248. +
  249. ["source","sh",subs="attributes"]
  250. ----
  251. docker run --rm -v es_certs:/certs --network=es_default {docker-image} curl --cacert /certs/ca/ca.crt -u elastic:<password> https://es01:9200
  252. ----
  253. // NOTCONSOLE
  254. +
  255. --
  256. `es_certs`:: The name of the volume that the script in `create-certs.yml`
  257. creates to hold your certificates.
  258. `<password>`:: The password for the `elastic` user, defined by the
  259. `ELASTIC_PASSWORD` variable in the `.env` file.
  260. --
  261. . Submit a `_cat/nodes` request to check that the nodes are up and running:
  262. +
  263. [source,sh]
  264. ----
  265. curl -X GET "https://localhost:9200/_cat/nodes?v=true&pretty"
  266. ----
  267. // NOTCONSOLE
  268. Log messages go to the console and are handled by the configured Docker logging
  269. driver. By default, you can access logs with `docker logs`. If you prefer that
  270. the {es} container write logs to disk, set the `ES_LOG_STYLE` environment
  271. variable to `file`. This causes {es} to use the same logging configuration as
  272. other {es} distribution formats.
  273. If you need to generate a new password for the `elastic` user or any of the
  274. built-in users, use the `elasticsearch-reset-password` tool:
  275. WARNING: Windows users not running PowerShell must remove all backslashes (`\`)
  276. and join lines in the following command.
  277. ["source","sh"]
  278. ----
  279. docker exec es01 /bin/bash -c "bin/elasticsearch-reset-password \
  280. auto --batch \
  281. --url https://localhost:9200"
  282. ----
  283. ===== Stop the cluster
  284. To stop the cluster, run `docker-compose down`. The data in the Docker volumes
  285. is preserved and loaded when you restart the cluster with `docker-compose up`.
  286. --
  287. ["source","sh"]
  288. ----
  289. docker-compose down
  290. ----
  291. --
  292. To **delete the data volumes** when you stop the cluster, specify the `-v`
  293. option:
  294. ["source","sh"]
  295. ----
  296. docker-compose down -v
  297. ----
  298. WARNING: Deleting data volumes will remove the generated security certificates
  299. for your nodes. You will need to run `docker-compose` and
  300. <<docker-generate-certificates,regenerate the security certificates>> before
  301. starting your cluster.
  302. ===== Next steps
  303. You now have a test {es} environment set up. Before you start
  304. serious development or go into production with {es}, review the
  305. <<docker-prod-prerequisites,requirements and recommendations>> to apply when running {es} in Docker in production.
  306. [[docker-prod-prerequisites]]
  307. ==== Using the Docker images in production
  308. The following requirements and recommendations apply when running {es} in Docker in production.
  309. ===== Set `vm.max_map_count` to at least `262144`
  310. The `vm.max_map_count` kernel setting must be set to at least `262144` for production use.
  311. How you set `vm.max_map_count` depends on your platform:
  312. * Linux
  313. +
  314. --
  315. The `vm.max_map_count` setting should be set permanently in `/etc/sysctl.conf`:
  316. [source,sh]
  317. --------------------------------------------
  318. grep vm.max_map_count /etc/sysctl.conf
  319. vm.max_map_count=262144
  320. --------------------------------------------
  321. To apply the setting on a live system, run:
  322. [source,sh]
  323. --------------------------------------------
  324. sysctl -w vm.max_map_count=262144
  325. --------------------------------------------
  326. --
  327. * macOS with https://docs.docker.com/docker-for-mac[Docker for Mac]
  328. +
  329. --
  330. The `vm.max_map_count` setting must be set within the xhyve virtual machine:
  331. . From the command line, run:
  332. +
  333. [source,sh]
  334. --------------------------------------------
  335. screen ~/Library/Containers/com.docker.docker/Data/vms/0/tty
  336. --------------------------------------------
  337. . Press enter and use`sysctl` to configure `vm.max_map_count`:
  338. +
  339. [source,sh]
  340. --------------------------------------------
  341. sysctl -w vm.max_map_count=262144
  342. --------------------------------------------
  343. . To exit the `screen` session, type `Ctrl a d`.
  344. --
  345. * Windows and macOS with https://www.docker.com/products/docker-desktop[Docker Desktop]
  346. +
  347. --
  348. The `vm.max_map_count` setting must be set via docker-machine:
  349. [source,sh]
  350. --------------------------------------------
  351. docker-machine ssh
  352. sudo sysctl -w vm.max_map_count=262144
  353. --------------------------------------------
  354. --
  355. * Windows with https://docs.docker.com/docker-for-windows/wsl[Docker Desktop WSL 2 backend]
  356. +
  357. --
  358. The `vm.max_map_count` setting must be set in the docker-desktop container:
  359. [source,sh]
  360. --------------------------------------------
  361. wsl -d docker-desktop
  362. sysctl -w vm.max_map_count=262144
  363. --------------------------------------------
  364. --
  365. ===== Configuration files must be readable by the `elasticsearch` user
  366. By default, {es} runs inside the container as user `elasticsearch` using
  367. uid:gid `1000:0`.
  368. IMPORTANT: One exception is https://docs.openshift.com/container-platform/3.6/creating_images/guidelines.html#openshift-specific-guidelines[Openshift],
  369. which runs containers using an arbitrarily assigned user ID.
  370. Openshift presents persistent volumes with the gid set to `0`, which works without any adjustments.
  371. If you are bind-mounting a local directory or file, it must be readable by the `elasticsearch` user.
  372. In addition, this user must have write access to the <<path-settings,config, data and log dirs>>
  373. ({es} needs write access to the `config` directory so that it can generate a keystore).
  374. A good strategy is to grant group access to gid `0` for the local directory.
  375. For example, to prepare a local directory for storing data through a bind-mount:
  376. [source,sh]
  377. --------------------------------------------
  378. mkdir esdatadir
  379. chmod g+rwx esdatadir
  380. chgrp 0 esdatadir
  381. --------------------------------------------
  382. You can also run an {es} container using both a custom UID and GID. Unless you
  383. bind-mount each of the `config`, `data` and `logs` directories, you must pass
  384. the command line option `--group-add 0` to `docker run`. This ensures that the user
  385. under which {es} is running is also a member of the `root` (GID 0) group inside the
  386. container.
  387. ===== Increase ulimits for nofile and nproc
  388. Increased ulimits for <<setting-system-settings,nofile>> and <<max-number-threads-check,nproc>>
  389. must be available for the {es} containers.
  390. Verify the https://github.com/moby/moby/tree/ea4d1243953e6b652082305a9c3cda8656edab26/contrib/init[init system]
  391. for the Docker daemon sets them to acceptable values.
  392. To check the Docker daemon defaults for ulimits, run:
  393. [source,sh]
  394. --------------------------------------------
  395. docker run --rm centos:8 /bin/bash -c 'ulimit -Hn && ulimit -Sn && ulimit -Hu && ulimit -Su'
  396. --------------------------------------------
  397. If needed, adjust them in the Daemon or override them per container.
  398. For example, when using `docker run`, set:
  399. [source,sh]
  400. --------------------------------------------
  401. --ulimit nofile=65535:65535
  402. --------------------------------------------
  403. ===== Disable swapping
  404. Swapping needs to be disabled for performance and node stability.
  405. For information about ways to do this, see <<setup-configuration-memory>>.
  406. If you opt for the `bootstrap.memory_lock: true` approach,
  407. you also need to define the `memlock: true` ulimit in the
  408. https://docs.docker.com/engine/reference/commandline/dockerd/#default-ulimits[Docker Daemon],
  409. or explicitly set for the container as shown in the <<docker-compose-file, sample compose file>>.
  410. When using `docker run`, you can specify:
  411. [source,sh]
  412. ----
  413. -e "bootstrap.memory_lock=true" --ulimit memlock=-1:-1
  414. ----
  415. ===== Randomize published ports
  416. The image https://docs.docker.com/engine/reference/builder/#/expose[exposes]
  417. TCP ports 9200 and 9300. For production clusters, randomizing the
  418. published ports with `--publish-all` is recommended,
  419. unless you are pinning one container per host.
  420. [[docker-set-heap-size]]
  421. ===== Manually set the heap size
  422. By default, {es} automatically sizes JVM heap based on a nodes's
  423. <<node-roles,roles>> and the total memory available to the node's container. We
  424. recommend this default sizing for most production environments. If needed, you
  425. can override default sizing by manually setting JVM heap size.
  426. To manually set the heap size in production, bind mount a <<set-jvm-options,JVM
  427. options>> file under `/usr/share/elasticsearch/config/jvm.options.d` that
  428. includes your desired <<set-jvm-heap-size,heap size>> settings.
  429. For testing, you can also manually set the heap size using the `ES_JAVA_OPTS`
  430. environment variable. For example, to use 16GB, specify `-e
  431. ES_JAVA_OPTS="-Xms16g -Xmx16g"` with `docker run`. The `ES_JAVA_OPTS` variable
  432. overrides all other JVM options. The `ES_JAVA_OPTS` variable overrides all other
  433. JVM options. We do not recommend using `ES_JAVA_OPTS` in production. The
  434. `docker-compose.yml` file above sets the heap size to 512MB.
  435. ===== Pin deployments to a specific image version
  436. Pin your deployments to a specific version of the {es} Docker image. For
  437. example +docker.elastic.co/elasticsearch/elasticsearch:{version}+.
  438. ===== Always bind data volumes
  439. You should use a volume bound on `/usr/share/elasticsearch/data` for the following reasons:
  440. . The data of your {es} node won't be lost if the container is killed
  441. . {es} is I/O sensitive and the Docker storage driver is not ideal for fast I/O
  442. . It allows the use of advanced
  443. https://docs.docker.com/engine/extend/plugins/#volume-plugins[Docker volume plugins]
  444. ===== Avoid using `loop-lvm` mode
  445. If you are using the devicemapper storage driver, do not use the default `loop-lvm` mode.
  446. Configure docker-engine to use
  447. https://docs.docker.com/engine/userguide/storagedriver/device-mapper-driver/#configure-docker-with-devicemapper[direct-lvm].
  448. ===== Centralize your logs
  449. Consider centralizing your logs by using a different
  450. https://docs.docker.com/engine/admin/logging/overview/[logging driver]. Also
  451. note that the default json-file logging driver is not ideally suited for
  452. production use.
  453. [[docker-configuration-methods]]
  454. ==== Configuring {es} with Docker
  455. When you run in Docker, the <<config-files-location,{es} configuration files>> are loaded from
  456. `/usr/share/elasticsearch/config/`.
  457. To use custom configuration files, you <<docker-config-bind-mount, bind-mount the files>>
  458. over the configuration files in the image.
  459. You can set individual {es} configuration parameters using Docker environment variables.
  460. The <<docker-compose-file, sample compose file>> and the
  461. <<docker-cli-run-dev-mode, single-node example>> use this method. You can
  462. use the setting name directly as the environment variable name. If
  463. you cannot do this, for example because your orchestration platform forbids
  464. periods in environment variable names, then you can use an alternative
  465. style by converting the setting name as follows.
  466. . Change the setting name to uppercase
  467. . Prefix it with `ES_SETTING_`
  468. . Escape any underscores (`_`) by duplicating them
  469. . Convert all periods (`.`) to underscores (`_`)
  470. For example, `-e bootstrap.memory_lock=true` becomes
  471. `-e ES_SETTING_BOOTSTRAP_MEMORY__LOCK=true`.
  472. You can use the contents of a file to set the value of the
  473. `ELASTIC_PASSWORD` or `KEYSTORE_PASSWORD` environment variables, by
  474. suffixing the environment variable name with `_FILE`. This is useful for
  475. passing secrets such as passwords to {es} without specifying them directly.
  476. For example, to set the {es} bootstrap password from a file, you can bind mount the
  477. file and set the `ELASTIC_PASSWORD_FILE` environment variable to the mount location.
  478. If you mount the password file to `/run/secrets/bootstrapPassword.txt`, specify:
  479. [source,sh]
  480. --------------------------------------------
  481. -e ELASTIC_PASSWORD_FILE=/run/secrets/bootstrapPassword.txt
  482. --------------------------------------------
  483. You can override the default command for the image to pass {es} configuration
  484. parameters as command line options. For example:
  485. [source,sh]
  486. --------------------------------------------
  487. docker run <various parameters> bin/elasticsearch -Ecluster.name=mynewclustername
  488. --------------------------------------------
  489. While bind-mounting your configuration files is usually the preferred method in production,
  490. you can also <<_c_customized_image, create a custom Docker image>>
  491. that contains your configuration.
  492. [[docker-config-bind-mount]]
  493. ===== Mounting {es} configuration files
  494. Create custom config files and bind-mount them over the corresponding files in the Docker image.
  495. For example, to bind-mount `custom_elasticsearch.yml` with `docker run`, specify:
  496. [source,sh]
  497. --------------------------------------------
  498. -v full_path_to/custom_elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
  499. --------------------------------------------
  500. If you bind-mount a custom `elasticsearch.yml` file, ensure it includes the
  501. `network.host: 0.0.0.0` setting. This setting ensures the node is reachable for
  502. HTTP and transport traffic, provided its ports are exposed. The Docker image's
  503. built-in `elasticsearch.yml` file includes this setting by default.
  504. IMPORTANT: The container **runs {es} as user `elasticsearch` using
  505. uid:gid `1000:0`**. Bind mounted host directories and files must be accessible by this user,
  506. and the data and log directories must be writable by this user.
  507. [[docker-keystore-bind-mount]]
  508. ===== Create an encrypted {es} keystore
  509. By default, {es} will auto-generate a keystore file for <<secure-settings,secure
  510. settings>>. This file is obfuscated but not encrypted.
  511. To encrypt your secure settings with a password and have them persist outside
  512. the container, use a `docker run` command to manually create the keystore
  513. instead. The command must:
  514. * Bind-mount the `config` directory. The command will create an
  515. `elasticsearch.keystore` file in this directory. To avoid errors, do
  516. not directly bind-mount the `elasticsearch.keystore` file.
  517. * Use the `elasticsearch-keystore` tool with the `create -p` option. You'll be
  518. prompted to enter a password for the keystore.
  519. ifeval::["{release-state}"!="unreleased"]
  520. For example:
  521. [source,sh,subs="attributes"]
  522. ----
  523. docker run -it --rm \
  524. -v full_path_to/config:/usr/share/elasticsearch/config \
  525. docker.elastic.co/elasticsearch/elasticsearch:{version} \
  526. bin/elasticsearch-keystore create -p
  527. ----
  528. You can also use a `docker run` command to add or update secure settings in the
  529. keystore. You'll be prompted to enter the setting values. If the keystore is
  530. encrypted, you'll also be prompted to enter the keystore password.
  531. [source,sh,subs="attributes"]
  532. ----
  533. docker run -it --rm \
  534. -v full_path_to/config:/usr/share/elasticsearch/config \
  535. docker.elastic.co/elasticsearch/elasticsearch:{version} \
  536. bin/elasticsearch-keystore \
  537. add my.secure.setting \
  538. my.other.secure.setting
  539. ----
  540. endif::[]
  541. If you've already created the keystore and don't need to update it, you can
  542. bind-mount the `elasticsearch.keystore` file directly. You can use the
  543. `KEYSTORE_PASSWORD` environment variable to provide the keystore password to the
  544. container at startup. For example, a `docker run` command might have the
  545. following options:
  546. [source,sh]
  547. ----
  548. -v full_path_to/config/elasticsearch.keystore:/usr/share/elasticsearch/config/elasticsearch.keystore
  549. -e KEYSTORE_PASSWORD=mypassword
  550. ----
  551. [[_c_customized_image]]
  552. ===== Using custom Docker images
  553. In some environments, it might make more sense to prepare a custom image that contains
  554. your configuration. A `Dockerfile` to achieve this might be as simple as:
  555. [source,sh,subs="attributes"]
  556. --------------------------------------------
  557. FROM docker.elastic.co/elasticsearch/elasticsearch:{version}
  558. COPY --chown=elasticsearch:elasticsearch elasticsearch.yml /usr/share/elasticsearch/config/
  559. --------------------------------------------
  560. You could then build and run the image with:
  561. [source,sh]
  562. --------------------------------------------
  563. docker build --tag=elasticsearch-custom .
  564. docker run -ti -v /usr/share/elasticsearch/data elasticsearch-custom
  565. --------------------------------------------
  566. Some plugins require additional security permissions.
  567. You must explicitly accept them either by:
  568. * Attaching a `tty` when you run the Docker image and allowing the permissions when prompted.
  569. * Inspecting the security permissions and accepting them (if appropriate) by adding the `--batch` flag to the plugin install command.
  570. See {plugins}/_other_command_line_parameters.html[Plugin management]
  571. for more information.
  572. The {es} Docker image only includes what is required to run {es}, and does
  573. not provide a package manager. It is possible to add additional utilities
  574. with a multi-phase Docker build. You must also copy any dependencies, for
  575. example shared libraries.
  576. [source,sh,subs="attributes"]
  577. --------------------------------------------
  578. FROM centos:8 AS builder
  579. yum install -y some-package
  580. FROM docker.elastic.co/elasticsearch/elasticsearch:{version}
  581. COPY --from=builder /usr/bin/some-utility /usr/bin/
  582. COPY --from=builder /usr/lib/some-lib.so /usr/lib/
  583. --------------------------------------------
  584. You should use `centos:8` as a base in order to avoid incompatibilities.
  585. Use http://man7.org/linux/man-pages/man1/ldd.1.html[`ldd`] to list the
  586. shared libraries required by a utility.
  587. [discrete]
  588. [[troubleshoot-docker-errors]]
  589. ==== Troubleshoot Docker errors for {es}
  590. Here’s how to resolve common errors when running {es} with Docker.
  591. ===== elasticsearch.keystore is a directory
  592. [source,txt]
  593. ----
  594. 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
  595. ----
  596. A <<docker-keystore-bind-mount,keystore-related>> `docker run` command attempted
  597. to directly bind-mount an `elasticsearch.keystore` file that doesn't exist. If
  598. you use the `-v` or `--volume` flag to mount a file that doesn't exist, Docker
  599. instead creates a directory with the same name.
  600. To resolve this error:
  601. . Delete the `elasticsearch.keystore` directory in the `config` directory.
  602. . Update the `-v` or `--volume` flag to point to the `config` directory path
  603. rather than the keystore file's path. For an example, see
  604. <<docker-keystore-bind-mount>>.
  605. . Retry the command.
  606. ===== elasticsearch.keystore: Device or resource busy
  607. [source,txt]
  608. ----
  609. 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
  610. ----
  611. A `docker run` command attempted to <<docker-keystore-bind-mount,update the
  612. keystore>> while directly bind-mounting the `elasticsearch.keystore` file. To
  613. update the keystore, the container requires access to other files in the
  614. `config` directory, such as `keystore.tmp`.
  615. To resolve this error:
  616. . Update the `-v` or `--volume` flag to point to the `config` directory
  617. path rather than the keystore file's path. For an example, see
  618. <<docker-keystore-bind-mount>>.
  619. . Retry the command.