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

构建Dockerfile镜像失败求助:ADD指令报错找不到文件

Fixing the "ADD failed: no such file or directory" Error in Docker

Hey there, let's work through this Docker build error together—this is a super common issue tied to how Docker handles build contexts, so we'll get it sorted quickly.

First, Understand the Root Cause

The key thing to remember here is that when you run docker build, Docker uses the directory you run the command from (or the directory you specify) as its "build context". It only has access to files and directories inside this context—any path you use in ADD or COPY has to be relative to this context. The error you're seeing means Docker can't find the service directory within the build context it's using.

Step-by-Step Fixes

1. Verify where your service directory lives

Take a look at the ls output you added to your Dockerfile. That output shows you exactly what's available in the build context. If service isn't listed there, it's either:

  • Not in the directory you're running docker build from
  • Or you're pointing to the wrong path in your ADD command

2. Adjust your ADD command or build context

Here are the most common fixes:

  • If service is in a subdirectory of your build context: Update the ADD path to match. For example, if service is inside a src folder, use ADD src/service /container/service
  • If service is outside your current build context: You have two options:
    • Move the service directory into the folder you're running docker build from (this is the simplest approach for most cases)
    • Or specify a different build context when running docker build. For example, if service is in /home/user/shared, and your Dockerfile is in /home/user/my-app, run:
      docker build -f /home/user/my-app/Dockerfile /home/user/shared
      
      This tells Docker to use /home/user/shared as the build context (so it can see the service directory) while still using your existing Dockerfile.

3. Double-check spelling and case sensitivity

Linux-based Docker images are case-sensitive. Make sure the directory name in your ADD command matches exactly—Service is not the same as service, so a typo here will cause the error too.

4. Test with an explicit RUN command

To confirm what's in your build context, add this line right before your ADD command:

RUN ls -la

The output of this command during the build will show every file/directory in the build context. If service isn't here, you know you need to adjust either the directory structure or your build command.

Example Scenario

Let's say your setup looks like this:

/home/user/
├── my-app/
│   └── Dockerfile
└── service/
    └── (your service files)

Running docker build . from /home/user/my-app/ will fail because service isn't in the my-app context. Instead, run:

docker build -f /home/user/my-app/Dockerfile /home/user

Now Docker uses /home/user as the context, so it can see the service directory, and your ADD service /container/service command will work.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:05:05