I would like to run a binary npm file in a container which was installed by another container.

I understand that it is possible using a shared volume.

I have the following configuration:

docker-compose.yml

version: '3'
services:
  cypress:
    build:
      context: .
      dockerfile: Dockerfile-cypress
    volumes:
      - ./node_modules:/app/node_modules
  test:
    build:
      context: .
      dockerfile: Dockerfile
    depends_on:
      - cypress
    environment:
      - ENV=development
    volumes:
      - ./node_modules:/app/node_modules

Dockerfile-cypress

FROM cypress/base:10

WORKDIR /app

COPY . /app

RUN npm install uuid
RUN $(npm bin)/uuid

Dockerfile

FROM node:10.13

COPY . /app

WORKDIR /app

RUN $(npm bin)/uuid

When running docker-compose build it fails because:

Step 5/5 : RUN $(npm bin)/uuid
 ---> Running in 1d86293ea47c
/bin/sh: 1: /app/node_modules/.bin/uuid: not found
ERROR: Service 'test' failed to build: The command '/bin/sh -c $(npm bin)/uuid' returned a non-zero code: 127

What is wrong in my configuration that the volume is not available in the second container?

up vote 1 down vote accepted

Volumes are shared only at runtime, not in build time.

Every RUN instruction works in build time, at this moment the volumes are not available.

You should put those instructions in CMD or an ENTRYPOINT instruction. In this case, because the application is simple, we don't even need the dockerfiles.

version: '3'
services:
  cypress:
    image: cypress/base:10
    volumes:
      - ./node_modules:/app/node_modules
    working_dir: /app
    command: /bin/sh -c 'npm install uuid && chown -R your_user_uid:your_group_uid /app && $$(npm bin)/uuid'

  test:
    image: node:10.13
    working_dir: /app
    environment:
      - ENV=development
    volumes:
      - ./node_modules:/app/node_modules
    command: /bin/sh -c '$$(npm bin)/uuid'

In this case it is better if you run the test command after the execution of cypres has finished, because you have no way of knowing if the binary is already available if you run them at the same time.

I recommend you create a script in bash like this:

docker_up.sh

docker-compose run cypress
docker-compose run test

Then:

./docker_up.sh

NOTES:

chown -R your_user_uid:your_group_uid /app

Above code is important because you could have permissions problems working with shared volumes. Run: echo $(id -u):$(id -g) to get yours uids.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.