MEAN Stack using Docker containers - node.js

New to this...
I'm trying to understand if a modern MEAN app should be deployed with 3 or 2 Docker containers:
Option 1: Express Server as container + Mongo DB as container
Option 2: All three as separate Docker containers
The second option sounds like the appropriate path so you can update any part of the stack without taking down other components if you don't want to. But then the question is does the ng app container need it's own server to serve the ng app files. I'm seeing some examples on Github where they are running the ng app with ng serve -H 0.0.0.0 from the Docker container which from my understanding is a no-no because that's not a prod ready server, just webpacks dev server.
To me, if you run all three separately then you actually need two servers, one to server the ng app (index.html, js, css, etc.) and the other to sever the backend app, the API.
The advantage I see if you run the Express Sever + ng app in one container then you can serve the initial index.html with ng app dependencies AND the API but then they both go down when they get updated.
What's the best practice here?

IMHO 2 Containers seems like the better solution with one for Mongo and one for Express. Anytime you're pushing new code, it doesn't make sense to have a front end still up if back end is down or vice versa. Also serving front end files from the same server reduces headaches of dealing with CSRF.
Regarding your other question, I think you can deploy your front end to something like AWS S3 and still only manage one server for your backend.
on a side note, you could also do it all in one container. It really depends on your other requirements to figure out the best architecture.

Related

Deploying an app made on MEAN stack to Nginx on docker

I am currently learning about docker and have read a few conflicting articles about the process so thought I would ask the oracles here for answers.
Having setup my app I use Node as the server, which handles the routing and API connection to Mongo just fine on my localhost, a few articles have said that you do not use Node on production so one of my friends followed a tutorial that introduces nginx to the equation
While his app loads the static content, he has lost the dynamic side of things, items in the database are not loading in for example.
First question is, does nginx take over from Node or add to it in these instances?
To me it seems that node is still involved to serve the content so express and mongo should also still work and my hope is theres some config that we haven't found that's the blocker.
Second question is, if my assumption is correct, and nginx is simply being introduced as well as Node, is it the server, is node the server or am I missing the relationship between them somehow. The tutorials and articles usually refer to nginx as the server hence the confusion.
My end goal is to work out how to use my Linode lab, setup docker and then have my angular app running in a publicly accessible way. I think I have the docker side of things worked out but my head isn't figuring out how things work after that with node and nginx - especially when my previous experience is with regular old servers with php and sql.

How to package MEAN stack application as docker container with nginx?

I've made an app using Angular, NestJS (Node.js) and MongoDB and I'm wondering how to easily turn it into a simple docker container, which will contain nginx sever for serving the frontend app and reverse proxy for the backend + the MongoDB. Also it would be cool if there was option for automatic Let's encrypt cert renewal etc.
Is there some pre-made package/template I could just clone, replace the app files and immediately just run? If not, I'd appreciate at least link to some guide on how to build such container myself - It's probably not super hard, but I've never really built anything with Docker (I do have some basic knowledge how it works) and my nginx experience is also very limited...
The expected result should be all-in-one docker app that I can easily provide to anyone, so they can just easily run it with something like docker run -p 80:80 image.

Vue.js on local, NGINX docker container, how to save files with Node?

I apologize if this is a general question, but apparently what I'm googling doesn't make sense, and I don't have anyone at my work I can ask.
Here's my situation:
Simple Vue.js app created with the Vue CLI 3.5, with an <input type="file">. Nothing hooked up yet when the form submits.
Docker container with Node and NGINX that pulls in and builds my Vue app - this is working. I basically copied the Dockerfile right from the vue.js site.
Now I need to store the file from the web app (and eventually FTP it to another server).
It was suggested I use Node, but I'm new to all the server-side stuff, so I don't know how to do that. My container has both Node and NGINX in it running the app, so can I stick with one container? And how do I build locally for development and then transfer that to the Docker image?
I'm just looking for pointers/articles/tutorials to help me think about this the right way.
Attention opinion:
In my experience NGINX is more of a router managing traffic.
Its in the name of Node to make a single one per task. So () i think its uesfull to split those.
Node is similar to "frotnend" except you have access to common system apis - wrapped by node.
But basically like you consume rest or graphql apis you can do with node.
Best is to simply peek into the basics you most likely gonna need:
file system, stream,...
https://nodejs.org/api/

how to dockerize my nodejs express application hosted on amazon linux ami?

1.my technology stack for above application is expressjs, nodejs, mongoDB, redisDB, s3(storage).
2.API is hosted on Linux AMI
3.I need to create docker container image for my application.
First of all you will need to decide to either keep everything inside a single container (monolithic, cannot really recommend it) or separate the concern and run a separate express/nodejs container, a mongodb container, and a redisDB container, s3 is a service you cannot run for yourself,
If you chose the later approach, there are already officially supported images on the docker hub for redis, and mongo, now for the actual app server (node) you need to set express as a dependency on node and start the official node image with an npm install command (which would get express on it) and then npm start (or whatever command you use for it), dont forget to include your code as a volume for this to work,
Now, bear in mind that if your app uses any reference data inside mongodb, you should make sure to insert it when the mongodb container starts or create an image based on the official mongodb that already has said data on it!
Another valuable note is that you should pass all connections inside your expressjs app as env vars, that way you can change them when deploying your app container (useful for when you distribute your system accross several hosts),
At the end of the day you would then start the containers in this order: mongodb, redis, and node/express. Now, the connection to s3 should already be handled inside your node app, so it is irrelevant in this context, just make sure the node app can reach the bucket!
If you want just to build a monolithic container, just start with a debian jessie image, get a shell inside the container, install everything as you would on a server, get your code running and commit the image to your repo, then use it to run your app, Still i cannot recommend this approach at all!
BR,

docker-compose / nginx / SPA

I want to use docker-compose with two containers, a nginx and another one that have a node.js application. The node.js application is a Single Page Application and an API express server.
I want that nginx serves the static files from the SPA. The problem is that my app container compiles the SPA when it starts and then I do not have the files for the nginx container.
I do not want to create a data volume for it, as I want the "composed" environment to not depend on external state.
I think something on a transient volume (a volume that is started with docker-compose up) and then removed, but this feature seems to not exist.
Another way be to serve the static files by NFS in the app container and let nginx read them, but not sure about how good or bad would be this.
What's the best practice to run this environment?

Resources