如何使用Dockerfile在启动docker容器上运行jboss-cli

时间:2019-05-09 13:50:42

标签: docker jboss containers dockerfile wildfly

我正在尝试使用Dockerfile创建映像docker容器。当Docker容器启动时,我需要使用jboss-cli执行一些命令。要执行jboss-cli,必须运行wildfly服务。

我的Dockerfile:

FROM jboss/wildfly:latest

USER jboss

RUN /opt/jboss/wildfly/bin/add-user.sh admin admin --silent

CMD ["/opt/jboss/wildfly/bin/standalone.sh", "-b", "0.0.0.0", "-bmanagement", "0.0.0.0"]

然后我尝试在Dockerfile中添加一个jboss-cli命令

FROM jboss/wildfly:latest

USER jboss

COPY mysql-connector-java-5.1.44.jar /opt/jboss/

RUN /opt/jboss/wildfly/bin/add-user.sh admin admin --silent

USER root
#in this line is needed to change owner file to jboss user use this file
RUN chown jboss.root /opt/jboss/mysql-connector-java-5.1.44.jar
USER jboss

CMD ["/opt/jboss/wildfly/bin/standalone.sh", "-b", "0.0.0.0", "-bmanagement", "0.0.0.0"]
CMD ["/opt/jboss/wildfly/bin/jboss-cli.sh", "-c", "controller=localhost:9990", "--user=admin", "--password=admin", "--command=\"module add --name=com.mysql --resources=/opt/jboss/mysql-connector-java-5.1.44.jar --dependencies=javax.api,javax.transaction.api\""]

当我运行docker run命令时:

docker run --name=wildfly-ci -p 8080:8080 -p 9990:9990 wildfly-ci

运行docker命令时,我从jboss-cli收到此消息错误

Failed to connect to the controller: The controller is not available at localhost:9990: java.net.ConnectException: WFLYPRT0053: Could not connect to remote+http://localhost:9990. The connection failed: WFLYPRT0053: Could not connect to remote+http://localhost:9990. The connection failed: XNIO000812: Connection closed unexpectedly

我不知道是因为这时wildfly的服务尚未运行,还是因为Docker Container的某些行为。

2 个答案:

答案 0 :(得分:0)

使用单个CMD尝试此Dockerfile,

Dockerfile

从jboss / wildfly:latest

USER jboss

复制mysql-connector-java-5.1.44.jar / opt / jboss /

运行/opt/jboss/wildfly/bin/add-user.sh admin admin --silent

USER root 运行chown jboss.root /opt/jboss/mysql-connector-java-5.1.44.jar 用户jboss

CMD [“ /opt/jboss/wildfly/bin/standalone.sh"、"-b"、"0.0.0.0"、"-bmanagement"、"0.0.0.0"、";"、"/opt/ jboss / wildfly / bin / jboss-cli.sh“,”-c“,” controller = localhost:9990“,”-user = admin“,”-password = admin“,”-command = \“模块添加--name = com.mysql --resources = / opt / jboss / mysql-connector-java-5.1.44.jar --dependencies = javax.api,javax.transaction.api \“”]

答案 1 :(得分:0)

这对我有用:

   RUN /bin/sh -c '$JBOSS_HOME/bin/standalone.sh -c=standalone-full.xml &' && \
      sleep 10 && \
      cd /tmp && \
      $JBOSS_HOME/bin/jboss-cli.sh --connect --command="module add --name=org.postgresql --resources=$JBOSS_HOME/standalone/configuration/postgresql-42.2.5.jar --dependencies=javax.api,javax.transaction.api,javax.servlet.api" && \
      $JBOSS_HOME/bin/jboss-cli.sh --connect --command=:shutdown 


    # User root to modify war owners
    USER root

    CMD ["/opt/eap/bin/standalone.sh", "-c", "standalone-full.xml", "-b", "0.0.0.0","-bmanagement","0.0.0.0"]