59

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.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
LuckyLuke
  • 47,771
  • 85
  • 270
  • 434
  • 1
    If you use Docker Compose, you can use the `context:` directive and store docker files in subdirectories. See https://stackoverflow.com/a/47786215/102675 – Simon Woodside Nov 01 '18 at 02:54

2 Answers2

26

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.

seanmcl
  • 9,740
  • 3
  • 39
  • 45
  • But you need one Dockerfile for the database, one for the application server, data volume containers and so on? How do you organize that? – LuckyLuke Dec 10 '14 at 19:51
  • With the layout I have above, you can have the components be the subdirectories, and then use top/fig.yml to launch and link the containers together. – seanmcl Dec 10 '14 at 20:44
3

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.

Adrian Mouat
  • 44,585
  • 16
  • 110
  • 102
  • 1
    But you need one Dockerfile for the database, one for the application server, data volume containers and so on? How do you organize that? – LuckyLuke Dec 10 '14 at 19:50
  • 1
    You can probably use the same Dockerfile for the Database and data-containers. But yeah, you either have to put the Dockerfiles in separate directories or use build tooling to copy things about. – Adrian Mouat Dec 10 '14 at 20:27