首页 > 解决方案 > 在 docker-compose 中结合 Next.JS 和 json-server

问题描述

我正在尝试创建一个 docker-compose 来启动带有json-server后端的 Next.js 应用程序的演示。

当我从一个页面移动到另一个页面时,什么都没有移动(使用 nextjs 链接),直到我从后端获得超时,而后端的日志中没有显示任何请求。之后,如果我重新加载页面,它会立即呈现,并且请求会显示在后端的日志中。此外,客户端请求根本不起作用。

直接访问 fakeapi (http://localhost:3521/) 确实没有问题。

Docker 撰写日志

app_1      | event - build page: /
app_1      | wait  - compiling...
app_1      | event - compiled successfully
app_1      | event - build page: /next/dist/pages/_error
>>>> PAGE RELOAD
app_1      | wait  - compiling...
app_1      | event - compiled successfully
fakeapi_1  | GET /products?_page=1&_limit=50 200 10.187 ms - 83189

码头工人-compose.yml

version: "3"

services:
  app:
    image: client/app
    depends_on:
      - fakeapi
    ports:
      - "3000:3000"
    environment:
      - NEXT_PUBLIC_INTERNAL_API_URI=http://fakeapi:3521
    networks:
      - mynetwork
  fakeapi:
    image: server/fakeapi
    restart: always
    ports:
      - "3521:3521"
    hostname:
      fakeapi
    networks:
      - mynetwork

networks:
  mynetwork:

json-server package.json

{
  "name": "fake-api",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "json-server --watch db.json --port 3521 --host 0.0.0.0"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "json-server": "^0.16.3"
  }
}

编辑:尝试为生产构建:页面不再“卡住”,但在我重新加载之前出现“发生意外错误”。客户端请求仍然无法正常工作。

标签: dockerdocker-composenext.jsjson-server

解决方案


推荐阅读