Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dockerfile specified with -f not found within build context when cwd is a symlink #16339

Closed
jmanning2k opened this issue Sep 16, 2015 · 10 comments

Comments

@jmanning2k
Copy link

Description of problem:
Symlinks appear to be resolved for the build context path (#15039), however they are not normalized/resolved when checking if a Dockerfile specified with '-f' is within the build context.

docker version:

docker version
Client:
 Version:      1.8.1
 API version:  1.20
 Go version:   go1.4.2
 Git commit:   d12ea79
 Built:        Thu Aug 13 02:32:18 UTC 2015
 OS/Arch:      linux/amd64

Server:
 Version:      1.8.1
 API version:  1.20
 Go version:   go1.4.2
 Git commit:   d12ea79
 Built:        Thu Aug 13 02:32:18 UTC 2015
 OS/Arch:      linux/amd64

docker info:

Containers: 18
Images: 719
Storage Driver: aufs
 Root Dir: /var/lib/docker/aufs
 Backing Filesystem: extfs
 Dirs: 755
 Dirperm1 Supported: false
Execution Driver: native-0.2
Logging Driver: json-file
Kernel Version: 3.13.0-57-generic
Operating System: Ubuntu precise (12.04.5 LTS)
CPUs: 2
Total Memory: 7.304 GiB
Name: lims-jmanning
ID: W2PD:YEXU:IOKW:BFAK:AXAJ:CQA5:TVRW:LMWI:TMFT:H6KR:4BVK:GH42
WARNING: No swap limit support

uname -a: Linux lims-jmanning 3.13.0-57-generic #95~precise1-Ubuntu SMP Mon Jun 22 09:43:07 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

Environment details (AWS, VirtualBox, physical, etc.):
Both on Ubuntu based AWS node and a docker-machine image on OSX

How reproducible:
Always, given recipe below.

Steps to Reproduce:

$ cd /home/test
$ ln -sf /tmp tmp
$ cd tmp
$ mkdir test-docker
$ cd test-docker
$ echo "FROM scratch" > Dockerfile
$ docker build .    ## WORKS
Sending build context to Docker daemon 2.048 kB
Step 0 : FROM scratch
 --->
No image was generated. Is your Dockerfile empty?
$ docker build -f Dockerfile .   ## FAILS
unable to prepare context: The Dockerfile (/home/test/tmp/test-docker/Dockerfile) must be within the build context (.)
$ docker build -f $(pwd)/Dockerfile $(pwd)  ## FAILS
unable to prepare context: The Dockerfile (/home/test/tmp/test-docker/Dockerfile) must be within the build context (/home/test/tmp/test-docker)
$ docker build -f $(pwd -P)/Dockerfile .    ## WORKS - pwd -P gives path with symlinks resolved
Sending build context to Docker daemon 2.048 kB
Step 0 : FROM scratch
 --->
No image was generated. Is your Dockerfile empty?
$echo docker build -f $(pwd -P)/Dockerfile $(pwd)
docker build -f /tmp/test-docker/Dockerfile /home/test/tmp/test-docker
$docker build -f $(pwd -P)/Dockerfile $(pwd)    ### WORKS even though paths are different
Sending build context to Docker daemon 2.048 kB
Step 0 : FROM scratch
 --->
No image was generated. Is your Dockerfile empty?

Actual Results:
Build fails unless I provide fully resolved path to Dockerfile regardless of if cwd is symlinked path or '.'
Error given is a variant of:
unable to prepare context: The Dockerfile (/home/test/tmp/test-docker/Dockerfile) must be within the build context (.)

Expected Results:
These commands should work regardless of symlinks. As noted in docker build -h it should be valid to specify docker build -f PATH/Dockerfile PATH
$ docker build -f Dockerfile . ## FAILS
$ docker build -f $(pwd)/Dockerfile $(pwd) ## FAILS

Additional info:
Our build uses symlinks for build folder in some environmentsso that dev builds and production builds can use the same filesystem paths throughout.
Related to #15037, probably caused by #15039 / bdc55be (build context dir is normalized for symlinks, but not a Dockerfile specified with '-f').

I think #14339 is the same issue, but mistakenly closed as duplicate of a similar but non-identical issue.

In the real world case, I am attempting to use '-f Dockerfile_base', which is why I can't simply omit the '-f' flag.

Same behavior seen on:
Docker version 1.8.1, build d12ea79 (ubuntu)
Docker version 1.8.2, build 0a8c2e3 (OSX)

EDIT: Updated to bug report template.

@rncry
Copy link

rncry commented Sep 25, 2015

I'm having this issue but without symlinks being used.

@jmanning2k
Copy link
Author

I can confirm there is an identical failure without symlinks when using docker-machine on OSX. Using any folder outside the folders mounted in the VM will give this error (only when '-f Dockerfile' is used, it works outside the build context when no Dockerfile is specified. Possibly the same cause.

OSX via dockermachine:

docker-machine version 0.4.1 (e2c88d6)
Docker version 1.8.2, build 0a8c2e3
#
$ mkdir /tmp/test-docker2 # not a symlink
$ cd /tmp/test-docker2
$ echo "FROM scratch" > Dockerfile
$ docker build -f Dockerfile .
unable to prepare context: The Dockerfile (/tmp/test-docker2/Dockerfile) must be within the build context (.)
$ docker build .
Sending build context to Docker daemon 2.048 kB
Step 0 : FROM scratch
(... WORKS)

This (original) error occurs on Linux only with symlinks involved. I cannot reproduce on Linux without symlinks.

@AaronDMarasco-VSI
Copy link

Seeings same problem on CentOS 7 (Docker version 1.8.2, build 0a8c2e3).

Worked around it using "realpath":

[amarasco@server releng]$ touch Dockerfile
[amarasco@server releng]$ ls -alF ~/work
lrwxrwxrwx. 1 amarasco amarasco 15 Sep 29 15:36 /home/amarasco/work -> /data/amarasco//
[amarasco@server releng]$ docker build -t image_81410_6 -f ./Dockerfile .
unable to prepare context: The Dockerfile (/home/amarasco/work/releng/Dockerfile) must be within the build context (.)
[amarasco@server releng]$ docker build -t image_81410_6 -f $(realpath .)/Dockerfile .
Sending build context to Docker daemon 91.65 kB
Error response from daemon: The Dockerfile (Dockerfile) cannot be empty

@Crazykev
Copy link
Contributor

I want to take a shot at this one.

@Crazykev
Copy link
Contributor

@thaJeztah I misunderstood the issue yesterday. Today I just can't reproduce this using 'master' and 'bf80ade'. It seems this issue has been fixed.

Here is the steps: (almost same as @jmanning2k did)

$ mkdir $HOME/test
$ cd $HOME/test
$ ln -sf /tmp tmp
$ cd tmp
$ mkdir test-docker
$ cd test-docker/
$ echo "FROM ubuntu" > Dockerfile
$ docker-1.9.0-dev build .
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM ubuntu
 ---> cdd474520b8c
Successfully built cdd474520b8c
$ docker-1.9.0-dev build -f Dockerfile .
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM ubuntu
 ---> cdd474520b8c
Successfully built cdd474520b8c
$ docker-1.9.0-dev build -f $(pwd)/Dockerfile $(pwd)
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM ubuntu
 ---> cdd474520b8c
Successfully built cdd474520b8c
$ docker-1.9.0-dev build -f $(pwd -P)/Dockerfile .
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM ubuntu
 ---> cdd474520b8c
Successfully built cdd474520b8c
$ docker-1.9.0-dev build -f $(pwd -P)/Dockerfile $(pwd)
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM ubuntu
 ---> cdd474520b8c
Successfully built cdd474520b8c
$ docker-1.9.0-dev build -f $(pwd)/Dockerfile $(pwd -P)
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM ubuntu
 ---> cdd474520b8c
Successfully built cdd474520b8c

@jmanning2k It seems this issue has been fixed. Can you still reproduce this using current 'master' ?

@djui
Copy link

djui commented Oct 19, 2015

I have this issue as well, running Docker 1.8.3. The symptom is when trying to build an image using -f in the temp directory ($TMPDIR) on OS X 10.11 the reported "out of context" error occurs. On OS X (Darwin) $TMPDIR resolves to /var/folders/.../.../... but /var is symlink'd /private/var. And using /tmp would also not work as it's symlink'd to /private/tmp.

One temporary solution is:

- cd ${TMPDIR:-/tmp}
+ cd -P ${TMPDIR:-/tmp}

  docker build --file=Dockerfile

@jmanning2k
Copy link
Author

Finally had a chance to test with 1.9.0 on linux and OSX, both working, no longer showing this error.

@thaJeztah
Copy link
Member

Thanks @jmanning2k! Good to hear it's resolved

@Boulboulouboule
Copy link

Thanks !
docker build --file=Dockerfile
works for me too :)

@rajiv180984
Copy link

I faced the same issue. I am using docker version:17.09.0-ce. I follow below steps.
Step 1: create Dockerfile and added commands for creating docker image
Step 2: go to directory where we have created Dockfile
Step 3: execute below command
$sudo docker build -t ubuntu-test:latest .
It resolved issue and image created successsfully
Note: build command depend on docker version as well as which build option we are using. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants