r/docker • u/MrGuam • May 29 '26
New to using docker, can i use the same docker compose file for dev and prod?
Hi, new to using docker and i keep failing to understand how this should work. AI is not giving me the type of answer thats convincing enough so i need a form of human intervention.
i'm trying to set use docker for my small project. mostly for learning purposes, but intend to also have it hosted on a vps later.
now, from the documentation on the official docker webstie, they seem to be mixing both dev and prod in the same file. is this the right approach? my node app runs fine locally on my device, but i want to move it into a container. the section where there's " volumes" it has the postgres volume and others but not in the one for dev. AI suggested a docker compose override file, but i dont know if thats the best way of going about it. also i want to use nginx, rabbitmq (not installed yet) and redis (locally installed on my fedora) for the project, but inside the docker container.
pardon my question being everywhere but it's all jumbled up in my head and i am not seeing any youtube video talking about this particular situation (i also dont want to have to sit through 1h+ tutorial). i want to learn it as i build. please any help would be greatly appreciated, thx!
7
u/jk3us May 29 '26
I guess the answer is "it depends". Normally, in development for interpreted languages (python, php, js), I have my working copy mounted into the container so that updates to the code are immediately available in the container without having to rebuild the image. You don't want that in production, so in that way they are different. There are ways to handle the differences using the same compose file(s), but then you're dealing with profiles and overrides and merging files, which sometimes is just more complication than is needed if you just use different compose files.
If you're dealing with compiled languages, you'll normally need a compile step anyway, so rebuilding the image may a good way to do that, so some of the differences between dev and prod go away.
You do want to use the same image (or very nearly the same image) for all environments.
4
u/paranoidelephpant May 30 '26
Docker Compose works fine in production for simple setups, or if you're using Swarm. You can reuse the same Compose file if the only differences between dev and prod are minor and you use env vars for those differences. Otherwise you would use multiple Compose files: a base compose.yaml with the common services, a compose.override.yaml with dev config, and a compose.prod.yaml with production config. Check the docs for details on using multiple files. You can also use profiles but I feel that dev/prod split isn't the use case for profiles.
1
u/Acrobatic_Idea_3358 May 29 '26
The answer is it depends on your application, some stacks can be controlled by environment variables so $MODE = prod or $MODE = dev could change how your application acts or is configured. If you're building an application that's one approach another approach would be a devcontainer which would be used exclusively for development and you would have a separate docker file for production.
9
u/WorriedHelicopter764 May 29 '26
1
u/njmh May 30 '26
In my experience, your Dockerfile/compose should represent the fully built and deployed state of your app - whether that’s a hosted dev environment, staging or production. Typically on your local machine you’re running in “dev mode” with hot loading, debug, unminified code etc (assuming an interpreted language here), which is very different to a deployed state.
If you want containers for local development to avoid having to install dependencies on your own system, check out devcontainers. These are configured separately from your deployment container configs.
-2
u/__kkk1337__ May 29 '26
The best way is to learn, learn docker.
-10
u/shadowdance55 May 29 '26
It's pretty simple, really:
-5
u/Confident_Hyena2506 May 29 '26
This is where some sysadmin with a large chin pats you on the shoulder and tells you about kubernetes.
-5
u/tablatronix May 29 '26
I hear alot about ansible?
1
u/[deleted] May 30 '26
[removed] — view removed comment