拉取镜像:
docker pull elasticsearch:7.4.2
docker pull kibana:7.4.2
创建实例:
- mkdir -p /mydata/elasticsearch/config
- mkdir -p /mydata/elasticsearch/data
- echo "http.host: 0.0.0.0" >> /mydata/elasticsearch/config/elasticsearch.yml
启动容器并并建立容器和宿主机目录的关联
docker run --name elasticsearch -p 9200:9200 -p 9300:9300 \ -e "discovery.type=single-node" \ -e ES_JAVA_OPTS="-Xms64m -Xmx512m" \ -v /mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \ -v /mydata/elasticsearch/data:/usr/share/elasticsearch/data \ -v /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins \ -d elasticsearch:7.4.2
提示下面的错误:
docker: Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: error mounting "/mydata/elasticsearch/config/elasticsearch.yml" to rootfs at "/usr/share/elasticsearch/config/elasticsearch.yml": mount /mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml (via /proc/self/fd/6), flags: 0x5000: not a directory: unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type.
出错原因在elasticsearch.yml文件的挂载上,根源是这一行代码
echo "http.host: 0.0.0.0" >> /mydata/elasticsearch/config/elasticsearch.yml
由于config是目录,elasticsearch.yml是文件,它们原先都不存在,直接往一个不存在的目录里的文件写入内容,虽然能创建目录成功并将内容写入文件,但以上面命令启动容器的时候会发现无法正常启动,正确的方式是如果涉及到文件,应该先创建目录,再创建目录里的文件,往文件里写内容,再以上面的命令启动容器就没问题了。参照下图: