This is dockerized Mern repo template (Create React App, Express, MongoDB, Nginx, pm2, Traefik) meant for production environments and client side rendering where static assets are served by Nginx. It uses strategy with two separate images for client and server. Both client and server containers are then routed to subdomain and exposed through Traefik reverse proxy.
- copy files from the root folder to yours project root folder
- copy
docker
folders inside yoursclient
andserver
folders - forward your specific environment variables in
docker-compose.yml
; Note: use eitherenvironment:
key with.env
file from the root folder orenv_file:
key with separate.env
files fromclient
andserver
folders - client container handles all routes except server routes
/api, /auth, /public/images
which are handled by server container. This is needed i.e. for Facebook and Google OAuth that need to access server routes directly. Same/api, /auth, /public/images
routes are ignored by Nginx inclient/docker/default.conf
. You can customize this to your own needs. - Important: you want to build images locally and push them to Docker Hub and pull images on server in
docker-compose.yml
instead of building images directly on server. This is needed because to build images you need 4GB+ RAM while 0.5GB is usually enough to run your app. - run with
docker-compose up -d
- https://github.com/nemanjam/traefik-proxy - here you can find complete Traefik deployment with Mern example on this path traefik-proxy/apps/mern-boilerplate
- this template is used to dockerize this example Mern app mern-boilerplate
# run from traefik-proxy folder
scp ./apps/mern-boilerplate/.env ubuntu@amd1:~/traefik-proxy/apps/mern-boilerplate
- Solution: append
?authSource=admin
to connection string - Important: must delete files in
server/docker/mongo-data
volume
sudo rm -rf ./server/docker/mongo-data/* # doesn't delete files with .
sudo rm -rf ./server/docker/mongo-data/.mongodb
- example of a working MongoDB url:
MONGO_URI_PROD=mongodb://username:$password@mongo-service:27017/db-name?authSource=admin
- Solution 1: add default network in
docker-compose.yml
mongo and server services
networks:
- default
- Solution 2: append
&directConnection=true
to connection string
- tutorial and repository KaranJagtiani/MERN-Docker-Production-Boilerplate by Karan Jagtiani
- tutorial Dockerizing a React App by Michael Herman