elasticsearch
<p>参考官方文档
<code>https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html</code>
elasticsearch8</p>
<pre><code>version: &#039;3.7&#039;
services:
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:8.17.2
container_name: elasticsearch
environment:
- discovery.type=single-node
- ES_JAVA_OPTS=-Xms2g -Xmx2g
- ELASTIC_PASSWORD=yourpassword # Replace with your desired password
ulimits:
memlock:
soft: -1
hard: -1
ports:
- &quot;9200:9200&quot;
- &quot;9300:9300&quot;
volumes:
- ./elasticsearch-data:/usr/share/elasticsearch/data
networks:
- es_network
restart: unless-stopped
networks:
es_network:
driver: bridge
volumes:
elasticsearch-data:
driver: local
</code></pre>
<p>elasticsearch7</p>
<pre><code>version: &#039;3.7&#039;
services:
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:7.17.28 # 修改版本号
container_name: elasticsearch
environment:
- discovery.type=single-node
- ES_JAVA_OPTS=-Xms2g -Xmx2g
- xpack.security.enabled=true # 7.x 需显式启用安全模块
- ELASTIC_PASSWORD=yourpassword # 替换为你的密码
ulimits:
memlock:
soft: -1
hard: -1
ports:
- &quot;9200:9200&quot;
- &quot;9300:9300&quot;
volumes:
- ./elasticsearch-data:/usr/share/elasticsearch/data
networks:
- es_network
restart: unless-stopped
networks:
es_network:
driver: bridge
volumes:
elasticsearch-data:
driver: local</code></pre>
<p>成功后访问
<a href="http://localhost:9200/">http://localhost:9200/</a>
用户名 elastic
密码 yourpassword</p>
<h3>解释:</h3>
<ul>
<li><strong>image</strong>:指定 Elasticsearch 8.17.2 版本的 Docker 镜像。</li>
<li><strong>environment</strong>:设置环境变量:
<ul>
<li><code>discovery.type=single-node</code>:配置 Elasticsearch 以单节点模式运行。</li>
<li><code>ES_JAVA_OPTS</code>:设置 Java 堆内存选项(根据系统的可用内存进行调整)。</li>
<li><code>ELASTIC_PASSWORD</code>:设置默认 <code>elastic</code> 用户的密码(用你自己的密码替换 <code>yourpassword</code>)。</li>
</ul></li>
<li><strong>ulimits</strong>:设置内存锁定限制,防止 Elasticsearch 将内存交换到磁盘。</li>
<li><strong>ports</strong>:映射必要的端口(<code>9200</code> 用于 HTTP,<code>9300</code> 用于节点间通信)。</li>
<li><strong>volumes</strong>:挂载卷来持久化数据。</li>
<li><strong>networks</strong>:定义一个用于 Elasticsearch 服务的自定义网络。</li>
<li><strong>restart</strong>:配置服务为 "除非停止" 的自动重启策略。</li>
</ul>
<p>确保替换密码并根据你的环境需要调整配置。</p>