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
dockerfolders inside yoursclientandserverfolders - forward your specific environment variables in
docker-compose.yml; Note: use eitherenvironment:key with.envfile from the root folder orenv_file:key with separate.envfiles fromclientandserverfolders - client container handles all routes except server routes
/api, /auth, /public/imageswhich 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/imagesroutes 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.ymlinstead 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=adminto connection string - Important: must delete files in
server/docker/mongo-datavolume
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.ymlmongo and server services
networks:
- default- Solution 2: append
&directConnection=trueto connection string
- tutorial and repository KaranJagtiani/MERN-Docker-Production-Boilerplate by Karan Jagtiani
- tutorial Dockerizing a React App by Michael Herman