0

I don't know if my question is stupid but, after hours crushing my brain on it, I prefer to ask you.

I'm trying to run NPM on a Docker container (windows). I don't want a real "node server" ; I just use NPM to run utilities like gulp, webpack, browserify, vue.js...

So I added this in my ./docker-compose.yml file :

services:    
  node:
    build: docker/node
    environment:
      - NODE_ENV=dev

Until here, everything sounds good in my head. Now here is the content of my ./docker/node/Dockerfile :

# See https://github.com/nodejs/docker-node#dockerfile
FROM node:6

EXPOSE 8080

USER node

# set the working directory
RUN mkdir /home/node/app
WORKDIR /home/node/app

# delete existing modules and re-install dependencies
COPY package.json /home/node/app/package.json
RUN rm -rf node_modules
RUN npm install

# launch the app
# EDIT : I removed this line to solve the issue. See answer.
CMD  ["npm", "start"]

To create it, I just followed official tutorials. And then, here is my ./docker/node/package.json file :

{
  "name": "custom-symfony-project",
  "version": "1.0.0",
  "dependencies": {
    "gulp": "^4.0.0"
  },
  "devDependencies": {
    "gulp": "^4.0.0"
  }
}

I also have 3 containers : PHP, MySQL and NGINX but they are independants and they all start correctly, so I don't thing they are the pain of the issue.

So I run my docker-compose build : everything works fine.

But when I run docker-compose start I got thing in my Node container logs :

npm ERR! missing script: start

I tried to add an empty server.js but the container doesn't start.

So my question is : do I really need to start something ? Do I need a server.js ? I don't what to put into it.

When I was using npm with Ubuntu, I've just never specified a start script..!

Thanks !

Pete_Gore
  • 594
  • 1
  • 5
  • 20
  • 1
    If you're going to have `CMD ["npm", "start"]` then yes, absolutely you need a `start` in your `scripts`, because *that's what that's trying to call*. If you don't actually have anything to run, why have that command in the Dockerfile? – jonrsharpe Jan 17 '18 at 17:47
  • Possible duplicate of [Start script missing error when running npm start](https://stackoverflow.com/questions/31976722/start-script-missing-error-when-running-npm-start) – Sardorbek Imomaliev Jan 17 '18 at 17:48
  • Ok so maybe my question can be asked differently : in which cases do I need a start script ? Or : when do we need to start Node ? Because I tried to remove this `start` command : the error disappears, but the docker container doesn't start... When I run `docker-compose ps` it's not "up". – Pete_Gore Jan 17 '18 at 18:04
  • Edit : the `docker-compose ps` returns an `Exit 0` for this container. – Pete_Gore Jan 17 '18 at 18:10

2 Answers2

1

Containers are designed to run as long as the process they support is running and a container should run only one process. In your case, you are removing the CMD line, which is starting the process the container supports, so the container has nothing to do and just shuts down immediately.

You should think about your Docker container as a process, not a VM (virtual machine). A VM would have Node and other dependencies loaded and it would be ready to run commands any time you log into it, but a container spins up to run one command and then shut down.

It sounds like you want this container to spin up, run Gulp, then shut down. If that's the case you can use a CMD line like this (assuming you install gulp globally within the Dockerfile):

CMD ['gulp']

Or maybe you want it to spin up and watch for changes using gulp-watch? In that case, the CMD should be something like this:

CMD ['gulp', 'watch']

If you go with either option, note that Gulp will build the files within the container and not on your host filesystem unless you use a bind mount. A bind mount will allow your host filesystem to share a directory with the container and facilitate one or two-way updates to files.

karllhughes
  • 300
  • 2
  • 13
  • Thanks for your answer. If I understand correctly, I should divide my Node container is as many containers as I got npm modules ? One container for Gulp, one for Vue.js, one for browserify, etc..? By doing this, I think I'm loosing the main interest of NPM ? – Pete_Gore Jan 19 '18 at 14:36
  • 1
    The great thing is that you can override the container's command when you run it, so you can run many different containers from the same image (it may be helpful to see [this answer](https://stackoverflow.com/questions/23735149/docker-image-vs-container) on the difference between an image and container). For example, to run the image as a container using the Gulp command, you would do something like: `docker run my-image gulp`. And to run it with gulp watch, you would just change that last bit of the command: `docker run my-image gulp watch`. – karllhughes Jan 20 '18 at 15:04
  • Thank you SO MUCH I understand a big point there ! So if I'm right, I can create one Node image, and then in my `docker-compose` file I create (for example) 2 services using this image, and with the `command: gulp` line and the other with `command: gulp watch` ? – Pete_Gore Jan 20 '18 at 19:16
  • 1
    Glad it helped! Two commands like that should work, but obviously the `gulp` one will just run the command and then exit, killing the container. The `gulp watch` one should stay running until you run a `docker-compose down` or `stop` though. – karllhughes Jan 20 '18 at 22:32
  • Yes I was an example. In my case I will prefer to use one commande for Gulp, and another for Vue.JS for example. Thanks again ! – Pete_Gore Jan 21 '18 at 09:16
0

Ok so I remove the CMD line into the Dockerfile but the container just stopped naturally.

So I added the tty: true option into the docker-compose.yml file in order to keep the container active even if nothing's currently running on it, and for the moment it seems to work :

node:
    build: docker/node
    environment:
      - NODE_ENV=dev
    container_name: symfony4-windock-node
    tty: true
Pete_Gore
  • 594
  • 1
  • 5
  • 20