how to run .sh file when container is running using dockerfile how to run .sh file when container is running using dockerfile elasticsearch elasticsearch

how to run .sh file when container is running using dockerfile


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

RUN "sh test.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.shRUN chmod +x test.sh; ./test.sh

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/elasticsearchcurl http://localhost:9200  # is it alive?./test.sh

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.

(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.)


RUN "sh test.sh"

Remove the quotes. Your script will try to run a command named sh test.sh (with space).


After small diving into your problem I think you miss one step, run chmod +x test.sh command before running actual script, because it might not be executable in container environment. Also, I personally prefer running shell scripts with bash.

Dockerfile:

FROM elasticsearch:6.5.4WORKDIR /appADD . /appCOPY test.sh .ADD analysis /usr/share/elasticsearch/config/analysisEXPOSE 9202EXPOSE 9200RUN chmod +x test.shRUN bash test.sh