Best practices for using Docker in development vs production (NestJS + NextJS monorepo)

Hi everyone, how are you?

I have basic knowledge of Docker (I understand what it’s for and the general idea), but I’ve never used it professionally, and I’m running into some doubts.

I’m working on a project that includes:
• Backend: NestJS, Prisma, PostgreSQL, BetterAuth, etc.
• Frontend: ReactJS, NextJS, BetterAuth, etc.

The idea is to put everything in a monorepo, but keep backend and frontend separated.

About Docker:
• I created a Dockerfile and a Dockerfile.dev to manage separate environments.
• My initial idea was to have everything (Node, Prisma, PostgreSQL, etc.) inside the Docker image, with nothing installed locally, so I could develop and later migrate the same image to production without differences.

But I’m getting confused:
• When I install a new dependency locally, it’s not always reflected in the Docker image.
• Sometimes I end up doing things locally and other things inside Docker, which gets messy.

So my question is:
:backhand_index_pointing_right: In a professional environment, what’s the most common approach?
• Do you develop everything inside Docker from day one?
• Or do you work locally and use Docker only for staging/production?

I know there’s no single right answer, but I’d love to hear your experiences and recommendations, especially considering the technologies I’m using.

Thanks in advance!

Docker is for isolation, usually separation of concerns is applied. I would use one container for frontend, one for backend, one for database.

If you want a quick turn-around during development, you could use a bind-mount of the sources into the container, so you don’t need to rebuild it for every change.

Usually you would use a watch on file changes during development, make sure to include package.json to reload when packages are changed. You probably also need to reload when db models change.

Make sure to use multi-stage Dockerfiles, create a builder with all the tools needed, then create a final stage with the minimum required.

We use the same Dockerfile, mostly the same compose file for dev and prod, only .env files are changed.

Is it ever?

These “best practices” topics are often unanswered as best practices topics are opened either spambots or beginners not really knowing much about the technology and jump to the “Best practice” part. I recommend learning more about containerization in general and I would search for best practices online as I’m sure many sources are already available. If you have specific questions, even if it is about a specific concept, then the forum could be a great place to ask about it.

Recommended links to learn the basics and concepts:

Hi!! Thank you so much for your reply, it really helped me get a much clearer direction.
At first, I was thinking about that same setup (one container for the backend, another for the frontend, and another for the database).
I’ll put your advice into practice, and if I run into any more questions, I’ll come back to ask.

Thanks again!

Hi! Yes, I understand. I’m starting a new project that requires Docker and, as I mentioned, I have used it before and I understand the basics of the technology, but I still need to go deeper and learn more best practices to make the most out of it.

Of course, I know I need to strengthen the fundamentals, but I also believe it’s valid to learn while practicing in a real project — that’s my approach :slightly_smiling_face:.

Thanks a lot for your advice and for sharing the resources, I’ll definitely check them out!

Sorry, I often ignore the “I know the basics” part and come to a conclusion based on the questions. The part I quoted in my previous reply made me think you expected something installed outside of images exist inside an image or container, which is not the case and it is one of the main features of containers in general.

Absolutely. But best practices are too general questions and discussed by blogposts, so a more specific question gets you quicker and probably better answers too.. Also when it comes to specific tools, languages, best practices are often better known by people using the same language, so a NextJS community for example. If I search for “Docker NextJS Best practices”, I find blogposts like this:

I also found this:

The author is a Docker Captain.