通过 Docker 在生产环境中运行 AdonisJS 时出现路径错误
我从 require-ts 中收到路径错误,但我不知道如何确定我缺少什么。该应用程序在本地使用 node aceserve
运行良好,但不能在生产服务器上的 docker 中运行。
这是错误:
docker exec -it adonis_app node ace list:routes
TypeError
The "path" argument must be of type string. Received undefined
1 Cache.makeCachePath
/home/node/app/node_modules/@adonisjs/require-ts/build/src/Cache/index.js:41
2 Config.getCached
/home/node/app/node_modules/@adonisjs/require-ts/build/src/Config/index.js:139
3 Config.parse
/home/node/app/node_modules/@adonisjs/require-ts/build/src/Config/index.js:156
4 register
/home/node/app/node_modules/@adonisjs/require-ts/build/index.js:82
5 Object.registerForAdonis [as default]
/home/node/app/node_modules/@adonisjs/assembler/build/src/requireHook/index.js:17
6 registerTsHook
/home/node/app/node_modules/@adonisjs/core/build/src/utils/index.js:26
7 App.onFind
/home/node/app/node_modules/@adonisjs/core/build/src/Ignitor/Ace/App/index.js:132
这是我的 dockerfile:
ARG NODE_IMAGE=node:16.13.1-alpine
FROM $NODE_IMAGE AS base
RUN apk --no-cache add dumb-init
RUN mkdir -p /home/node/app && chown node:node /home/node/app
WORKDIR /home/node/app
USER node
RUN mkdir tmp
FROM base AS dependencies
COPY --chown=node:node ./package*.json ./
COPY --chown=node:node ./tsconfig*.json ./
RUN npm ci
COPY --chown=node:node . .
FROM dependencies AS build
RUN node ace build --production --ignore-ts-errors
FROM base AS production
ENV NODE_ENV=production
ENV PORT=$PORT
ENV HOST=0.0.0.0
COPY --chown=node:node ./package*.json ./
RUN npm ci --production
COPY --chown=node:node --from=build /home/node/app/build .
EXPOSE $PORT
CMD [ "dumb-init", "node", "build/server.js" ]
和 docker-compose:
version: '3'
services:
adonis_app:
container_name: adonis_app
restart: always
build:
context: .
#target: dependencies
depends_on:
- postgres
ports:
- ${PORT}:${PORT}
- 9229:9229
env_file:
- .env
volumes:
- ./:/home/node/app
- uploads:/home/node/app/public/uploads
networks:
- adonis
#command: dumb-init node ace serve --watch --node-args="--inspect=0.0.0.0"
#command: dumb-init node build/server.js --node-args="--inspect=0.0.0.0"
postgres:
image: postgres:13.1
healthcheck:
test: [ "CMD", "pg_isready", "-q", "-d", "postgres", "-U", "root" ]
timeout: 45s
interval: 10s
retries: 10
restart: always
environment:
- POSTGRES_USER=${ROOT_DB_USER}
- POSTGRES_PASSWORD=${ROOT_DB_PASS}
- APP_DB_USER=${PG_USER}
- APP_DB_PASS=${PG_PASSWORD}
- APP_DB_NAME=${PG_DB_NAME}
volumes:
#- ./db:/docker-entrypoint-initdb.d/
- ./db:/var/lib/postgresql/data
ports:
- 54325:5432
networks:
- adonis
networks:
adonis:
driver: bridge
volumes:
db:
driver: local
uploads:
driver: local
在运行任何 ace 命令(例如迁移和播种)时,我收到相同的错误。我缺少什么?
I'm getting a path error from require-ts and I have no idea how to determine what I'm missing. The app works fine locally using node ace serve
but not within docker on a production server.
This is the error:
docker exec -it adonis_app node ace list:routes
TypeError
The "path" argument must be of type string. Received undefined
1 Cache.makeCachePath
/home/node/app/node_modules/@adonisjs/require-ts/build/src/Cache/index.js:41
2 Config.getCached
/home/node/app/node_modules/@adonisjs/require-ts/build/src/Config/index.js:139
3 Config.parse
/home/node/app/node_modules/@adonisjs/require-ts/build/src/Config/index.js:156
4 register
/home/node/app/node_modules/@adonisjs/require-ts/build/index.js:82
5 Object.registerForAdonis [as default]
/home/node/app/node_modules/@adonisjs/assembler/build/src/requireHook/index.js:17
6 registerTsHook
/home/node/app/node_modules/@adonisjs/core/build/src/utils/index.js:26
7 App.onFind
/home/node/app/node_modules/@adonisjs/core/build/src/Ignitor/Ace/App/index.js:132
This is my dockerfile:
ARG NODE_IMAGE=node:16.13.1-alpine
FROM $NODE_IMAGE AS base
RUN apk --no-cache add dumb-init
RUN mkdir -p /home/node/app && chown node:node /home/node/app
WORKDIR /home/node/app
USER node
RUN mkdir tmp
FROM base AS dependencies
COPY --chown=node:node ./package*.json ./
COPY --chown=node:node ./tsconfig*.json ./
RUN npm ci
COPY --chown=node:node . .
FROM dependencies AS build
RUN node ace build --production --ignore-ts-errors
FROM base AS production
ENV NODE_ENV=production
ENV PORT=$PORT
ENV HOST=0.0.0.0
COPY --chown=node:node ./package*.json ./
RUN npm ci --production
COPY --chown=node:node --from=build /home/node/app/build .
EXPOSE $PORT
CMD [ "dumb-init", "node", "build/server.js" ]
And docker-compose:
version: '3'
services:
adonis_app:
container_name: adonis_app
restart: always
build:
context: .
#target: dependencies
depends_on:
- postgres
ports:
- ${PORT}:${PORT}
- 9229:9229
env_file:
- .env
volumes:
- ./:/home/node/app
- uploads:/home/node/app/public/uploads
networks:
- adonis
#command: dumb-init node ace serve --watch --node-args="--inspect=0.0.0.0"
#command: dumb-init node build/server.js --node-args="--inspect=0.0.0.0"
postgres:
image: postgres:13.1
healthcheck:
test: [ "CMD", "pg_isready", "-q", "-d", "postgres", "-U", "root" ]
timeout: 45s
interval: 10s
retries: 10
restart: always
environment:
- POSTGRES_USER=${ROOT_DB_USER}
- POSTGRES_PASSWORD=${ROOT_DB_PASS}
- APP_DB_USER=${PG_USER}
- APP_DB_PASS=${PG_PASSWORD}
- APP_DB_NAME=${PG_DB_NAME}
volumes:
#- ./db:/docker-entrypoint-initdb.d/
- ./db:/var/lib/postgresql/data
ports:
- 54325:5432
networks:
- adonis
networks:
adonis:
driver: bridge
volumes:
db:
driver: local
uploads:
driver: local
I receive the same error when running any ace command, such as migration and seeding. What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在看了两个小时的 adonis 编译代码后,我终于有了一个解决方案。
我发现 adonis 使用一个名为
find-cache-dir
。而且它似乎没有找到任何缓存目录,因此它返回
undefined
(著名的那个)。我选择的解决方案是在环境变量中指定缓存目录。
例如:
如果您想在其他地方指定缓存目录,请查阅库文档。
编辑:如果库返回
未定义
,也可能是因为node_modules
目录不可写,在这种情况下使其可写或移动缓存目录。感谢您的阅读(这是我的第一个答案)。
I finally have a solution after watching the adonis compiled code for two hours.
I found out that adonis uses a library called
find-cache-dir
.And it seems that it doesn't find any cache directory, so it returns
undefined
(the famous one).The solution I opted for is to just specify the cache directory in the environment variables.
For example:
Consult the library documentation if you want to specify the cache directory elsewhere.
EDIT: If the library returns
undefined
it could also be because thenode_modules
directory is not writable, in this case make it writable or move the cache directory.Thanks for reading (this is my first answer).