I have to execute a shell script log-agent.sh inside my docker container.
Dockerfile
FROM openjdk:11-jre
LABEL org.opencontainers.image.authors="[email protected]"
# Install AWS CLI
RUN apt-get update && \
apt-get install -y awscli && \
apt-get clean
VOLUME /tmp
ARG JAR_FILE
ARG PROFILE
ADD ${JAR_FILE} app.jar
ENV PROFILE_ENV=${PROFILE}
EXPOSE 8080
COPY entrypoint.sh /
COPY log-agent.sh /
# Set permissions for log-agent.sh
RUN chmod +x /log-agent.sh
# Use entrypoint.sh as the entry point
ENTRYPOINT ["/entrypoint.sh"]
# Execute log-agent.sh
# RUN /bin/bash -c '/logs/log-agent.sh'
CMD ["/bin/bash", "-c", "/log-agent.sh"]
entrypoint.sh
#!/bin/bash
# Run your script
/bin/bash /log-agent.sh
# Checking for lead detection level
if [ -z "$LEAK_DETECTION_LEVEL" ]
then
LEAK_DETECTION_LEVEL=advanced
fi
# Start the Spring Boot application
java -Dspring.profiles.active=${PROFILE_ENV} -XX:+UseG1GC -Dio.netty.leakDetection.level=${LEAK_DETECTION_LEVEL} -Djava.security.egd=file:/dev/./urandom -Dloader.main=com.adtech.DemoApplication -jar app.jar
The application startup is successful, but the container is not executing the script. No errors in the logs as well. Here is what I have already verified:
- File location.
- File permissions.
- Validated the shell script for correctness. (proper shebang operator)
- Script is executing correctly if executed manually from the container using the docker exec command
Any suggestions?
Why is not running?
Basically, is not usual to use CMD and ENTRYPOINT together. If you do that, the cmd instructions are appended to the entrypoint as simple arguments. In your case something like this:
More details here
Explanation
In docker, only one foreground process can run. Several processes in just one container is not a good practice.
Docker official web says :
Also to have several processes inside of one container is called: Fat container
Anyway you have these alternatives:
#1 Supervisor
With supervisor, you can run several process in one container. For example: postgress + java. In your case, the supervisor config file could be like this
More details here:
How to package several services in one docker image?
#2 Send agent to background
Something like this
ENTRYPOINT ["/entrypoint.sh"]Notes: