Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dockerfile setup for both production and development?

I'm wondering how people go about using docker for both production and development. In development I want to mount my source/build files to be able to quickly and easily make changes. For production, I want to include the source/build files in the image.

How is this typically done and is there a best or more common practice?

In my mind, ideally I would have one Dockerfile that uses something like a flag or environment variable to setup a prod or dev image, but I can't find any examples of people doing this and I am not sure how exactly it would be done.

I've also seen a few rough examples of projects with unique Dockerfiles for production and development, but then there is the issue of maintaining separate Dockerfiles which could diverge over time if we aren't careful.

Are both of these sensible or am I possibly misunderstanding something? I'm relatively new to docker. An example Dockerfile or project utilizing a similar setup would be great. I wary of dockerizing some of our services with bad practices at the start.

Edit: All my current apps are python base if that affects any responses.

like image 775
Shatnerz Avatar asked Jan 05 '18 15:01

Shatnerz


People also ask

Should you use the same Dockerfile for Dev staging and production builds?

🐳 Use the same Dockerfile for both local development and production with multi-stage builds. If you haven't read my previous post on how to use Docker for local development. I highly recommend you read it before this.

Can you have two Dockerfile?

As Kingsley Uchnor said, you can have multiple Dockerfile , one per directory, which represent something you want to build.

Can we have 2 base images in Dockerfile?

Using multi-stage dockerfiles, you can use several base images as well as previous intermediate image layers to build a new image layer.


2 Answers

A good aproach is to use Docker's multi-stage builds, since they will allow you to build your artifact inside an image that contains your dev dependencies and only use your artifact and runtime dependencies in the final image.

I'd generally advise against using different Dockerfiles for different environments, this should normally be achieved using configuration parameters (environment variables are a good solution).

Having a faster development feedback cycle depends IMO heavily on the used language. Many people will develop using an IDE, in a more classic fashion and only build the image for integration testing and so on (in my experience this is for example often the case for Java developers). Other interpreted languages might indeed profit from mounting your sources into a development environment image. In this case, you might incorperate this image into your multi-stage build.

like image 186
Kevin Wittek Avatar answered Nov 15 '22 09:11

Kevin Wittek


In addition to the great answer by Kevin Wittek, it turns out this may have been more of a non issue.

I can use COPY to copy the files to the image, but I can simply mount over top of them for development.

If there's other things like dev-only deps that need to get installed, I can use ARG to specify environments like like ARG APP_ENV=prod and overwrite that with --build-arg=dev or vice-versa.

like image 27
Shatnerz Avatar answered Nov 15 '22 08:11

Shatnerz