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

  • docker-machine, create and run a nginx container is not exposing port 80
  • docker network in bridge mode
  • Nginx resolving system in Docker container
  • What causes this intermittent nix-build failure with error “Too many levels of symbolic links”?
  • Set the Perl environment correctly in dockerfile
  • Java process as non-root consumes 100% CPU, but as root is fine
  • 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?

  • Use sysctl to change kernel params in docker file,will the running container affect Host kernel
  • How to change timezone of postgreSQL 9.5 on docker?
  • docker 1.9.0 network support in mesos
  • MySQL is using way more memory that it is configured to within a Docker container
  • How to automate Grunt on a Docker image
  • How can I deploy to a specific app with heroku docker:release?
  • One Solution collect form web for “Starting an app fails”

    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.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.