【问题标题】:'docker run ...' hangs at execuring sh script'docker run ...' 在执行 sh 脚本时挂起
【发布时间】:2021-05-17 11:42:53
【问题描述】:

所以我有这个 Dockerfile,它构建了一个 React 应用程序,然后提供它。它工作得很好。但是,只要我添加要在 CMD 中执行的 shell 脚本,图像就会构建,但在运行时会无限期挂起。

我遵循了这个教程:adding env variables to nginx docker

# Stage 0, build-stage, based on Node.js to build the frontend
FROM node:alpine as build
MAINTAINER Kuba Wasilewski <jakub.wasilewski@sprint.pl>
WORKDIR /app
COPY package*.json /app/
RUN apk add --update python make g++\
   && rm -rf "/var/cache/apk/*"
RUN npm install
COPY . /app/
RUN npm run build

# Stage 1, run-phase, based on NGINX to provide SSL configuration and serve static files
FROM nginx:alpine
MAINTAINER Kuba Wasilewski <jakub.wasilewski@sprint.pl>
COPY --from=build /app/build /usr/share/nginx/html
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx/default.conf /etc/nginx/conf.d
COPY nginx/server.cert /etc/nginx
COPY nginx/server.key /etc/nginx
EXPOSE 443

# working CMD
# CMD ["nginx", "-g", "daemon off;"]

# changes Ive applied
WORKDIR /usr/share/nginx/html
COPY nginx/env.sh .
COPY nginx/.env .
RUN apk add --no-cache bash
RUN chmod +x env.sh
CMD ["/bin/bash", "-c", "/usr/share/nginx/html/env.sh && nginx -g \"daemon off;\""]

【问题讨论】:

  • 唯一看起来有点不对劲的是第一张图片中的 RUN。为什么要安装 python 和 make 之类的东西?在下一张图片中,这将不再可用,并且您没有使用它,也不需要它。
  • 没有 Python 我无法运行 npm install 出于某种原因.. 另外,我知道这不是脚本本身,因为我尝试使用 'echo "hello"' 脚本,结果相同...
  • 您也是先安装 bash。您可以在交互模式下运行容器并确认您可以毫无问题地运行 bash 命令吗?
  • 我认为你可以通过更新 apk 逃脱。这应该允许您安装节点。恰好是你沿着你的 python 运行它并进行安装。但这不是你的问题。它的其他东西略有不同,但应该仍然有效。通常,您必须在新服务器和 docker 映像上运行 apt update 或 apk 或其他什么来获取可用的软件包列表。
  • @TheFool 我的意思是测试'hello'脚本运行得很好,但仍然挂起......

标签: reactjs docker nginx dockerfile


【解决方案1】:

如果其他人遇到这个问题,实际解决方案是COPY build/myscript.sh /docker-entrypoint.d/myscript.sh,并在Dockerfile中添加RUN chmod +x /docker-entrypoint.d/myscript.sh

你可以在最后留下CMD [ "nginx", "-g", "daemon off;"]

这将起作用,因为 nginx:alpine 将在启动 nginx 之前运行 /docker-entrypoint.d/ 中的脚本。如果您尝试在 nginx 内部使用 sh 或 bash,那么它将挂起。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-07
    • 2021-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-27
    • 1970-01-01
    相关资源
    最近更新 更多