简体   繁体   中英

Cannot obtain elasticsearch version from docker container as one-liner

I tried to get Elasticsearch version by one-liner command:

$ docker run --rm -ti --memory=512m --cpus=1 docker.elastic.co/elasticsearch/elasticsearch:8.0.0 elasticsearch --version

It starts Elasticsearch, show logs and did not return version. In case, I run Elasticsearch with bash, and ask for Elasticsearch version, it works fine:

$ docker run --rm -ti --memory=512m --cpus=1 docker.elastic.co/elasticsearch/elasticsearch:8.0.0 /bin/bash
$ elasticsearch --version
Version: 8.0.0, Build: default/docker/1b6a7ece17463df5ff54a3e1302d825889aa1161/2022-02-03T16:47:57.507843096Z, JVM: 17.0.1

When I test it with date command, it works fine:

$ docker run --rm -ti --memory=512m --cpus=1 docker.elastic.co/elasticsearch/elasticsearch:8.0.0 /bin/date                                                                                                                                            
WARNING: Your kernel does not support swap limit capabilities or the cgroup is not mounted. Memory limited without swap.
Fri Feb 18 14:25:08 UTC 2022

Why I cannot obtain Elasticsearch version from command line as one-liner but it works with date command?

To make sure only the given command is run you should set the entrypoint to empty string while executing the docker run.

docker run --rm --entrypoint '' docker.elastic.co/elasticsearch/elasticsearch:8.0.0 elasticsearch --version

In docker run, if an entrypoint is defined in an image, the command is passed as an argument to this entrypoint. And there is a logic in the current entrypoint that executes the passed command unless it is elasticsearch.

A related code in the entrypoint that caused the observed behaviour:

# docker run --rm docker.elastic.co/elasticsearch/elasticsearch:8.0.0 cat /usr/local/bin/docker-entrypoint.sh

#!/bin/bash
set -e

# Files created by Elasticsearch should always be group writable too
umask 0002

# Allow user specify custom CMD, maybe bin/elasticsearch itself
# for example to directly specify `-E` style parameters for elasticsearch on k8s
# or simply to run /bin/bash to check the image
if [[ "$1" == "eswrapper" || $(basename "$1") == "elasticsearch" ]]; then
  # Rewrite CMD args to remove the explicit command,
  # so that we are backwards compatible with the docs
  # from the previous Elasticsearch versions < 6
  # and configuration option:
  # https://www.elastic.co/guide/en/elasticsearch/reference/5.6/docker.html#_d_override_the_image_8217_s_default_ulink_url_https_docs_docker_com_engine_reference_run_cmd_default_command_or_options_cmd_ulink
  # Without this, user could specify `elasticsearch -E x.y=z` but
  # `bin/elasticsearch -E x.y=z` would not work. In any case,
  # we want to continue through this script, and not exec early.
  set -- "${@:2}"
else
  # Run whatever command the user wanted
  exec "$@"
fi
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM