Solution of Automatic End of docker run Running Container

  • 2021-09-12 02:38:12
  • OfStack

Today, I encountered the problem of creating an image with Dockerfile, and the container automatically ends after the image runs.

Start 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 looking up the data, it is found that this problem is not complicated. The reason is that Docker container runs in the background, so there must be a foreground process.

Solution:

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

2. Use tail, top this can run foreground program, especially recommended tail, output your log file.

Add ENTRYPOINT/opt/tomcat7.0/bin/startup. sh to Dockerfile & & tail -F /opt/tomcat7.0/logs/catalina.out

Supplementary knowledge: tomcat pulled by docker pull did not generate a log, so I wrote an tocmat dockerfile file by hand, and actually measured a log generation

1. The dockfile document and its 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 zone  
RUN 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 http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.41/bin/apache-tomcat-8.5.41.tar.gz && \
 tar -zxvf apache-tomcat-8.5.41.tar.gz && \
 rm apache-tomcat*.tar.gz && \
 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  admin 
ADD tomcat-users.xml /opt/tomcat/conf/
 
# Remote access  
ADD context.xml /opt/tomcat/webapps/manager/META-INF/
 
ENV LANG zh_CN.UTF-8
 
# Open port  
EXPOSE 8080
 
# Launch Tomcat 
WORKDIR /opt/tomcat/bin
CMD ["catalina.sh","run"]

Note: This docker needs to reference external files

See github for details

2. dockerfile compile 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/context.xml:/opt/tomcat/webapps/manager/META-INF/context.xml -v /opt/docker-tomcat/tomcat-users.xml:/opt/tomcat/conf/tomcat-users.xml --privileged=true zyj/tomcat

Related articles: