Search code examples
dockerdockerfile

Shell script not starting java process in Docker container


I've got a Docker Ubuntu 24.04 image and am trying to run a Java process via a shell script. I plan to deploy this to Kubernetes, so I need the agent to run once the container starts. The Docker image builds fine, and I can start the process on my local Mac machine with this shell script and if I log into the container, I can start it there, but my agent will not start from my Dockerfile unless I manually log in and run the shell script.

FROM ubuntu:24.04

# Set environment variable
RUN apt-get update && \
    apt-get install -y unzip && \ 
    apt-get install -y tar && \
    rm -rf /var/lib/apt/lists/*

# Copy the Java tar file into the container
COPY jre-8u391-linux-aarch64.tar.gz /tmp/
COPY no-java.zip /tmp
ADD agent.config.properties /tmp/
RUN unzip /tmp/no-java.zip 

# Extract Java tar file
WORKDIR /no-java

RUN chmod +x install

RUN ./install -e -i /opt/agent -s http://localhost -p 8080 -l -q
RUN tar -xvf /tmp/jre-8u391-linux-aarch64.tar.gz -C /opt/java-agent/agent/bin/java
RUN mv /tmp/agent.config.properties /opt/java-agent/agent/bin/conf/
RUN rm -rf /tmp/no-java && rm -rf /no-java

CMD ["sh", "/opt/java-agent/agent/bin/run_agent.sh"]

The COMD does not seem to start the agent; when I log into the container and do a top, I can see it is not running. If manually run the run_agent.sh script while in the container, it will start the agent as expected:

 docker run -it agent /bin/sh
 sh /opt/java-agent/agent/bin/run_agent.sh

I've tried various iterations of CMD.

CMD ["/bin/sh","-c", "/opt/java-agent/agent/bin/run_agent.sh"]

But still doesn't work. How do I execute the script and start my Java process?


Solution

  • OK there were two issues:

    First I Had to give run permissions to the shell scripts within my bin directory. Second, I changed the working directory to the bin folder and it worked and executed the script within that directory and it worked.

    RUN find /opt/java-agent/agent/ -type f -name "*.sh" -exec chmod +x {} \;
    WORKDIR /opt/java-agent/agent/bin
    CMD ["sh","run_agent.sh"]