Where to keep Dockerfile's in a project?

I am gaining knowledge about Docker and I have the following questions

  • Where are Dockerfile's kept in a project?
    • Are they kept together with the source?
    • Are they kept outside of the source? Do you have an own Git repository just for the Dockerfile?

If the CI server should create a new image for each build and run that on the test server, do you keep the previous image? I mean, do you tag the previous image or do you remove the previous image before creating the new one?

I am a Java EE developer so I use Maven, Jenkins etc if that matter.


Solution 1:

The only restriction on where a Dockerfile is kept is that any files you ADD to your image must be beneath the Dockerfile in the file system. I normally see them at the top level of projects, though I have a repo that combines a bunch of small images where I have something like

top/
  project1/
    Dockerfile
    project1_files
  project2/
    Dockerfile
    project2_files  

The Jenkins docker plugin can point to an arbitrary directory with a Dockerfile, so that's easy. As for CI, the most common strategy I've seen is to tag each image built with CI as 'latest'. This is the default if you don't add a tag to a build. Then releases get their own tags. Thus, if you just run an image with no arguments you get the last image built by CI, but if you want a particular release it's easy to say so.

Solution 2:

I'd recommend keeping the Dockerfile with the source as you would a makefile.

The build context issue means most Dockerfiles are kept at or near the top-level of the project. You can get around this by using scripts or build tooling to copy Dockerfiles or source folders about, but it gets a bit painful.

I'm unaware of best practice with regard to tags and CI. Tagging with the git hash or similar might be a good solution. You will want to keep at least one generation of old images in case you need to rollback.