Firstly, this question is not a duplicate of the question here. I am currently learning docker, and am required to create a dockerfile which installs tomcat and runs it at the start of a container with the entrypoint command:
Following are the contents of my dockerfile:
FROM ubuntu:latest LABEL MAINTAINER="kesarling" RUN apt update RUN apt upgrade -y RUN apt install apt-utils -y RUN apt install maven gradle -y RUN apt install wget tar zip unzip openjdk-14-jdk -y RUN mkdir /usr/local/apache-tomcat-v8.5.55 RUN wget http://www-us.apache.org/dist/tomcat/tomcat-8/v8.5.55/bin/apache-tomcat-8.5.55.tar.gz -O /tmp/apache-tomcat-v8.5.55.tar.gz RUN cd /tmp && tar xvfz apache-tomcat-v8.5.55.tar.gz RUN cp -Rv /tmp/apache-tomcat-8.5.55/* /usr/local/apache-tomcat-v8.5.55/ RUN mkdir /usr/local/spring-v5.2.6 RUN wget https://repo.spring.io/release/org/springframework/spring/5.2.6.RELEASE/spring-5.2.6.RELEASE-dist.zip -O /tmp/spring-v5.2.6.zip RUN cd /tmp && unzip spring-v5.2.6.zip RUN cp -Rv /tmp/spring-framework-5.2.6.RELEASE/* /usr/local/spring-v5.2.6/ RUN cd ~ RUN touch server-start.sh RUN echo "#!/bin/sh" | cat > server-start.sh # start of condition RUN echo 'bash -c "/usr/local/apache-tomcat-v8.5.55/bin/catalina.sh run&"' | cat >> server-start.sh RUN echo "/bin/bash" | cat >>server-start.sh # end of condition RUN chmod +x server-start.sh EXPOSE 8080 ENTRYPOINT [ "./server-start.sh" ] As the server starts as a background process, it runs fine only when I give the -it flag. Else, as expected, the container quits the moment I detach it.
So, in a nutshell:
docker container run -it -p 8080:8080 <mydockername> Works but:
docker container run -dp 8080:8080 <mydockername> Does not (as expected). That is where the condition comes in. In a nutshell, I need it to detect if the docker has been run in background or in interactive mode.
The condition block mentioned in the dockerfile needs to be as follows:
If I am executing it with the -it flag:
RUN echo 'bash -c "/usr/local/apache-tomcat-v8.5.55/bin/catalina.sh run&"' | cat >> server-start.sh RUN echo "/bin/bash" | cat >>server-start.sh Else if I am executing with the -d flag:
RUN echo 'bash -c "/usr/local/apache-tomcat-v8.5.55/bin/catalina.sh run"' | cat >> server-start.sh (Notice that the /bin/bash has been removed)
How do I go about this?
P.S. This is my first week into docker, so please bear with my dockerfile :P
Oh, and not to mention that searching detect if the container has been run with an interactive flag doesn't bring up anything useful on Google