Gangstead
Gangstead

Reputation: 4182

WSL Node path not found? Volume seems to be mounted

I'm trying to set up a unit test run time through docker compose. When I try to run an npm script through docker-compose Node is acting like it can't find the modules on the path:

➜ docker-compose run --rm server npm run test
Starting redis ... done
Starting mongodb ... done

> [email protected] test /server
> mocha --recursive tests

sh: mocha: not found
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! [email protected] test: `mocha --recursive tests`
npm ERR! spawn ENOENT

I've confirmed that the files are being mounted into the container, so why can't Node find them?

➜  dc run --rm --service-ports server ls node_modules/.bin | grep "mocha"
Starting redis   ... done
_mocha                  mocha

The script in my package.json are very basic:

    "test": "mocha --recursive tests",
    "build": "gulp default:dev",

docker-compose.yml

version: '3' #compose version
services:
  server:
    build:
      context: .
      dockerfile: Dockerfile.test
    ports:
     - "3000:3000"
    volumes:
     - ".:/server"
    working_dir: "/server"
    depends_on:
      - mongodb
      - redis
    environment:
      PORT: 3000
      NODE_ENV: test
  mongodb:
    image: mongo:latest
    container_name: "mongodb"
    environment:
      - MONGO_DATA_DIR=/data/db
      - MONGO_LOG_DIR=/dev/null
    volumes:
      - ./localdata/db:/data/db
    ports:
        - 27017:27017
    command: mongod --smallfiles --logpath=/dev/null # --quiet
  redis:
    container_name: redis
    command: redis-server --requirepass testredispassword
    image: redis
    ports:
      - "6379:6379"
    volumes:
      - ./localdata/redis:/data
    entrypoint: redis-server --appendonly yes
    restart: always

The Dockerfile.test is different than the prod dockerfile in that it doesn't install / build the front end of the app or pass in any versioning info. I'm trying to make it build quicker with just what it needs to run the unit tests for the server:

FROM node:8-alpine

RUN apk update \
    && apk --update --no-cache add \
        git \
        python \
        build-base

ADD ./ /server WORKDIR /server

RUN rm -rf node_modules && npm install && npm run build

I think this is all pretty straight forward and I've done similar set ups before but on Docker for Mac. This time I'm running Docker For Windows and running the commands through WSL. I've got the drives shared and bound /mnt/c to /c.

For another reference this project I can run the unit tests on Docker for Mac, but get the same sh: mocha: not found when running it through WSL connected to Docker for Windows on Windows 10. It seems to be just the path to the binaries node_modules/.bin that's not found because I can start the project up without any errors, it just can't find any binaries like mocha, nsp, gulp etc...

Upvotes: 0

Views: 1159

Answers (2)

user1718229
user1718229

Reputation: 11

I encountered a similar situation and it had to do with symlink creation in WSL/Docker. According to this Github issue reported to MS, all you should have to do is enable Developer Mode in the system settings.

Edit: This Microsoft article describes how to enable developer mode on your Windows 10 machine.

Upvotes: 1

nateleavitt
nateleavitt

Reputation: 1250

Sounds like this is similar to a path issue I've experienced with a windows/wsl environment. Try changing your volume definition to the full path and see if that solves it.

volumes:
 - /c/Users/username/Code/server:/server

Even though you've copied the files into the container with the Dockerfile, when mounting a volume with docker-compose, it doesn't really care about that. You essentially change the mapping of the source of the directory.

Upvotes: 1

Related Questions