使用docker compose运行Gatsby

9
我正在尝试使用Docker Compose运行Gatsby。 据我了解,Gatsby站点正在我的Docker容器中运行。
我将容器的端口8000映射到本地主机的端口8000。 但是当查看localhost:8000时,我没有看到我的Gatsby站点。
我使用以下Dockerfile使用docker build -t nxtra/gatsby .构建镜像:
FROM node:8.12.0-alpine

WORKDIR /project

COPY ./package.json /project/package.json
COPY ./.entrypoint/entrypoint.sh /entrypoint.sh

RUN apk update \
  && apk add bash \
  && chmod +x /entrypoint.sh \
  && npm set progress=false \
  && npm install -g yarn gatsby-cli

EXPOSE 8000

ENTRYPOINT [ "/entrypoint.sh" ]

entrypoints.sh 包含以下内容:

#!/bin/bash

yarn install
gatsby develop

docker-compose up 运行 docker-compose.yml 文件。

version: '3.7'

services:
  gatsby:
    image: nxtra/gatsby
    ports:
    - "8000:8000"
    volumes:
    - ./:/project
    tty: true

docker ps 显示端口8000被转发到 0.0.0.0:8000->8000/tcp
使用docker inspect --format='{{.Config.ExposedPorts}}' id检查容器,确认该端口已被暴露 -> map[8000/tcp:{}]

docker tops 在容器中显示以下进程正在运行:

18465               root                0:00                {entrypoint.sh} /bin/bash /entrypoint.sh
18586               root                0:11                node /usr/local/bin/gatsby develop
18605               root                0:00                /usr/local/bin/node /project/node_modules/jest-worker/build/child.js
18637               root                0:00                /bin/bash

Dockerfile 和 docker-compose.yml 文件位于我的 Gatsby 项目根目录下。 当我不使用 Docker 运行该项目时,它可以正常运行 gatsby develop

我在哪里出错了,导致我无法通过 localhost:8000 访问在容器中运行的 Gatsby 网站?

1个回答

17
我的问题是 Gatsby 仅在容器内部监听请求,就像这个答案所建议的。确保您已经将 Gatsby 配置为主机 0.0.0.0。以这个(有点巧妙的)设置为例:
FROM node:alpine
RUN npm install --global gatsby-cli

docker-compose.yml

version: "3.7"

services:
  gatsby:
    build:
      context: .
      dockerfile: Dockerfile
    entrypoint: gatsby
    volumes:
      - .:/app

  develop:
    build:
      context: .
      dockerfile: Dockerfile
    command: gatsby develop -H 0.0.0.0
    ports:
      - "8000:8000"
    volumes:
      - .:/app

你可以在容器中运行Gatsby命令: docker-compose run gatsby info

或者运行开发服务器: docker-compose up develop


2
gatsby develop -H 0.0.0.0 做到了,谢谢! - Tjen Wellens

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接