首页 > 解决方案 > 如何修复 docker-compose 中的 FileNotFoundError

问题描述

我正在学习 docker 课程,但示例后端节点应用程序的 docker build 似乎存在问题。

前端dockerfile:

FROM node:14.16.0-alpine3.13
RUN addgroup app && adduser -S -G app app
USER app
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000 
CMD ["npm", "start"]

后端dockerfile:

FROM node:14.16.0-alpine3.13
RUN addgroup app && adduser -S -G app app
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . . 
EXPOSE 3001 
USER app
CMD ["npm", "start"]

码头工人-compose.yml

version: "3.8"

services:
  frontend:
    depends_on: 
      - backend
    build: ./frontend
    ports:
      - 3000:3000

  backend: 
    depends_on: 
      - db
    build: ./backend
    ports: 
      - 3001:3001
    environment: 
      DB_URL: mongodb://db/vidly
    command: ./docker-entrypoint.sh

  db:
    image: mongo:4.0-xenial
    ports:
      - 27017:27017
    volumes:
      - vidly:/data/db

volumes:
  vidly:

追溯

Building backend
Sending build context to Docker daemon  291.3kB
Step 1/9 : FROM node:14.16.0-alpine3.13
14.16.0-alpine3.13: Pulling from library/node
ca3cd42a7c95: Pull complete 
cf8dc363e30f: Pull complete 
074ae49463c4: Pull complete 
48fd78e4b532: Pull complete 
Digest: sha256:2c51dc462a02f15621e7486774d36d048a27225d581374b002b8477a79a59b4b
Status: Downloaded newer image for node:14.16.0-alpine3.13
 ---> 50bfd284aa0d
Step 2/9 : RUN addgroup app && adduser -S -G app app
 ---> Running in 4183bada01d2
Removing intermediate container 4183bada01d2
 ---> 0c55d313489a
Step 3/9 : WORKDIR /app
 ---> Running in e6da0a502c0c
Removing intermediate container e6da0a502c0c
 ---> c62eee1e61e1
Step 4/9 : COPY package*.json ./
 ---> caad8596ddd4
Step 5/9 : RUN npm install
 ---> Running in 5d2f331053db

> nodemon@2.0.7 postinstall /app/node_modules/nodemon
> node bin/postinstall || exit 0

Love nodemon? You can now support the project via the open collective:
 > https://opencollective.com/nodemon/donate

npm WARN vidly-backend@1.0.0 No description
npm WARN vidly-backend@1.0.0 No repository field.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.3.1 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.3.1: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

added 770 packages from 462 contributors and audited 771 packages in 10.856s

35 packages are looking for funding
  run `npm fund` for details

found 90 vulnerabilities (61 moderate, 28 high, 1 critical)
  run `npm audit fix` to fix them, or `npm audit` for details
Removing intermediate container 5d2f331053db
 ---> 89123dc063c8
Step 6/9 : COPY . .
 ---> 74c3d17d99a8
Step 7/9 : EXPOSE 3001
 ---> Running in db775adf83f0
Removing intermediate container db775adf83f0
 ---> 82e0d9220d54
Step 8/9 : USER app
 ---> Running in 95d1419578ac
Removing intermediate container 95d1419578ac
 ---> ffb4ca92826e
Step 9/9 : CMD ["npm", "start"]
 ---> Running in d1b88a01f889
Removing intermediate container d1b88a01f889
 ---> 418d04e16184
Successfully built 418d04e16184
Successfully tagged vidly_backend:latest
Traceback (most recent call last):
  File "docker-compose", line 3, in <module>
  File "compose/cli/main.py", line 81, in main
  File "compose/cli/main.py", line 203, in perform_command
  File "compose/metrics/decorator.py", line 18, in wrapper
  File "compose/cli/main.py", line 1186, in up
  File "compose/cli/main.py", line 1182, in up
  File "compose/project.py", line 664, in up
  File "compose/service.py", line 364, in ensure_image_exists
  File "compose/service.py", line 1133, in build
  File "compose/service.py", line 1948, in build
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/tmpafoiltwd'
[39279] Failed to execute script docker-compose

我在想/tmp/tmpafoiltwd可能在主机上(?)并且存在权限冲突(尽管它不是权限错误)。我对下一步要采取的措施感到困惑。任何方向将不胜感激:)

编辑:如果我移到USER app第 3 行,我会收到权限错误:

FROM node:14.16.0-alpine3.13
RUN addgroup app && adduser -S -G app app
USER app
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . . 
EXPOSE 3001 
CMD ["npm", "start"]

追溯

npm WARN checkPermissions Missing write access to /app
npm WARN vidly-backend@1.0.0 No description
npm WARN vidly-backend@1.0.0 No repository field.

npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /app
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, access '/app'
npm ERR!  [Error: EACCES: permission denied, access '/app'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/app'
npm ERR! }

标签: node.jsdockernpmdocker-compose

解决方案


推荐阅读