使用PHP静态Web服务器无法将端口从容器暴露给主机

时间:2018-02-01 01:10:59

标签: php docker dockerfile

我在Dockerfile中有这个多阶段构建:

## Stage 1
FROM node:9 as builder

RUN apt-get update && apt-get -y install sudo
RUN echo "newuser ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers

RUN useradd -ms /bin/bash newuser
USER newuser
RUN mkdir -p /home/newuser/app
WORKDIR /home/newuser/app

RUN sudo chown -R $(whoami) $(npm config get prefix)/lib
RUN npm set progress=false
RUN npm config set depth 0
RUN npm cache clean --force

COPY dist .

## Stage 2
FROM php:5.6.30

RUN apt-get update && apt-get -y install sudo
RUN echo "newuser ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers

RUN useradd -ms /bin/bash newuser
USER newuser
RUN mkdir -p /home/newuser/app
WORKDIR /home/newuser/app

COPY --from=builder /home/newuser/app /home/newuser/app

RUN ls -a /home/newuser/app

CMD ["php", "-S", "localhost:3000"]

图像构建成功,使用:

  docker build -t x .

然后我用:

运行它
   docker run -p 3000:3000 x

但是当我在主机上访问localhost:3000时,我没有得到回复。网页是空白的。

有谁知道为什么会这样?

我也尝试过:

CMD ["sudo", "php", "-S", "localhost:80"]

  docker run -p 3000:80 x

以及其他一些变化,仍然没有。

1 个答案:

答案 0 :(得分:0)

不确定为什么PHP静态服务器不起作用,但我使用Node.js静态服务器包:

FROM node:9 as builder

RUN apt-get update && \
      apt-get -y install sudo

RUN echo "newuser ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers

RUN useradd -ms /bin/bash newuser
USER newuser
RUN mkdir -p /home/newuser/app
WORKDIR /home/newuser/app

RUN sudo chown -R $(whoami) $(npm config get prefix)/lib
RUN sudo chown -R $(whoami) $(npm config get prefix)/lib/node_modules
RUN sudo chown -R $(whoami) $(npm config get prefix)/bin
RUN sudo chown -R $(whoami) $(npm config get prefix)/share
RUN sudo chown -R $(whoami) /usr/local/lib

RUN npm set progress=false
RUN npm config set depth 0
RUN npm cache clean --force

COPY dist .

RUN npm install -g http-server

ENTRYPOINT ["http-server", "."]

你是这样构建的:

docker build -t x .

然后像这样运行它:

docker run -p 3000:8080 x