SoFunction
Updated on 2025-03-09

Docker run the solution to automatically end the container

Today I encountered the problem of creating a mirror with Dockerfile, and the container automatically ends after the image is run.

Start the command:

docker run -d -p 8080:8080 -v /usr/local/tomcat7.0/logs:/usr/local/tomcat7.0/logs --name tomcatweb tomcat:7.0

After running, use docker ps to find that the docker container has ended

After searching for information, I found that this problem is not complicated. The reason is that: when the Docker container is running in the background, there must be a foreground process.

Solution:

1. Distribute the running process into the foreground to start, such as:nginx nginx -g "daemon off;" tomcat ./ run

2. Use tail and top, a program that can be run on the foreground. It is especially recommended to output your log file.

Add ENTRYPOINT /opt/tomcat7.0/bin/ && in Dockerfile tail -F /opt/tomcat7.0/logs/

Supplementary knowledge:The tomcat pulled by docker pull did not generate a log, so I handwritten a tocmat dockerfile file, and the actual measurements were generated.

1. The dockfile file and explanation are as follows

FROM openjdk:8-jre 
MAINTAINER zyj
 
ENV JAVA_HOME /docker-java-home
ENV CATALINA_HOME /opt/tomcat 
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin:$CATALINA_HOME/scripts
 
#Time ZoneRUN echo "Asia/Shanghai" > /etc/timezone 
RUN mv /etc/localtime /etc/localtime_bak 
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
 
#TOMCAT 
ENV TOMCAT_MAJOR 8
ENV TOMCAT_VERSION 8.5.35
 
RUN wget /apache/tomcat/tomcat-8/v8.5.41/bin/apache-tomcat-8.5. && \
 tar -zxvf apache-tomcat-8.5. && \
 rm apache-tomcat*. && \
 mv apache-tomcat* ${CATALINA_HOME} 
 
RUN chmod +x ${CATALINA_HOME}/bin/*sh 
RUN chmod 777 ${CATALINA_HOME}/logs/ 
RUN chmod 777 ${CATALINA_HOME}/webapps/
 
#Set username and password adminADD  /opt/tomcat/conf/
 
#Remote accessADD  /opt/tomcat/webapps/manager/META-INF/
 
ENV LANG zh_CN.UTF-8
 
#Open portEXPOSE 8080
 
# Launch Tomcat 
WORKDIR /opt/tomcat/bin
CMD ["","run"]

Note: This docker requires reference to external files

See detailsgithub

2. Dockerfile compilation command

docker build -f dockerfile -t zyj/tomcat .

3. Run the command

docker run -d -p 8080:8080 --name tomcat8
-v /opt/docker-tomcat/logs/:/opt/tomcat/logs/ -v /opt/docker-tomcat/webapps/:/opt/tomcat/webapps/ -v /opt/docker-tomcat/:/opt/tomcat/webapps/manager/META-INF/ -v /opt/docker-tomcat/:/opt/tomcat/conf/ --privileged=true zyj/tomcat

The above solution to the automatic end of the docker run container is all the content I share with you. I hope you can give you a reference and I hope you can support me more.