0

I am trying to get a nodejs app to run in a container. I have followed the tutorial here: https://nodejs.org/en/docs/guides/nodejs-docker-webapp/ and it works for the example. But, I do not know how to convert my start up command "yarn start" to be triggered by server.js.

All of the examples of dockerizing a nodejs app that I have found online use this server.js approach. In the tutorial linked above the container ends up running a simple "Hello World!" example app. I have tried putting

CMD [ "yarn", "start" ]

in my Dockerfile but it does not launch the app.

This is my package.json file:

{
  "name": "my-server",
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "start": "concurrently \"cd api && yarn startd\" \"cd admin && yarn startd\"",
    "dev": "concurrently \"cd api && yarn dev\" \"cd admin && yarn dev\"",
    "logs": "heroku logs -t",
    "heroku-postbuild": "cd src/client && npm install && npm run build"
  },
  "devDependencies": {
    "concurrently": "^4.1.0",
    "nodemon": "^1.19.4",
    "prettier": "2.7.1",
    "prettier-plugin-apex": "1.10.0"
  }
}

Question is: How do I get server.js to do the equivalent of "yarn start"?

server.js (modified to open two ports):

'use strict';

const express = require('express');

// Constants
const PORT1 = 3000;
const PORT2 = 4000;
const HOST = '0.0.0.0';

// App
const app = express();
app.get('/', (req, res) => {
  res.send('Hello World from my-server');
});

app.listen(PORT1, PORT2, HOST, () => {
  console.log(`Running on http://${HOST}:${PORT1}`);
});

Thank you!

fvlasie
  • 73
  • 10
  • I'm a little confused by the flow here; I'd normally expect `yarn start` to invoke your Javascript code, and not _vice versa_. The `package.json` fragment suggests there are actually two separate applications in this setup, so could it be more straightforward to run them in two separate containers? – David Maze Jan 13 '23 at 18:40
  • `yarn start` or `npm start` is what I would use to start the apps manually. I could separate the apps into two containers but I still need to initiate `yarn start` somehow from the Dockerfile. Thanks for your comment! – fvlasie Jan 13 '23 at 19:07

0 Answers0