简体   繁体   中英

ts-node app running using docker run but not docker compose

Using this 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:

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:

 => 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

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. 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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