Dockerfile中ADD与COPY命令失效问题求助
Let’s break down the common issues that cause files copied via COPY/ADD to disappear in Heroku’s container environment, plus actionable fixes to resolve them:
Set your working directory explicitly
Heroku defaults to/appas the container’s working directory, but declaring this at the top of your Dockerfile eliminates relative path confusion. Add this line before anyCOPY/ADDcommands:WORKDIR /appNow all subsequent copy commands will target
/appby default. For example,COPY . .copies everything from your local project root into/app—exactly the directory you land in when runningheroku run bash.Audit your
.dockerignorefile
This is one of the most overlooked culprits. If your.dockerignorelists the files/directories you’re trying to copy (e.g.,luup/,config/, or even a misplaced.), Docker will skip copying them entirely. Open your.dockerignoreand remove any lines that exclude the content you need in the container.Validate your
COPYsyntax with the working directory
WithWORKDIR /appset, use clean, reliable syntax:- Copy your entire local project to
/app:COPY . . - Copy the local
luupdirectory to/app/luup:COPY luup ./luup/ - Copy
requirements.txtdirectly to/app:COPY requirements.txt .
Stick to relative paths here—absolute paths are unnecessary and can introduce unexpected behavior.
- Copy your entire local project to
Test locally first to rule out Heroku-specific issues
Before deploying, build and run your image locally to confirm files are present:# Build the image docker build -t my-heroku-app . # Run a temporary container and open bash docker run -it --rm my-heroku-app bashIf files are missing locally, the problem is in your Dockerfile or build context—not Heroku. If they’re present locally but missing on Heroku, double-check that you’re pushing the correct image tag to Heroku.
Check Heroku’s build logs
When you runheroku container:push web, scan the logs for lines related toCOPYcommands. You should see entries like:Step 5/10 : COPY . . ---> Using cache ---> abc123def456If these lines are missing or show errors (e.g., "no such file or directory"), your files aren’t available in the build context when Heroku processes your image.
Don’t overwrite
/applater in your Dockerfile
Ensure you don’t have commands that delete or replace the/appdirectory after copying files. For example, aRUN rm -rf /apporVOLUME /appcommand would wipe out your copied content.
If you’ve tried all these steps and still have issues, sharing your full Dockerfile and .dockerignore content can help pinpoint the problem.
内容的提问来源于stack exchange,提问作者Jay Jung

