使用dockerfile运行容器时如何运行.sh文件 [英] how to run .sh file when container is running using dockerfile

查看:4471
本文介绍了使用dockerfile运行容器时如何运行.sh文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个dockerfile来安装elasticsearch:6.5.4,并将几个文件添加到所需位置,并运行一个名为test.sh的脚本,以便在Elasticsearch运行时在elasticsearch中创建新索引。

I'm making a dockerfile to install elasticsearch:6.5.4 and add few files to required locations and run a script named test.sh to create a new index in elasticsearch while elasticsearch is running.

我不确定是否应该使用RUN,CMD或ENTRYPOINT来做到这一点。

I'm not sure whether i should use RUN, CMD or ENTRYPOINT to do that.

我已经成功构建了映像并运行通过注释我的最后一行(包含RUN / CMD / ENTRYPOINT test.sh)来添加一个容器。我能够从容器的bash运行test.sh并获得所需的结果。

I've successfully built an image and run a container by commenting my last line (containing RUN/CMD/ENTRYPOINT test.sh). I was able to run the test.sh from bash of container and get the desired result.

但是当我尝试为同一过程构建映像时,得到以下内容错误:

but when i try to build an image for same process, i get the following error:

$ docker build -t es .
Sending build context to Docker daemon  7.499MB
Step 1/8 : FROM elasticsearch:6.5.4
 ---> 93109ce1d590
Step 2/8 : WORKDIR /app
 ---> Running in 6b6412093d53
Removing intermediate container 6b6412093d53
 ---> a374ab69eb1a
Step 3/8 : ADD . /app
 ---> 6ed98ee7ad49
Step 4/8 : COPY test.sh .
 ---> 42184ec64c09
Step 5/8 : ADD analysis /usr/share/elasticsearch/config/analysis
 ---> 5a96f2098dd7
Step 6/8 : EXPOSE 9202
 ---> Running in 6c44b54dcc77
Removing intermediate container 6c44b54dcc77
 ---> d8723189c843
Step 7/8 : EXPOSE 9200
 ---> Running in c571b4cba1fa
Removing intermediate container c571b4cba1fa
 ---> 8fa11b03051e
Step 8/8 : RUN "sh test.sh"
 ---> Running in cf2e8cb3fd37
/bin/sh: sh test.sh: command not found
The command '/bin/sh -c "sh test.sh"' returned a non-zero code: 127

我为STEP 8尝试了RUN,CMD和ENTRYPOINT的不同组合

I've tried different combinations of RUN, CMD and ENTRYPOINT for STEP 8

我的dockerfile如下:

my dockerfile is as follows :

FROM elasticsearch:6.5.4
WORKDIR /app
ADD . /app
COPY test.sh .
ADD analysis /usr/share/elasticsearch/config/analysis

EXPOSE 9202
EXPOSE 9200

RUN "sh test.sh"

我想在容器中运行elasticsearch并为elasticsearch创建新索引

I want to run elasticsearch in container and make a new index for elasticsearch

推荐答案

在纯机械层面上,引号引起了麻烦。当您说

At a purely mechanical level, the quotes are causing trouble. When you say

RUN "sh test.sh"

它尝试运行一个名为 sh\ test.sh 的命令;它不会尝试使用 test.sh 作为参数运行 sh 。以下任何内容都将实际运行脚本

it tries to run a single command named sh\ test.sh; it does not try to run sh with test.sh as a parameter. Any of the following will actually run the script

RUN ["sh", "test.sh"]
RUN sh test.sh
RUN chmod +x test.sh; ./test.sh

在操作级别,您在运行该命令时会遇到很多麻烦服务器容器。最大的问题是服务器已经启动并运行后,您需要运行该命令。因此,您根本无法在Dockerfile中运行它( RUN 命令中从未运行过任何服务)。容器运行一个进程,您需要将该进程作为Elasticsearch服务器本身,因此您不能直接在 ENTRYPOINT CMD <

At an operational level you'll have a lot of trouble running that command in the server container at all. The big problem is that you need to run that command after the server is already up and running. So you can't run it in the Dockerfile at all (no services are ever running in a RUN command). A container runs a single process and you need that process to be the Elasticsearch server itself, so you can't do this directly in ENTRYPOINT or CMD either.

最简单的路径是从主机运行此命令:

The easiest path is to run this command from the host:

docker build -t my/elasticsearch .
docker run -d --name my-elasticsearch -p 9200:9200 my/elasticsearch
curl http://localhost:9200  # is it alive?
./test.sh

如果您具有Docker Compose设置,则也可以运行这可以从单独的容器中获取,也可以在应用程序容器启动时将其运行。在运行您的应用程序容器的 ENTRYPOINT 脚本中,有一些很好的示例来运行数据库迁移,这基本上就是您要寻找的模式。

If you have a Docker Compose setup, you could also run this from a separate container, or you could run it as part of the startup of your application container. There are some good examples of running database migrations in an ENTRYPOINT script for your application container running around, and that's basically the pattern you're looking for.

(从理论上说,可以在入口点脚本中运行它。您必须启动服务器,等待其启动,运行脚本,然后停止服务器,最后是 exec $ @ 来运行 CMD 。对于Elasticsearch来说,这比较棘手,因为您可能需要连接到同一Elasticsearch集群中的其他服务器,以免状态不同步。官方Docker Hub mysql 针对非集群数据库服务器执行此操作;请参见其颇为复杂的入口点脚本提供了一些想法。 )

(It is theoretically possible to run this in an entrypoint script. You have to start the server, wait for it to be up, run your script, stop the server, and then finally exec "$@" to run the CMD. This is trickier for Elasticsearch, where you might need to connect to other servers in the same Elasticsearch cluster lest your state get out of sync. The official Docker Hub mysql does this, for a non-clustered database server; see its rather involved entrypoint script for ideas.)

这篇关于使用dockerfile运行容器时如何运行.sh文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆