Dockerized Node.js app with MongoDB

1 min read

Enter docker-compose up command to pull node packages and run the app.

Code's hot-reload is provided by nodemon.

docker-compose.yml

version: '3' services: app: image: node:14 command: bash -c "yarn && yarn start" restart: unless-stopped volumes: - '.:/usr/src/app' working_dir: /usr/src/app ports: - '3300:3300' links: - mongo mongo: restart: unless-stopped image: mongo ports: - '27017:27017'

package.json

{ "scripts": { "start": "nodemon index.js" }, "dependencies": { "express": "^4.17.1", "nodemon": "^2.0.4" } }

index.js

const express = require('express'); const app = express(); const port = 3300; app.get('/', (req, res) => { res.send('Hello World!') }); app.listen(port, () => { console.log(`Example app listening at http://localhost:${port}`) });