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

Allow for Dockerfile to be named something else. #9707

Merged
merged 1 commit into from Jan 7, 2015

Conversation

duglin
Copy link
Contributor

@duglin duglin commented Dec 17, 2014

This is a follow-on to #7995 and #7284.

This PR allows for the Dockerfile to be named something other than Dockerfile via a -f/--file option. Aside from the name/path of the Dockerfile being variable, nothing else should change from today's behavior. The location of the Dockerfile MUST be within the build context.

This PR contains the:

  • code changes
  • docs updates
  • testcases

Signed-off-by: Doug Davis dug@us.ibm.com

@crosbymichael
Copy link
Contributor

@duglin you are so fast!

if err != nil {
return fmt.Errorf("Bad .dockerignore pattern: '%s', error: %s", pattern, err)
}
if ok {
return fmt.Errorf("Dockerfile was excluded by .dockerignore pattern '%s'", pattern)
return fmt.Errorf("Dockerfile(", *dockerfileName, "was excluded by .dockerignore pattern '%s'", pattern)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks kinda weird, can you please use the traditional printf format here?

@cyphar
Copy link
Contributor

cyphar commented Dec 17, 2014

IMO, I don't like that the Dockerfiles use the root context. While it may make sense from a short-sighted end-user point of view, there's an issue regarding symmetry.

Specifically:

$ docker build -f dockerfiles/some-file
$ cd dockerfiles && docker build -f some-file

The above two lines are not synonymous, because the Dockerfiles are built in different contexts. This results in two (IMO not acceptable) side effects:

  1. The above symmetry is broken. This means that the directory in which docker build is called is now important, despite having a flag that allows you to specify relative directories.
  2. Dockerfiles now need to be context aware on a different level -- they need to care about where they are called from.

IMHO, Dockerfiles specified by the -f flag should be used as the root of the context, not applied to the root context of the docker run command.

Just my $0.02.

@tianon
Copy link
Member

tianon commented Dec 17, 2014

The "context" argument is still required, right?

@duglin
Copy link
Contributor Author

duglin commented Dec 17, 2014

@tianon yes I think that's a typo in his example.
So, let's say you do this (starting at the root of my build context on the cli):
cd foo
docker build -f myDockerfile ..

Right now this will fail because the CLI blindly passed the -f value to the daemon w/o taking into account that we're in "foo" and the daemon will look for myDockerfile in the root of the build context. IOW, the CLI really should convert "-f myDockerfile" to "-f foo/myDockerfile" before it sends it to the REST API so the daemon will find it.

@cyphar I think you're correct but let's see what other think before I change it.

@duglin
Copy link
Contributor Author

duglin commented Dec 17, 2014

@crosbymichael I fixed the errorf stuff you noticed.
@jfrazelle I got drone working. It turns out I had to name my containers as they were built. Go figure.

@tianon
Copy link
Member

tianon commented Dec 17, 2014

Cool, SGTM. I think the CLI would then also need to throw an error when
attempting to use a Dockerfile from outside the context.

ADD/COPY are also going to get a little confusing here. :)

@duglin
Copy link
Contributor Author

duglin commented Dec 17, 2014

@tianon yes, it already throws an error if -f (on the daemon) points to something outside of the context, but yes this will force an additional check on the CLI side.

@cyphar
Copy link
Contributor

cyphar commented Dec 17, 2014

@tianon a little more confusing. The reason for the whole context beef is that Dockerfiles now will need to either reference things relative to the root context when ADD or COPYing (which means they must know where they should be called from, meaning you lose some level of predictability) or relative to the Dockerfile (which means you now have Dockerfiles that care where they are placed inside the repository).

Neither of those options sound favourable to me (also the above symmetry concerns are quite important IMO, and if we're going to add this new functionality we might as well get it right the first time :P).

@crosbymichael
Copy link
Contributor

@cyphar but also, you don't have to use -f. The people who want this feature want more control and therefore must pay the portability cost that it comes with.

@duglin
Copy link
Contributor Author

duglin commented Dec 17, 2014

right, I think you should be able to put your dockerfile anywhere you want, but in the end any relative paths in there (e.g. on ADD/COPY) are relative to the context root. I think the docs are pretty clear on this already.

@cyphar
Copy link
Contributor

cyphar commented Dec 17, 2014

@crosbymichael Surely they can place Dockerfile.prod, Dockerfile.test, etc in the root directory and call it a day with -f in the example? It's not just a portability thing (although that is an issue), it's a symmetry and consistency thing.

But idk, I might be acting far too idealistically for a feature like this.

@duglin
Copy link
Contributor Author

duglin commented Dec 17, 2014

@cyphar are you saying you don't think I should make your suggested change?
Seemed right to me.

@cyphar
Copy link
Contributor

cyphar commented Dec 17, 2014

@duglin Nono, I think that the change should be made. I just thought I was the only one who thought that :P.

@LK4D4
Copy link
Contributor

LK4D4 commented Dec 17, 2014

I'm not sure about what we talking about, if context parameter is still necessary.

@cyphar
Copy link
Contributor

cyphar commented Dec 17, 2014

@LK4D4 Oh, I completely forgot about the context parameter. That makes -f even weirder. Do you have to do this now:

$ docker build -f somedir/Dockerfile.magic somedir/

Or should it assume that you want the context to be somedir, unless you specify the context parameter?

@LK4D4
Copy link
Contributor

LK4D4 commented Dec 17, 2014

@cyphar Yes, you have to do this.

@duglin
Copy link
Contributor Author

duglin commented Dec 17, 2014

so I guess the question is:
should -f be relative to the current dir or to the root of the build context?
As an end user, my first reaction was to think it should be relative to the current dir. Your first example was the most compelling to me:
cd foo
docker build -f myDockerfile ..
it would be really odd to me to have to write:
docker build -f foo/myDockerfile ..

@slafs
Copy link

slafs commented Dec 17, 2014

Just wanted to add that I think we have a potential backwards incompatible change here. Because having that kind of structure:

.
├── dockercontext
│   ├── Dockerfile
│   └── ...
├── Dockerfile
└── ...

and running docker build dockercontext/ (without -f option) could build different images on different versions of Docker. Right? Or am I missing something?

@duglin
Copy link
Contributor Author

duglin commented Dec 17, 2014

@slafs no it should not change that, if it does then I broke something :-)

@thaJeztah
Copy link
Member

Hope I'm not messing up the discussion with this, but something I brought up in #7284; Should each Dockerfile also be able to have its own .dockerignore file?

My use case; being able to build multiple images (dev/prod) from the same context, but exclude some large files (or large amount of files) from the image that are not needed when building a dev image. Being able to exclude those files gives a huge speed gain when building the image.

@crosbymichael
Copy link
Contributor

@duglin what you said here sounds like what we would expect as an end user "As an end user, my first reaction was to think it should be relative to the current dir."

We can go that route.

@duglin
Copy link
Contributor Author

duglin commented Dec 18, 2014

@thaJeztah if we did want to support multiple .dockerignores then I would think it could be done in a similar way to the -f option, so: -i/--ignore=file but I think that would be a new PR.

@LK4D4
Copy link
Contributor

LK4D4 commented Jan 6, 2015

@duglin need rebase :) sorry

@duglin
Copy link
Contributor Author

duglin commented Jan 6, 2015

@LK4D4 ok rebased


if *dockerfileName == "" {
// No -f/--file was specified so use the default
origDockerfile = "Dockerfile"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we make this a constant?

@erikh
Copy link
Contributor

erikh commented Jan 6, 2015

need the constant, otherwise LGTM

@erikh erikh self-assigned this Jan 6, 2015
@duglin
Copy link
Contributor Author

duglin commented Jan 6, 2015

@erikh ok created a constant and used it on client and server

@duglin
Copy link
Contributor Author

duglin commented Jan 6, 2015

ok rebased and constant is made/used.

@crosbymichael
Copy link
Contributor

@duglin Either we are working really fast today or you forgot to push your rebase ;)

Add a check to make sure Dockerfile is in the build context
Add docs and a testcase
Make -f relative to current dir, not build context

Signed-off-by: Doug Davis <dug@us.ibm.com>
@duglin
Copy link
Contributor Author

duglin commented Jan 7, 2015

no, I pushed it, but then #8748 caused a conflict.
rebased and added a testcase to make sure that the new .dockerignore processing (from #8748) works even when the Dockerfile is called something else.

@crosbymichael
Copy link
Contributor

Nice

@crosbymichael
Copy link
Contributor

Cool, i think this has enough LGTMs

crosbymichael added a commit that referenced this pull request Jan 7, 2015
Allow for Dockerfile to be named something else.
@crosbymichael crosbymichael merged commit a67e738 into moby:master Jan 7, 2015
@duglin duglin deleted the RenameDockerfile branch January 22, 2015 22:31
@duglin duglin unassigned erikh Feb 5, 2015
@dreamcat4
Copy link

We also need this feature on the DockerHub (hub.docker.com) for it's Automated Builds. In the 'Edit Build Settings' page where it has the Field named 'Dockerfile Location'.

https://support.docker.com/hc/en-us/requests/3494

gurjeet added a commit to gurjeet/docker that referenced this pull request Oct 13, 2015
NOTE on Oct 13, 2015:
This patch has now been obsoleted, since the merge of moby#9707
moby#9707
END NOTE

Now one can perform the following commands in the same directory:

    docker build -t db_container -f db.dockerfile .
    docker build -t web_container -f web.dockerfile .
    docker build -t client_container -f client.dockerfile .
    docker build -t client_container .

The last one will use the default Dockerfile file.
@thaJeztah thaJeztah added area/builder kind/feature Functionality or other elements that the project doesn't currently have. Features are new and shiny labels Dec 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/builder kind/feature Functionality or other elements that the project doesn't currently have. Features are new and shiny
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet