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.
🐳 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.
As Kingsley Uchnor said, you can have multiple Dockerfile , one per directory, which represent something you want to build.
Using multi-stage dockerfiles, you can use several base images as well as previous intermediate image layers to build a new image layer.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With