1

EDITED - Added folder structure

I'm trying to run a node.js app in a docker container using docker-compose, however, the container fails to run the specified command and exits.

I'm running docker-for-mac, version 17.09.0-ce-mac35 (19611)

This is my folder structure:

.
├── docker-compose.yml
├── backend/
│  ├── Dockerfile
│  ├── package.json
│  ├── (source files)
├── frontend/
│  ├── Dockerfile
│  ├── package.json
│  ├── (source files)

This is my docker-compose file:

version: "3"
services: 
  ihm-backend:
    environment:
      - NODE_ENV=development
    build: ./backend
    volumes:
      - ./backend:/src
    ports:
      - "3000:3000"
      - "9222:9222"
    working_dir: /src
    command:
      - "./node_modules/.bin/nodemon --watch ./ --inspect=0.0.0.0:9222 --nolazy ./app.js"

And this is my Dockerfile:

FROM node:6-alpine

COPY ./package.json src/
COPY ./npm-shrinkwrap.json src/

RUN cd /src && npm install

COPY . /src

WORKDIR /src

CMD ["./bin/www"]

Notice that the docker-compose file overrides the command.

When I run it, I get the following error:

Cannot start service ihm-backend: oci runtime error: container_linux.go:265: 
starting container process caused "exec: \"./node_modules/.bin/nodemon --watch 
./ --inspect=0.0.0.0:9222 --nolazy ./app.js\": stat ./node_modules/.bin/nodemon 
--watch ./ --inspect=0.0.0.0:9222 --nolazy ./app.js: no such file or directory"

I tried to play with the command a bit - when changing it to ls or pwd I got the expected output. However, once I try to use npm or anything from the file-system like ls ./node_modules, I get the same error. Also, if I just start the container into a shell, I'm able to run any command I want, including npm scripts

4
  • Is nodemon defined in package.json and is it installed on docker image? Commented Dec 5, 2017 at 18:32
  • Yes of course. I also tried running through an npm script but that didn't work either Commented Dec 5, 2017 at 18:33
  • Can you please include your directory structure Commented Dec 5, 2017 at 20:41
  • @TJBiddle - added folder structure Commented Dec 6, 2017 at 9:35

2 Answers 2

1

I found the problem. Seems like there's an issue with having spaces in the command attribute of the docker-compose file. The solution was to split it down to an array like this:

version: "3"
services: 
  ihm-backend:
    environment:
      - NODE_ENV=development
    build: ./backend
    volumes:
      - ./backend:/src
    ports:
      - "3000:3000"
      - "9222:9222"
    working_dir: /src
    command:
      - /src/node_modules/.bin/nodemon
      - --watch
      - ./
      - --inspect=0.0.0.0:9222
      - --nolazy
      - ./app.js
Sign up to request clarification or add additional context in comments.

Comments

0

Not sure if this helps, but this is what I use for my node app and it works .
Dockerfile

FROM node:carbon

# Create app directory
WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install
RUN npm install nodemon -g

# Bundle app source
COPY . .

docker-compose

web:
  build: .
  volumes:
    - ".:/src/app"
  ports:
    - "3000:3000"
  command:
      - "nodemon"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.