【发布时间】:2019-07-29 14:56:52
【问题描述】:
我正在尝试创建一个安装了 Elasticsearch 的 Docker 映像。我知道 Elasticsearch 已经有他的自定义 Docker 映像,但我需要使用安装了 Python 3.6.5 的 AWS Lambda 函数的相同环境。所以我不得不扩展 AWS Docker 镜像,并且必须安装 Elasticsearch 服务。
在这里你可以找到我的 Dockerfile:
FROM lambci/lambda-base:build
ENV PATH=/var/lang/bin:$PATH \
LD_LIBRARY_PATH=/var/lang/lib:$LD_LIBRARY_PATH \
AWS_EXECUTION_ENV=AWS_Lambda_python3.6 \
PYTHONPATH=/var/runtime \
PKG_CONFIG_PATH=/var/lang/lib/pkgconfig:/usr/lib64/pkgconfig:/usr/share/pkgconfig
RUN rm -rf /var/runtime /var/lang && \
curl https://lambci.s3.amazonaws.com/fs/python3.6.tgz | tar -xz -C / && \
sed -i '/^prefix=/c\prefix=/var/lang' /var/lang/lib/pkgconfig/python-3.6.pc && \
curl https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tar.xz | tar -xJ && \
cd Python-3.6.8 && \
LIBS="$LIBS -lutil -lrt" ./configure --prefix=/var/lang && \
make -j$(getconf _NPROCESSORS_ONLN) libinstall libainstall inclinstall && \
cd .. && \
rm -rf Python-3.6.8
EXPOSE 9200 9300
# Add these as a separate layer as they get updated frequently
RUN pip install -U pip setuptools --no-cache-dir && \
pip install -U virtualenv pipenv --no-cache-dir && \
pip install -U awscli boto3 aws-sam-cli==0.10.0 aws-lambda-builders==0.1.0 --no-cache-dir && \
yum install wget -y && \
echo "vm.max_map_count=262144" >> /etc/sysctl.conf && \
# I try to install elasticsearch manually
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.6.1.rpm && \
rpm --install elasticsearch-6.6.1.rpm && \
service elasticsearch start && \
chkconfig --add elasticsearch && \
sed -i -e "s/#network.host: 192.168.0.1/network.host: 127.0.0.1/g" /etc/elasticsearch/elasticsearch.yml && \
sed -i -e "s/#http.port: 9200/http.port: 9200/g" /etc/elasticsearch/elasticsearch.yml && \
sed -i -e "s/# Elasticsearch performs poorly when the system is swapping the memory./network.bind_host: 127.0.0.1\nnetwork.publish_host: localhost/g" /etc/elasticsearch/elasticsearch.yml
RUN service elasticsearch start && \
service elasticsearch status && \
service elasticsearch status
当我构建镜像时,镜像构建正确,并且 elasticsearch 服务正在运行。我可以看到日志:elasticsearch (pid 87) is running...。问题是我无法对http://localhost:9200 进行API 调用,因为它说:Failed to connect to localhost port 9200: Connection refused
我需要在容器内部使用 elasticsearch 服务,而不是从外部使用。我做错了什么?
【问题讨论】:
-
您无法在 Dockerfile 中启动后台服务(它们不会保留在映像中)。 Docker 中
service的典型用法由于几个原因不起作用,您应该直接启动该服务。 (尝试另一个RUN service elasticsearch status命令,看看它说了什么。) -
另外,
bind_host: 127.0.0.1设置保证服务将无法从容器外部访问。在 Docker 中通常需要 0.0.0.0。 -
@DavidMaze 你是对的。如果我运行另一个
RUN service elasticsearch status命令,我会得到elasticsearch dead but pid file exists。如何直接启动服务?我不需要使用容器外的服务,但需要使用容器内的elasticsearch服务。 -
@DavidMaze 我还在 Dockerfile 的末尾添加了命令
CMD elasticsearch start && bash,但错误是一样的。
标签: amazon-web-services docker elasticsearch dockerfile aws-codebuild