构建Docker镜像时遇文件/目录不存在错误,请求协助
Alright, let's figure out why your Docker COPY command is failing and get it fixed. The error message clearly states Docker can't locate /fecode/dist in the ams-fe build stage or image—here are the most likely reasons and step-by-step checks to resolve this:
1. Verify the ams-fe build stage actually generates /fecode/dist
The --from=ams-fe flag pulls files from a previous build stage (defined with AS ams-fe in your Dockerfile) or a pre-existing image. First, confirm this stage is properly creating the dist directory:
- Check your Dockerfile for the
ams-festage definition. It should look something like this (adjust based on your frontend stack):FROM node:18 AS ams-fe WORKDIR /fecode # Sets the working directory to /fecode COPY package*.json ./ RUN npm install COPY . . RUN npm run build # This command should generate the dist folder - Ensure the build command (like
npm run build) actually runs successfully and outputs thedistdirectory into/fecode. If this command fails silently or outputs to a different path, thedistfolder won't exist where Docker expects it.
2. Double-check the path in the COPY command
It’s easy to mix up paths. Here’s how to confirm:
- Temporarily add a debug step to your
ams-festage to list the contents of/fecode:RUN ls -la /fecode # Add this right after your build command - Rebuild the image and look at the output—you should see the
distdirectory listed. If it’s missing, your build command isn’t generating it. If it’s in a different location (like/distinstead of/fecode/dist), update your COPY command to match the correct path.
3. Confirm the build stage name is correct
Docker is case-sensitive when referencing build stages. Make sure:
- The name in
COPY --from=ams-feexactly matches theAS ams-fedefinition in your Dockerfile. For example, if you wroteAS ams_fe(underscore instead of hyphen), the COPY command will fail because it can’t find the stage.
4. Rule out Docker cache issues
Sometimes Docker uses cached build stages, which might skip steps that generate the dist directory. Try rebuilding with no cache to force a fresh build:
docker build --no-cache -t project/test:v1 .
5. Test the ams-fe stage independently
To isolate the problem, build just the ams-fe stage and inspect it directly:
# Build only the ams-fe stage docker build --target ams-fe -t ams-fe-test . # Run a temporary container to check for the dist directory docker run --rm -it ams-fe-test ls /fecode/dist
If this command returns "No such file or directory", you know the issue is entirely within the ams-fe stage—focus on fixing the build steps there to generate the dist folder.
内容的提问来源于stack exchange,提问作者Owais

