You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker构建时COPY指令报错:未指定源文件问题求助

Docker构建时"no source files were specified"错误解决方法

错误核心原因

Docker的COPY命令不支持宿主机绝对路径,源路径必须是相对于构建上下文的相对路径。构建上下文是你执行docker build命令时所在的目录(或通过-f参数指定Dockerfile时,上下文为你指定的目录)。你写的/usr/jim/docker-app/package*.json是宿主机绝对路径,Docker在构建上下文范围内找不到对应文件,因此报错。

假设你的目录结构

/usr/jim/
  ├── dockerwork/       # 存放Dockerfile和index.js
  └── docker-app/       # 存放应用文件(package.json等)

两种修正方案

方案一:调整构建上下文(推荐)

  1. 切换到上级目录:
    cd /usr/jim
    
  2. 执行构建命令,指定Dockerfile位置:
    docker build -t your-image-name -f dockerwork/Dockerfile .
    
  3. 修改Dockerfile中的COPY路径(相对于/usr/jim上下文):
    # Use the official Node.js image from Docker Hub
    FROM node:latest
    
    # Set the working directory inside the container
    WORKDIR /usr/jim/dockerwork
    
    # Copy package.json and package-lock.json from docker-app directory
    COPY docker-app/package*.json ./
    
    # Install the dependencies
    RUN npm install
    
    # Copy the rest of the application files from docker-app into the container
    COPY docker-app .
    
    # Expose port 8080 to the outside world
    EXPOSE 8080
    
    # Start the application
    CMD ["npm", "start"] 
    

方案二:使用相对上级目录的路径

如果保持在dockerwork目录执行构建:

  1. 直接执行构建命令:
    docker build -t your-image-name .
    
  2. 修改Dockerfile中的COPY路径(相对于dockerwork目录):
    # Use the official Node.js image from Docker Hub
    FROM node:latest
    
    # Set the working directory inside the container
    WORKDIR /usr/jim/dockerwork
    
    # Copy package.json and package-lock.json from ../docker-app directory
    COPY ../docker-app/package*.json ./
    
    # Install the dependencies
    RUN npm install
    
    # Copy the rest of the application files from ../docker-app into the container
    COPY ../docker-app .
    
    # Expose port 8080 to the outside world
    EXPOSE 8080
    
    # Start the application
    CMD ["npm", "start"] 
    

额外提醒

  • WORKDIR是容器内部的路径,和宿主机目录无关,你的设置是正确的。
  • 先复制package*.json再执行npm install的做法是合理的,能利用Docker缓存层,避免代码改动时重复安装依赖。

内容的提问来源于stack exchange,提问作者wheedwhackerjones

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 10:37:06