没有这样的文件或目录:Docker-compose up

4
我使用docker-compose将我的mean应用程序容器化了。这很好用。 现在我尝试使用"volumes",使我的angular应用程序(使用ng serve)和我的express应用程序(使用nodemon.js)在编码时自动重启。 但是,无论是angular还是express容器都出现了相同的错误:
angular_1   |
angular_1   | up to date in 1.587s
angular_1   | found 0 vulnerabilities
angular_1   |
angular_1   | npm ERR! path /usr/src/app/package.json
angular_1   | npm ERR! code ENOENT
angular_1   | npm ERR! errno -2
angular_1   | npm ERR! syscall open
angular_1   | npm ERR! enoent ENOENT: no such file or directory, open '/usr/src/app/package.json'
angular_1   | npm ERR! enoent This is related to npm not being able to find a file.
angular_1   | npm ERR! enoent
angular_1   |
angular_1   | npm ERR! A complete log of this run can be found in:
angular_1   | npm ERR!     /root/.npm/_logs/2019-04-07T20_51_38_933Z-debug.log
harmonie_angular_1 exited with code 254

看一下我的文件夹层次结构:

-project
  -client
    -Dockerfile
    -package.json
  -server
    -Dockerfile
    -package.json
  -docker-compose.yml

这是我的 Angular 的 Dockerfile:

# Create image based on the official Node 10 image from dockerhub
FROM node:10

# Create a directory where our app will be placed
RUN mkdir -p /usr/src/app

# Change directory so that our commands run inside this new directory
WORKDIR /usr/src/app

# Copy dependency definitions
COPY package*.json /usr/src/app/

# Install dependecies
RUN npm install

# Get all the code needed to run the app
COPY . /usr/src/app/

# Expose the port the app runs in
EXPOSE 4200

# Serve the app
CMD ["npm", "start"]

我的 Express Dockerfile:

# Create image based on the official Node 6 image from the dockerhub
FROM node:6

# Create a directory where our app will be placed
RUN mkdir -p /usr/src/app

# Change directory so that our commands run inside this new directory
WORKDIR /usr/src/app

# Copy dependency definitions
COPY package*.json /usr/src/app/

# Install dependecies
RUN npm install

# Get all the code needed to run the app
COPY . /usr/src/app/

# Expose the port the app runs in
EXPOSE 3000

# Serve the app
CMD ["npm", "start"]

最后是我的docker-compose.yml文件

version: '3' # specify docker-compose version

# Define the services/containers to be run
services:
  angular: # name of the first service
    build: client # specify the directory of the Dockerfile
    ports:
      - "4200:4200" # specify port forwarding
#WHEN ADDING VOLUMES, ERROR APPEARS!!!!!!
    volumes:
      - ./client:/usr/src/app

  express: #name of the second service
    build: server # specify the directory of the Dockerfile
    ports:
      - "3000:3000" #specify ports forwarding
    links:
      - database
#WHEN ADDING VOLUMES, ERROR APPEARS!!!!!!
    volumes:
      - ./server:/usr/src/app

  database: # name of the third service
    image: mongo # specify image to build container from
    ports:
      - "27017:27017" # specify port forwarding

顺便问一下,如果您在构建过程中已经复制了内容,为什么还要挂载 ./server./client - Daniel Habenicht
因为这是本教程中的操作方式: https://scotch.io/tutorials/create-a-mean-app-with-angular-2-and-docker-compose 我也觉得很惊讶,但是作为一个新手... - Gabriel
2个回答

3
我也遇到了这个错误,结果是我的docker-compose版本有问题。我在Windows 10上运行WSL,WSL中安装的docker-compose版本未正确处理卷绑定。我通过删除/usr/local/bin/docker-compose然后添加一个别名到Windows docker-compose可执行文件alias docker-compose="/mnt/c/Program\ Files/Docker/Docker/resources/bin/docker-compose.exe"来解决了这个问题。
如果以上方法不适用于您,请尝试更新您的docker-compose版本

2

node_modules添加到.dockerignore文件中。 - Daniel Habenicht
@DanielHabenicht 忘记了我已经有一个 .dockerignore 用于那个目的。 - Gabriel
@Nikola Bodrozic:我看了你的教程,但是我没有看到与我的配置有什么不同。除了文件路径之外,由于你的教程没有描述文件层次结构,我无法将其与我的进行比较... - Gabriel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接