CMD does not run if used after ENTRYPOINT.

huangapple go评论109阅读模式
英文:

CMD does not run if used after ENTRYPOINT

问题

我有以下的Docker文件

FROM confluentinc/cp-kafka-connect:5.3.1

RUN apt-get update && apt-get -y install cron

ENV CONNECT_PLUGIN_PATH=/usr/share/java

# JDBC-MariaDB
RUN wget -nv -P /usr/share/java/kafka-connect-jdbc/ https://downloads.mariadb.com/Connectors/java/connector-java-2.4.4/mariadb-java-client-2.4.4.jar

# SNMP Source
RUN wget -nv -P /tmp/ https://github.com/name/kafka-connect-snmp/releases/download/0.0.1.11/kafka-connect-snmp-0.0.1.11.tar.gz
RUN mkdir /tmp/kafka-connect-snmp && tar -xf /tmp/kafka-connect-snmp-0.0.1.11.tar.gz -C /tmp/kafka-connect-snmp/
RUN mv /tmp/kafka-connect-snmp/usr/share/kafka-connect/kafka-connect-snmp /usr/share/java/

COPY plugins-config.sh /usr/share/kafka-connect-script/plugins-config.sh
RUN chmod +x /usr/share/kafka-connect-script/plugins-config.sh

ENTRYPOINT [ "./etc/confluent/docker/run" ]

CMD ["/usr/share/kafka-connect-script/plugins-config.sh"]

以及这个bash文件

#!/bin/bash

#用于配置带有插件的kafka连接的脚本
# export CONNECT_REST_ADVERTISED_HOST_NAME=localhost
# export CONNECT_REST_PORT=8083
url=http://$CONNECT_REST_ADVERTISED_HOST_NAME:$CONNECT_REST_PORT/connectors
curl_command="curl -s -o /dev/null -w %{http_code} $url"
sleep_second=5
sleep_second_counter=0
max_seconds_to_wait=60

echo "等待Kafka Connect开始在本地监听" >> log.log
echo "主机: $CONNECT_REST_ADVERTISED_HOST_NAME , 端口: $CONNECT_REST_PORT" >> log.log
while [[ $(eval $curl_command) -eq 000 && $sleep_second_counter -lt $max_seconds_to_wait ]]
 do 
    echo "中" >> log.log
    echo -e $date " Kafka Connect监听HTTP状态: " $(eval $curl_command) " (等待200) $sleep_second_counter" >> log.log
    echo "将休眠 $sleep_second 秒" >> log.log
    sleep $sleep_second 
    echo "休眠完成" >> log.log
    ((sleep_second_counter+=$sleep_second))
    echo "完成计数" >> log.log
done
echo "结束" >> log.log
nc -vz $CONNECT_REST_ADVERTISED_HOST_NAME $CONNECT_REST_PORT

/bin/bash

入口点被正确调用,但CMD没有被调用。

我也尝试理解这里提供的解决方案 https://stackoverflow.com/questions/54447913/cmd-doesnt-run-after-entrypoint-in-dockerfile

但我没有理解解决方案。

如果有人能够更详细地解释这里有什么问题,那将会很有帮助。

我试图完成的目标

我试图创建一个单一的Docker容器映像,该映像将启动kafka-connect服务器(ENTRYPOINT),然后通过bash文件(CMD)配置插件。要求每次容器重新启动时都执行相同的步骤。

英文:

I have the following docker file

FROM confluentinc/cp-kafka-connect:5.3.1

RUN apt-get update && apt-get -y install cron

ENV CONNECT_PLUGIN_PATH=/usr/share/java

# JDBC-MariaDB
RUN wget -nv -P /usr/share/java/kafka-connect-jdbc/ https://downloads.mariadb.com/Connectors/java/connector-java-2.4.4/mariadb-java-client-2.4.4.jar

# SNMP Source
RUN wget -nv -P /tmp/ https://github.com/name/kafka-connect-snmp/releases/download/0.0.1.11/kafka-connect-snmp-0.0.1.11.tar.gz
RUN mkdir /tmp/kafka-connect-snmp && tar -xf /tmp/kafka-connect-snmp-0.0.1.11.tar.gz -C /tmp/kafka-connect-snmp/
RUN mv /tmp/kafka-connect-snmp/usr/share/kafka-connect/kafka-connect-snmp /usr/share/java/

COPY plugins-config.sh /usr/share/kafka-connect-script/plugins-config.sh
RUN chmod +x /usr/share/kafka-connect-script/plugins-config.sh

ENTRYPOINT [ "./etc/confluent/docker/run" ]

CMD ["/usr/share/kafka-connect-script/plugins-config.sh"]

And the bash file as this

#!/bin/bash

#script to configure kafka connect with plugins
# export CONNECT_REST_ADVERTISED_HOST_NAME=localhost
# export CONNECT_REST_PORT=8083
url=http://$CONNECT_REST_ADVERTISED_HOST_NAME:$CONNECT_REST_PORT/connectors
curl_command="curl -s -o /dev/null -w %{http_code} $url"
sleep_second=5
sleep_second_counter=0
max_seconds_to_wait=60

echo "Waiting for Kafka Connect to start listening on localhost" >> log.log
echo "HOST: $CONNECT_REST_ADVERTISED_HOST_NAME , PORT: $CONNECT_REST_PORT" >> log.log
while [[ $(eval $curl_command) -eq 000 && $sleep_second_counter -lt $max_seconds_to_wait ]]
 do 
    echo "In" >> log.log
    echo -e $date " Kafka Connect listener HTTP state: " $(eval $curl_command) " (waiting for 200) $sleep_second_counter" >> log.log
    echo "Going to sleep for $sleep_second seconds" >> log.log
    sleep $sleep_second 
    echo "Finished sleeping" >> log.log
    ((sleep_second_counter+=$sleep_second))
    echo "Finished counter" >> log.log
done
echo "Out" >> log.log
nc -vz $CONNECT_REST_ADVERTISED_HOST_NAME $CONNECT_REST_PORT

/bin/bash

Entry point gets called correctly but CMD does not get invoked.

I also try to understand the solution given here https://stackoverflow.com/questions/54447913/cmd-doesnt-run-after-entrypoint-in-dockerfile

but I did not understand the solution.

If someone could explain a bit more what is wrong here.

What I am trying to accomplish

I am trying to have a single docker container image which will start the kafka-connect server (ENTRYPOINT) and then via bash file (CMD) I will configure the plugins. Requirement is that the same sequence of steps gets executed everytime the containers restarts.

答案1

得分: 1

CMDENTRYPOINT 之后运行,就像函数调用后的参数一样,在同一条命令行上。

在你的情况下,你想要按顺序运行两个不同的命令。那么,你可以将它们添加到一个名为 startup_script.sh 的文件中,其内容如下:

#!/bin/bash

./etc/confluent/docker/run & # 后台运行,以免在此处卡住

/usr/share/kafka-connect-script/plugins-config.sh # 应用配置

sleep 100000000 # 为了避免启动脚本退出,因为那会导致容器被终止
英文:

CMD is run after ENTRYPOINT, like parameters after a function invokation, in the same command line.

In your case you want two different commands running sequentially. Then, you may add them to a startup_script.sh whose content is:

#!/bin/bash

./etc/confluent/docker/run & # run in background not to get stuck in here

/usr/share/kafka-connect-script/plugins-config.sh # apply configuration

sleep 100000000 # to avoid the startup script to exit since that would kill the container

huangapple
  • 本文由 发表于 2020年1月3日 18:56:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577310.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定