Docker exited with code 0 on trying to use nodejs , nodemon and subfolder

Viewed 419

I have this setup which work fine but recently I moved all my codes to sub folder functions.

web get exited with 0
mongo run fine.

Note : I'm creating that subfolder functions because i'm trying to use firebase.

docker-compose.yml

version: "3"
services:
    web:
        build: .
        ports:
            - "8080:8080"
        links:
            - mongo
        volumes:
            - .:/usr/src/app
    mongo:
        image: mongo
        ports:
            - "27018:27017"
        volumes:
            -  mongodata:/data/db
volumes:
    mongodata:

Dockerfile

FROM node:latest

WORKDIR /usr/src/app

COPY functions/package*.json ./

RUN npm install

COPY . .

EXPOSE 8080

RUN npm install -g nodemon

CMD [ "nodemon", "functions/index.js" ]

.dockerignore

node_modules
npm-debug.log

enter image description here

1 Answers

I have checked the current situation like this:

docker-compose run web bash

In bash, i have checked folder structure with ls and they were coming from root folder. So even you copy your functions folder to docker-image it volumes override the docker-image folder.

I have tried to change web -> volumes value to - .functions:/usr/src/app in docker-compose.yml but it didn't work.

So finally I moved Dockerfile and docker-compose.yml files under functions folder. And changed the scripts like

Dockerfile

FROM node:latest

WORKDIR /usr/src/app

COPY . .

RUN npm install

EXPOSE 8080

RUN npm install -g nodemon
CMD nodemon "index.js"

docker-compose.yml

version: "3"
services:
  web:
    build: .
    ports:
      - "8080:8080"
    links:
      - mongo
    volumes:
      - .:/usr/src/app
  mongo:
    image: mongo
    ports:
      - "27018:27017"

In my terminal with this command:

cd functions && docker-compose -f ./docker-compose.yml up --build --force-recreate

It works fine but I would recommend you to not move your application under another folder and have a sub npm project. In future you will have different problems because of that like when you run your tests. You need to find a way to keep them in root folder.

Related