38

When building my Docker image I need to copy all of the files in the same directory in to the Docker image.

I attempted to do this

ADD ./* $HOME/src

RUN ls $HOME/src

but it doesn't seem to work

ls: cannot access /root/src: No such file or directory

How would I go about copying all of the current directory and subdirectories in to my docker image while building?

2

2 Answers 2

30

Just add / at the end of src in ADD statement.

ADD ./* $HOME/src/
2
  • 24
    Just a minor remark - using ./* seems to copy all files as a flat list into src (I used COPY instead of ADD but I doubt there lies the difference). If you want to copy the current project as is, better use COPY . /src/ Commented Jun 4, 2020 at 14:40
  • 5
    OP, please modify your answer based on @ShovalSadde's suggestion. COPY . /dest/directory is indeed the right way to do it. Especially to maintain directory structure, etc. Commented Oct 17, 2022 at 13:12
23

I was building the images using docker build - < Dockerfile which apparently doen't send the build context so things can't be copied. After changing to docker build . and adding the / like MB11 suggested the build worked.

3
  • I am doing the same, and is there a way to add context with docker build - < Dockerfile?
    – skyuuka
    Commented Mar 10, 2020 at 6:26
  • 4
    Figure it out, use docker build -f Dockerfile .
    – skyuuka
    Commented Mar 10, 2020 at 6:36
  • True, i just encountered the same situation and your solution resolves
    – devplayer
    Commented Mar 19 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.