Search code examples
hadoop-yarnddev

Run yarn from Dockerfile in ddev


This is a followup to How can I add and use nvm in a DDEV web container?

My dockerfile now looks like this:

ARG BASE_IMAGE
FROM $BASE_IMAGE

ENV NVM_DIR=/usr/local/nvm
ENV NODE_DEFAULT_VERSION=v8.16.1

RUN curl -sL https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh -o install_nvm.sh
RUN mkdir -p $NVM_DIR && bash install_nvm.sh
RUN echo "source $NVM_DIR/nvm.sh" >>/etc/profile
RUN bash -ic "nvm install $NODE_DEFAULT_VERSION && nvm use $NODE_DEFAULT_VERSION"
RUN chmod -R ugo+w $NVM_DIR
RUN npm install -g foundation-cli
RUN npm install -g gulp-cli
RUN yarn --cwd foundation-src install

The last line returns an error: Service 'web' failed to build: The command '/bin/sh -c yarn --cwd foundation-src install' returned a non-zero code: 1'

When I ddev ssh and then run yarn --cwd foundation-src install it does the job (running yarn in the foundation-src folder).

I also tried RUN (cd foundation-src; yarn install;) but no luck either. I prefer the first command anyway. But what is going on? Why can I run stuff from inside the container but not from the dockerfile?


Solution

  • Your command is RUN yarn --cwd foundation-src install - it's assuming that there is a subdirectory "foundation-src" under the current directory.

    But the Dockerfile is running long, long before your source is anywhere useful. The container has not been run yet, nothing is mounted. So you can't do things that require your source code to be present.

    Since this command appears to require your source code to be present, I think you'll be better doing this as a post-start hook, perhaps

    hooks:
      post-start:
        - exec: "yarn --cwd foundation-src install"
    

    Since actions like yarn install happen irregularly, it's also easy to ddev exec yarn --cwd foundation-src install and also easy to create a custom command to do that when you need it.