1

I am trying to setup a docker image for my local serverless development and I'm having issues reaching the files using volumes.

Dockerfile

FROM node:8.10

ADD . /code
WORKDIR /code


RUN npm install -g serverless
RUN npm install serverless-offline

EXPOSE 3000

# COPY . /code

CMD ["serverless", "offline", "--host", "0.0.0.0", "--port", "5000"]

docker-compose-yml

version: "3"
services:
  serverless_proj_1:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "3000:5000"
    volumes:
      - .:/code
      - /code/node_modules

Docker is listening to all my serverless endpoints correctly:

enter image description here

But when i'm triggering one of the api endpoints from Postman this is the error I'm getting: enter image description here

  • docker container exec apps-services_serverless_proj_1_1 pwd returns /code
  • docker container exec apps-services_serverless_proj_1_1 ls -al returns my codebase
  • docker container exec apps-services_serverless_proj_1_1 ls /code -al again returns my codebase(both commands have same total)
  • docker container exec apps-services_serverless_proj_1_1 ls /code/node_modules -al returns all my dependencies(total 3074)
Arlind
  • 347
  • 2
  • 15
  • What's your intent with this volume - /code/node_modules? That doesn't seem right. – Robert Moskal Mar 05 '20 at 16:47
  • Its how you define a placeholder that tells docker not to map that folder back to the volume but use the folder as defined within the container. It should be fine. – Damo Mar 05 '20 at 17:39
  • It’s a directive to Docker that the directory contains valuable user data and must not ever be updated. As a side effect, the first time you run a container, it will be populated from the image, but it will never be updated again, even if you change the `package.json` file. I’d recommend deleting the entire `volumes:` section and using the code built into the image. – David Maze Mar 05 '20 at 18:04

1 Answers1

1

I don't see an RUN npm install. since you are marking a place holder for the node_modules you will need to install them inside the container.

EDIT

Please try attaching to your running docker container and looking in the node_modules folder. Just to double check that a) you have one, b) it is located where it should be and c) it contains all the modules you expect.

You can do this by running docker container exec -it serverless_proj_1 /bin/bash this should put you in the /code dir by default, then just run ls -al

Damo
  • 5,698
  • 3
  • 37
  • 55
  • Its still the same, its just pointing to a different missing file. Actually everytime im calling the API its pointing to a different file saying that its missing **'Error: ENOENT: no such file or directory, lstat \'/code/...** – Arlind Mar 05 '20 at 22:08
  • Any reason we can't see the actual missing file? I think the best way anyone can help is if you put a slimmed down example on github that exhibits the same behaviour – Damo Mar 05 '20 at 22:34
  • **[ 'Error: Cannot find module \'./src/RenameProject\'',** **[ 'Error: Cannot find module \'../../config/config\'',** I'm reproducing this when I'm importing with "require" exported functions from different files. But this is weird because in this case I'm not importing this file called "RenameProject" I'm importing something different entirely. And everytime I'm calling this lambda its spitting a random file from the codebase that it cannot find. – Arlind Mar 06 '20 at 09:43
  • Could you please check your node_modules, see my latest edit. – Damo Mar 06 '20 at 11:00
  • Getting this error when executing the docker command you mentioned: **unknown shorthand flag: 'i' in -it** – Arlind Mar 06 '20 at 12:00
  • Ever so sorry there was a typo in the command, try again please – Damo Mar 06 '20 at 17:19
  • No worries. I executed the command and my whole code base was in there when i ls-ed, node_modules was populated with all the dependencies as well. I wasn't in the "code" dir though. It's called **root@:/service** – Arlind Mar 06 '20 at 22:05
  • That seems wrong to me, can you amend your question with more information showing what the output of the following commands produced: `docker container exec serverless_proj_1 pwd` `docker container exec serverless_proj_1 ls -al` `docker container exec serverless_proj_1 ls /code -al` `docker container exec serverless_proj_1 ls /code/node_modules -al` – Damo Mar 09 '20 at 10:35
  • Could it be that when I'm starting the container with "docker-compose up" the container name that's activating is called "apps-services_serverless_proj_1" instead of "serverless_proj_1" that I specified in my docker-compose file? And if yes, why is that happening... – Arlind Mar 09 '20 at 22:20
  • Here is something to try if you will. Remove node_modules from your host machine before running `docker-compose up`. Now run the commands to see if there is a node_modules folder in the /code folder – Damo Mar 10 '20 at 17:29
  • So after I deleted node_modules on my host, and ran `docker-compose up` it was creating an empty dir node_modules in my host. And when I ran the commands that you mentioned above they returned the same results, the node_modules in the /code dir had all the dependencies. – Arlind Mar 11 '20 at 09:37
  • one last thing to try before im all out of ideas. add a `RUN npm install` right after the `WORKDIR /code` line of the docker file. Then see if this makes any difference to the results in postman – Damo Mar 11 '20 at 10:50
  • I actually added the RUN npm install in my dockerfile after you wrote in the first comment. But still the same. I will try and use a different node base image maybe that will do something. Thanks for all the answers btw. – Arlind Mar 11 '20 at 10:55