Hidden File .Env Not Copied Using Docker Copy

Hidden file .env not copied using Docker COPY

If you have .dockerignore file then it might be you added to ignore hidden files like .git, .vagrant etc.

If .dockerfile ignoring hidden files then either you can enable to not ignore or change file name.

For more info about .dockerignore file

dockerfile COPY not copying file

Thanks everyone, finally I managed to see the problem.
It's quite weird... I'm cloning my repository to dest-folder, but copying the file in dest_folder.
That's why the file wasn't being detected, because it was in another folder.

Dockerfile COPY .env directory

COPY ./aws [DESTINATION] is "COPY, from the current directory './', the directory named 'aws' to the [DESTINATION]".

COPY ./.aws [DESTINATION] will COPY the hidden directory '.aws' from the current directory './' to [DESTINATION].

COPY ./.aws/ /home/appuser/.aws will result in /home/appuser/.aws/credentials existing in the built image.


Tip: [DESTINATION] is created by COPY if it doesn't already exist.

Note: if the directory is ignored in a .dockerignore then the COPY will not work.

Note: should never COPY credentials in an image if you intend on sharing the image rather bind-mount the credentials during the containers runtime i.e. docker run --rm -it -v ./.aws/credentials:/home/appuser/.aws/credentials:ro myimage

COPYing a file in a Dockerfile, no such file or directory?

The COPY instruction in the Dockerfile copies the files in src to the dest folder. Looks like you are either missing the file1, file2 and file3 or trying to build the Dockerfile from the wrong folder.

Refer Dockerfile Doc

Also the command for building the Dockerfile should be something like.

cd into/the/folder/
docker build -t sometagname .

Azure DevOps copying .env file to docker image

You can add a download secure file task to download the .env file to the agent machine.

Once downloaded, use the name value that is set on the task (or "Reference name" in the classic editor) to reference the path to the secure file on the agent machine. For example, if the task is given the name mySecureFile, its path can be referenced in the pipeline as $(mySecureFile.secureFilePath). Alternatively, downloaded secure files can be found in the directory given by $(Agent.TempDirectory)

Then you can add Copy files task to copy the .env file downloaded by above task to the docker build context. For below example:

steps:
- task: DownloadSecureFile@1
displayName: 'Download secure file'
inputs:
secureFile: .env

- task: CopyFiles@2
displayName: 'Copy Files to: $(System.DefaultWorkingDirectory)'
inputs:
SourceFolder: '$(Agent.TempDirectory)'
Contents: .env
TargetFolder: '$(System.DefaultWorkingDirectory)'

If folder $(System.DefaultWorkingDirectory) (ie. c:\agent_work\1\s) is the docker build context. You can then refer to .env file from your Dockerfile.

Docker: COPY failed: file not found in build context (Dockerfile)

The docker context is the directory the Dockerfile is located in. If you want to build an image that is one of the restrictions you have to face.

In this documentation you can see how contexts can be switched, but to keep it simple just consider the same directory to be the context. Note; this also doesn't work with symbolic links.

So your observation was correct and you need to place the files you need to copy in the same directory.

Alternatively, if you don't need to copy them but still have them available at runtime you could opt for a mount. I can imagine this not working in your case because you likely need the files at startup of the container.



Related Topics



Leave a reply



Submit