简体   繁体   English

使用 docker 运行的 ts-node 应用程序运行但不是 docker 撰写

[英]ts-node app running using docker run but not docker compose

Using this Dockerfile:使用这个 Dockerfile:

FROM node:14-alpine
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
EXPOSE 3000
CMD ["npm", "start"]
docker build -t ts-node-api .    
docker run -it --entrypoint sh --expose 3000 -it ts-node-api

or或者

docker run --expose 3000 -it steve/ts-node-api     

works correctly, apart from not connected to the db, which is a later problem.工作正常,除了没有连接到数据库,这是后来的问题。

However, run the same Dockerfile as such:但是,运行相同的 Dockerfile 如下:

version: "3"
services:
  mongo:
    image: mongo
    volumes:
      - ./data:/data/db
    ports:
      - "27017:27017"
  ts-node-api:
    depends_on:
      - mongo
    build:
      context: .
      dockerfile: services/ts-node-api/Dockerfile
    ports: 
      - 3000:3000

using docker compose results in:使用 docker 组合结果:

 => ERROR [6/6] RUN npm run build                                                                                                               0.3s
------                                                                                                                                               
 > [6/6] RUN npm run build:
#9 0.244 npm ERR! code ENOENT
#9 0.244 npm ERR! syscall open
#9 0.244 npm ERR! path /usr/src/app/package.json
#9 0.245 npm ERR! errno -2
#9 0.246 npm ERR! enoent ENOENT: no such file or directory, open '/usr/src/app/package.json'
#9 0.246 npm ERR! enoent This is related to npm not being able to find a file.
#9 0.246 npm ERR! enoent 
#9 0.248 
#9 0.249 npm ERR! A complete log of this run can be found in:
#9 0.249 npm ERR!     /root/.npm/_logs/2022-08-26T18_29_26_477Z-debug.log

Any help would be greatly appreciated, thank you.任何帮助将不胜感激,谢谢。

Your two build setups are different.您的两个构建设置不同。 The docker build command you show is equivalent to您显示的docker build命令等效于

build:
  context: .
  # dockerfile: Dockerfile

which has a shorthand有一个速记

build: .

If the Compose file is in an ancestor directory of the service, so you cd into that subdirectory and then run the docker build.如果 Compose 文件位于服务的祖先目录中,则cd进入该子目录,然后运行docker build. command, you need to use that subdirectory name as the context directory命令,您需要使用该子目录名称作为上下文目录

build: services/ts-node-api

The Compose block you show is equivalent to您显示的 Compose 块相当于

docker build -f services/ts-node-api/Dockerfile .

which causes COPY commands to be interpreted relative to the root of your tree, not the directory containing your application (compare the two context: directories), which is why it doesn't work as expected.这会导致COPY命令相对于树的根进行解释,而不是相对于包含应用程序的目录(比较两个context:目录),这就是它无法按预期工作的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM