user1753106
user1753106

Reputation: 1316

Can dockerfile be put in .dockerignore?

The documentation says that yes it can.

https://docs.docker.com/engine/reference/builder/

You can even use the .dockerignore file to exclude the Dockerfile and .dockerignore files. These files are still sent to the daemon because it needs them to do its job. But the ADD and COPY instructions do not copy them to the image.

But when I put dockerfile in the .dockerignore I get

Sending build context to Docker daemon  1.646MB
Error response from daemon: Cannot locate specified Dockerfile: Dockerfile

Upvotes: 40

Views: 29803

Answers (3)

BMitch
BMitch

Reputation: 264316

Make sure you are running a current version of docker. Older versions won't match the documentation. This feature was added in release 1.5.

$ cat .dockerignore 
Dockerfile
.dockerignore

$ cat Dockerfile 
FROM busybox
COPY . /context
WORKDIR /context
CMD find .

$ docker build -t test-context .                               
Sending build context to Docker daemon  4.096kB
Step 1/4 : FROM busybox
 ---> 54511612f1c4
Step 2/4 : COPY . /context
 ---> bd941f5e9e18
Step 3/4 : WORKDIR /context
Removing intermediate container 47245e176bf4
 ---> c25ebcc95d95
Step 4/4 : CMD find .
 ---> Running in 6c1b05066c80
Removing intermediate container 6c1b05066c80
 ---> 6ee47079b59c
Successfully built 6ee47079b59c
Successfully tagged test-context:latest

$ docker run -it --rm test-context                             
.
./test-file2
./test-file1

$ docker version
Client:
 Version:      17.11.0-ce
 API version:  1.34
 Go version:   go1.8.3
 Git commit:   1caf76c
 Built:        Mon Nov 20 18:36:33 2017
 OS/Arch:      linux/amd64

Server:
 Version:      17.11.0-ce
 API version:  1.34 (minimum version 1.12)
 Go version:   go1.8.3
 Git commit:   1caf76c
 Built:        Mon Nov 20 18:35:05 2017
 OS/Arch:      linux/amd64
 Experimental: true

Upvotes: 2

user1753106
user1753106

Reputation: 1316

The question was edited a while back which I didn't notice. The original question more accurately reflects the knowledge that I was missing and the issue that I was having and I have reverted to that version.

Docker treats the names 'dockerfile' and 'Dockerfile' equally up to a point. i.e. you can call it 'dockerfile' and docker will still automatically use it if you do something like docker build -t blah .. However if you then put dockerfile in the .dockerignore file it doesn't know how to build your image automatically anymore and you will get

Error response from daemon: Cannot locate specified Dockerfile: Dockerfile

Upvotes: 7

TJ Biddle
TJ Biddle

Reputation: 6484

Yes, you can; you can even throw the .dockerignore itself in there!

You're likely doing something else incorrect - possibly in the wrong directory?

Directory listing:

➜  ls -la
total 16
drwxr-xr-x  4 tj    wheel  128 Nov 30 13:42 .
drwxrwxrwt  7 root  wheel  224 Nov 30 13:42 ..
-rw-r--r--  1 tj    wheel   26 Nov 30 13:41 .dockerignore
-rw-r--r--  1 tj    wheel   28 Nov 30 13:42 Dockerfile

Content of files:

➜  cat .dockerignore
.dockerignore
Dockerfile

➜  test_docker_ignore cat Dockerfile
FROM ubuntu:16.04
ADD . .

Build it once; specifying --no-cache to be verbose:

➜  docker build -t test --no-cache .
Sending build context to Docker daemon  3.072kB
Step 1/2 : FROM ubuntu:16.04
 ---> 20c44cd7596f
Step 2/2 : ADD . .
 ---> 4d8ded297954
Successfully built 4d8ded297954
Successfully tagged test:latest

Add something to the Dockerfile and rebuild: The build will use the cache as it ignores the changes made to the Dockerfile

➜  echo "# A Test Comment" >> Dockerfile
➜  docker build -t test .
Sending build context to Docker daemon  3.072kB
Step 1/2 : FROM ubuntu:16.04
 ---> 20c44cd7596f
Step 2/2 : ADD . .
 ---> Using cache
 ---> 4d8ded297954
Successfully built 4d8ded297954
Successfully tagged test:latest

Upvotes: 40

Related Questions