561

I have read http://docs.docker.com/engine/reference/builder/#add however I met a problem. I want to copy the local directory go to docker /user/local/

I tried:

ADD go /usr/local/

and:

ADD /go/ /usr/local/ 

also:

RUN chmod 0755 /usr/local/go/src/make.bash

However, I see the following error message:

/usr/local/go/src/make.bash: No such file or directory

but the local go directory does contain make.bash.

5 Answers 5

1073
ADD go /usr/local/

will copy the contents of your local go directory in the /usr/local/ directory of your docker image.

To copy the go directory itself in /usr/local/ use:

ADD go /usr/local/go

or

COPY go /usr/local/go
12
  • 66
    I'm trying to use the multiple source version of ADD, and so I can't specify the target directory name for the destination. Is there a way to do what the OP asked in a multi-src format (i.e. without creating an ADD layer for each source directory)?
    – Guss
    May 26, 2015 at 18:15
  • 15
    I just want to add a sidenote to this by saying that $HOME or ~ or any other shell variable will not work. I'm saying this because I spent good amount trying to figure this out than I'd like to admit even though it's glaringly obvious to most.
    – shriek
    Sep 18, 2015 at 1:08
  • 45
    COPY go /usr/local/go doesn't work for me. Does not copy.
    – Tyguy7
    Dec 2, 2015 at 18:52
  • 125
    This is really quite a confusing matter. Why can't COPY simply behave like cp? Instead there needs to be an extra police how it works. Mar 2, 2017 at 14:07
  • 22
    .dockerignore file can hide a directory and COPY or ADD will not work for listed items
    – Vereb
    Mar 24, 2017 at 13:14
119

Indeed ADD go /usr/local/ will add content of go folder and not the folder itself, you can use Thomasleveil solution or if that did not work for some reason you can change WORKDIR to /usr/local/ then add your directory to it like:

WORKDIR /usr/local/
COPY go go/

or

WORKDIR /usr/local/go
COPY go ./

But if you want to add multiple folders, it will be annoying to add them like that, the only solution for now as I see it from my current issue is using COPY . . and exclude all unwanted directories and files in .dockerignore, let's say I got folders and files:

- src 
- tmp 
- dist 
- assets 
- go 
- justforfun 
- node_modules 
- scripts 
- .dockerignore 
- Dockerfile 
- headache.lock 
- package.json 

and I want to add src assets package.json justforfun go so:

in Dockerfile:

FROM galaxy:latest

WORKDIR /usr/local/
COPY . .

in .dockerignore file:

node_modules
headache.lock
tmp
dist

In this way, you ignore node_modules headache.lock tmp dist so they will not be added!

Or for more fun (or you like to confuse more people make them suffer as well :P) can be:

*
!src 
!assets 
!go 
!justforfun 
!scripts 
!package.json 

In this way you ignore everything, but exclude what you want to be copied or added only from the "ignore list".

It is a late answer but adding more ways to do the same covers even more cases.

4
  • 4
    This answer is much more clear, and actually works. This should be the accepted answer.
    – dashesy
    Dec 29, 2021 at 21:58
  • This approach can copy unwanted files and directories on docker images unawares, if forgot updating files or directories which newly added on .dockerignore. Feb 9, 2022 at 9:45
  • 1
    @InsungPark you can ignore everything and exclude what you want to be added, did you see my second approach? Feb 10, 2022 at 17:19
  • @Al-Mothafar Oh, I didn't check your last approach, I think it will work well and until now it's maybe only way to keep single layer on copying multiple directory, even a little bit tricky. Thank you for notifying me. Feb 11, 2022 at 5:14
30

You can use COPY. You need to specify the directory explicitly. It won't be created by itself

COPY go /usr/local/go

Reference: Docker CP reference

11

As the official docs state:

The directory itself is not copied, just its contents.

The trick is to concat in the <dest> path also the folder name, like this:

COPY src ./src

Even if ./src does not exist in the container yet, the command COPY internally creates it and copies the content of src into the new folder (which is ./src).

-4

This can help if you want to add all files to a specified location

#ADD XML SUITE files ADD src/test/resources/xmls/* /usr/share/tag/

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