DEV Community

nithinalias
nithinalias

Posted on

Docker Compose deploy MERN Stack

Clone the MERN stack directory from github

https://github.com/sidpalas/devops-directive
Enter fullscreen mode Exit fullscreen mode

You can make MERN stack using the blog link shown below.

https://medium.com/swlh/how-to-create-your-first-mern-mongodb-express-js-react-js-and-node-js-stack-7e8b20463e66
Enter fullscreen mode Exit fullscreen mode

Edit the database file inside the MERN stack directory as shown below.

cd mern-docker-compose/
vim server/db/index.js


const mongoose = require('mongoose')

mongoose
    .connect('mongodb://mongo:27017/cinema', { useNewUrlParser: true, useUnifiedTopology: true })
    .catch(e => {
        console.error('Connection error', e.message)
    })

const db = mongoose.connection

module.exports = db

Enter fullscreen mode Exit fullscreen mode

If you are deploying MERN stack in Virtual Machine, you need to change localhost to ipaddress of virtual machine (baseURL: 'http://localhost:3000/api' to baseURL:'http://ipaddress:3000/api').

cd mern-docker-compose/
vim client/src/api/index.js


import axios from 'axios'

const api = axios.create({
    baseURL: 'http://localhost:3000/api',
})

export const insertMovie = payload => api.post(`/movie`, payload)
export const getAllMovies = () => api.get(`/movies`)
export const updateMovieById = (id, payload) => api.put(`/movie/${id}`, payload)
export const deleteMovieById = id => api.delete(`/movie/${id}`)
export const getMovieById = id => api.get(`/movie/${id}`)

const apis = {
    insertMovie,
    getAllMovies,
    updateMovieById,
    deleteMovieById,
    getMovieById,
}

export default apis
Enter fullscreen mode Exit fullscreen mode

Then build and run docker-compose file

make build
make run
Enter fullscreen mode Exit fullscreen mode

Top comments (0)