docker安装Elasticsearch的具体方法详见:https://blog.csdn.net/qq_40942490/article/details/111594267,本文仅作脚本化的练习,有什么建议,欢迎指正
制作bash脚本
#/bin/bash
# 获取镜像的ID
export image_id=`docker images | grep "elasticsearch" | awk {'print $3'}`
echo $image_id
# 没有镜像ID则拉取
if [ ! $image_id ];
then
echo "es docker image is not installed"
# 拉取镜像
docker pull elasticsearch:7.7.0
fi
echo "es docker image has installed"
# 获取容器ID,如果存在则停止当前容器,
export container_id=`docker ps -a | grep elasticsearch | awk {'print $1'}`
if [ $container_id ];
then
echo $container_id
docker stop $container_id
docker rm $container_id
echo "remove es container"
fi
#启动新容器
docker run --name elasticsearch -d -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -e "discovery.type=single-node" -p 9200:9200 -p 9300:9300 elasticsearch:7.7.0
echo "es docker container is starting"
小结
提示:bash脚本的书写规则还是很严格的,尤其是一些空格的位置,重点是‘=’ '[ ]'的空格要求