Search code examples
nestjs

Easiest way to deploy nestjs app with postgres and frontend app


I have a simple Nestjs api in the backend that works with Postgres and in the frontend a simple HMTL/JS app that communicates with Nestjs api. Everything is in the main Nestjs project. Frontend app uses ngix docker container to run frontend and share files. Postgres is also a docker container and I also use the adminer container for my database. Only my nestjs api is actually installed on the local machine with all needed npm modules from the package.json. The structure is:

nestjs-api with docker-compose 
--> for postgres-container
--> for adminer-container 
--> and staring frontend-container
nestjs-api/frontend with Dockerfile
--> for nginx and sharing frontend files

This is my first working mini application and I have never deployed a Nestjs application anywhere. On the computer, I start all containers via docker-compose up and then the Nestjs app with npm run start:dev. It is simple and I am looking for a way to deploy my app without having to worry about server configurations. Preferably it should be as simple as on my computer. I pushed my code to a private GitHub repository.

AWS EC2 looks too complicated, the solution from Google or Microsoft will probably be similarly complicated. Railway app seems much easier and many also use Heroku. However, it is unclear to me whether I can use my docker-compose file and the docker container there in the same way as on my local computer? Or do I need to build my application differently? I want to be able to use the app over the internet when my computer is switched off. What do I have to consider and is there a provider who makes it as easy as I imagine?

Update from me:

I managed to get everything working on Railway.app. However, I had to create a separate postgres database there. I now use environment variables to switch between local docker postgres and real posgres on Railway. I also dockerized my entire Nestjs api with a Dockerfile. That's probably why it worked. I'll have to research what exactly a Dockerfile needs later, so I don't copy too much. In addition, I had to create my frontend app as a new project, remove it from my Nestjs project and move it to a new repository. I guess that's how it's generally done? If that was correct and is common practice, then my question is answered.


Solution

  • The solution was Railway.app (PaaP). Connect Railway.app with your GitHub repository. Use Dockerfiles - Railway.app detects all Dockerfile in the main folders of the projects. Build a Docker image from Nestjs Api with Dockerfile. Separate Postgres and Frontend from the Nestjs Api. Install Postgres as a separate DB on Railway.app. Move the Frontend to a new repository and move it to Railway.app as a new application using another Dockerfile for NGINX (COPY . /usr/share/nginx/html).