ekkis
ekkis

Reputation: 10226

Starting an app fails

I have a node.js app that compiles the runtime version into the dist folder. therefore the package.json file specifies the start script node dist/index.js.

I now want to containerize it but the container doesn't need to have the distribution directory and those files really should live in the root of the app, thus my Dockerfile contains the lines

COPY package.json /usr/src/app/
COPY dist/* /usr/src/app/

which places the runtime files in the image. the problem I have is that when the docker file issues its last command:

CMD ["npm", "start"]

it fails because the index.js is now in the wrong location (because it's looking for it in the non-existent distribution directory). I could solve the problem by issuing:

CMD ["node", "index.js"]

instead but that seems like the wrong thing to do. what is the correct approach?

* update I *

I had modified the index.js so it could run from the root of the project (i.e. it expects to find the resources it needs in the dist/ folder) by issuing a node dist/index.js, but of course, this is now also a problem since there is no distribution directory. how is this generally approached?

Upvotes: 0

Views: 73

Answers (1)

Peter Lyons
Peter Lyons

Reputation: 145994

I would code all your javascript require calls relative to the current file with no reference to the "dist" directory. So let's say you have index.js and routes.js in the project root. index.js loads routes via var routes = require('./routes'). When you compile these, compile BOTH of them into the dist directory and all should be well.

If you want to run via npm start from the project root, you can configure that to do node dist/index.js.

For docker, there's no reason to use npm at all to launch your app. It's an unnecessary process executing for no benefit. Just launch via node index.js in your dockerfile with WORKDIR /usr/src/app.

Upvotes: 2

Related Questions