Apache Tomcat 8无法在docker容器中启动

我正在试验Docker并且对它很新.我很长一段时间都很震惊,并没有找到方法,因此在这里想出了这个问题……

问题陈述:
我试图从包含Apache和lynx安装的docker文件创建一个图像.一旦完成,我试图访问容器的8080上的tomcat,然后转发到主机的8082.但是在运行映像时,我从未在容器中启动tomcat.

Docker文件

FROM ubuntu:16.10
#Install Lynx
Run apt-get update
Run apt-get install -y lynx

#Install Curl
Run apt-get install -y curl

#Install tools: jdk
Run apt-get update
Run apt-get install -y openjdk-8-jdk wget

#Install apache tomcat
Run groupadd tomcat
Run useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
Run cd /tmp
Run curl -O http://apache.mirrors.ionfish.org/tomcat/tomcat-    8/v8.5.12/bin/apache-tomcat-8.5.12.tar.gz
Run mkdir /opt/tomcat
Run tar xzvf apache-tomcat-8*tar.gz -C /opt/tomcat --strip-components=1
Run cd /opt/tomcat
Run chgrp -R tomcat /opt/tomcat
Run chmod -R g+r /opt/tomcat/conf
Run chmod g+x /opt/tomcat/conf
Run chown -R tomcat /opt/tomcat/webapps /opt/tomcat/work /opt/tomcat/temp opt/tomcat/logs

Run cd /opt/tomcat/bin

Expose 8080
CMD /opt/tomcat/bin/catalina.sh run && tail -f /opt/tomcat/logs/catalina.out

构建映像时,我尝试通过以下两种方法运行容器

> docker run -d -p 8082:8080 imageid tail -f / dev / null
使用上述内容时,容器正在运行,但是tomcat未在容器内启动,因此无法从localhost:8082访问.如果我执行docker logs longcontainerid,我也看不到任何东西
> docker run -d -p 8082:8080 imageid /path/to/catalina.sh start tail -f / dev / null
当我做docker logs longconatainrid时,我看到tomcat启动了
使用上面的容器时,容器立即启动和停止,并且没有运行,因为我可以从docker ps看到,因此无法从localhost:8082访问.

谁能告诉我哪里出错了?

附:我在互联网上搜索了很多但是无法把事情弄好.可能有一些我不清楚的概念.

最佳答案 查看
docker run command documentation,doc指出传递给run的任何命令都将覆盖Dockerfile中的原始CMD:

As the operator (the person running a container from the image), you can override that CMD instruction just by specifying a new COMMAND

1 /然后当你跑:

docker run -d -p 8082:8080 imageid tail -f /dev/null

使用COMMAND tail -f / dev / null运行容器,重写启动tomcat的原始命令.

要解决您的问题,请尝试运行:

docker run -d -p 8082:8080 imageid

docker log -f containerId

查看tomcat是否正确启动.

2 /你不应该在catalina.sh中使用start参数.看看这个official tomcat Dokerfile,团队使用:

CMD ["catalina.sh", "run"]

启动tomcat(当你使用start时,docker在shell脚本的末尾结束容器并且tomcat将启动但不保持正在运行的进程).

3 /最后,为什么不使用tomcat官方映像来构建容器?你可以使用:

FROM tomcat:latest

在Dockerfile开头的指令,并将所需的元素(新文件,webapps war,设置)添加到docker镜像.

点赞