我正在使用 dockerfile 进行构建和映像,它构建并 运行 成功,但 tomcat 未启动

I have working with dockerfile to build and image and it build and run successfully but tomcat is not up

我正在使用 Docker 文件构建映像。
Docker 文件的内容:

FROM ubuntu
# Update Ubuntu
RUN apt-get update && apt-get -y upgrade
# Add oracle java 7 repository
RUN apt-get -y install software-properties-common
RUN add-apt-repository ppa:webupd8team/java
RUN apt-get -y update
# Accept the Oracle Java license
RUN echo "oracle-java7-installer shared/accepted-oracle-license-v1-1 boolean true" | debconf-set-selections
# Install Oracle Java
RUN apt-get -y install oracle-java7-installer
# Install tomcat
RUN apt-get -y install tomcat7
RUN echo "JAVA_HOME=/usr/lib/jvm/java-7-oracle" >> /etc/default/tomcat7
EXPOSE 8080
# Download Slashdot homepage
RUN mkdir /var/lib/tomcat7/webapps/slashdot
RUN wget http://www.slashdot.org -P /var/lib/tomcat7/webapps/slashdot
# Start Tomcat, after starting Tomcat the container will stop. So use a 'trick' to keep it running.
CMD service tomcat7 start && tail -f /var/lib/tomcat7/logs/catalina.out

当我尝试使用命令docker build -t sample .构建镜像时,镜像构建成功。
当我尝试使用 运行 命令时

docker run -it --rm -p 8080:8080 sample

显示:Starting Tomcat servlet engine tomcat7

但是当我尝试打开 localhost:8080 时,它显示 webpage is not available

请提出为什么这不起作用。

由于您处于 boot2docker 环境中,这意味着端口 8080 映射到 boot2docker VM(Linux 主机)中的 8080。不在您的 PC 中(Windows 实际主机)。

您还需要在您的 Virtualbox 中打开该端口,以便从您的 windows 主机可见该端口,并让您的浏览器访问 localhost:8080.

有关更多信息,请参阅 Boot2Docker: can't get ports forwarding to work
(确保 c:\path\to\VirtualBox 在你的 PATH 中)

you can set up a permanent VirtualBox NAT Port forwarding:

VBoxManage modifyvm "boot2docker-vm" --natpf1 "tcp-port8080,tcp,,8080,,8080";

If the vm is already running, you should run this other command:

VBoxManage controlvm "boot2docker-vm" natpf1 "tcp-port8080,tcp,,8080,,8080";