1

I have a Dockerfile as below:

#1st stage - wildfly production image
FROM wildfly-setup:17.0.0 AS wildfly-prod
USER jenkins
RUN mkdir /opt/wildfly/install && mkdir /opt/wildfly/install/config
COPY --chown=jenkins:jenkins init.sh /opt/wildfly/bin
RUN mkdir -p $JBOSS_HOME/standalone/data/datastorage
#Second stage - test run image
FROM wildfly-prod AS wildfly-sedi-test
USER jenkins
COPY --chown=jenkins:jenkins init.sh /opt/wildfly/bin
RUN /opt/wildfly/bin/init.sh
#CMD ["/opt/wildfly/bin/init.sh"]

And the bash script which I am running from the above Dockerfile is as below:

#!/bin/bash

if [ -e "$JBOSS_HOME/install/wildfly.sh" ] ; then
    $JBOSS_HOME/install/wildfly.sh
    rm $JBOSS_HOME/install/wildfly.sh
fi

# check for postgres running or not 
cnt=0
psql_terminate=2
while (( $cnt < 120 && $psql_terminate != 0 )); do
    postgres_isready -h $POSTGRES > /dev/null 2>&1

    if [ $? -eq 0 ] ; then
        let psql_terminate=0
        echo $psql_terminate
    fi

    let cnt=cnt+1
    sleep 1
done

if (( $psql_terminate == 0)) ; then
    exec $JBOSS_HOME/bin/standalone.sh -c standalone-full.xml
else
    echo "database unavailable."
    exit 1
fi

In the Dockerfile when I unable the CMD command it works, but with the RUN command it throws the below the error while building the image:

The command '/bin/sh -c /opt/wildfly/bin/init.sh' returned a non-zero code: 1

Can somebody please help me on this?

Thanks in advance.

2
  • 1
    use #!/bin/bash -x in the script and try to rebuild. it should provide more info about what breaks Commented Oct 31, 2019 at 14:45
  • And, you have an exit 1 with the message "database unavailable". Do you see that message appear? It seems likely that the something about db isn't active when you run this, or it can't be reached. Can't help with that however. Good luck. Commented Oct 31, 2019 at 15:05

1 Answer 1

1

RUN will execute your bash script when building the image.

The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile.

CMD will execute your bash script when starting the container.

The main purpose of a CMD is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT instruction as well.

So I'm assuming that your script reaches exit 1 because it is supposed to run when the container starts instead of when building your image.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.