node.jsdockerdockerfilealpine-linuxalpine-package-keeper

npm command not found error while running docker container


I am trying some something out with gitlab-runner image,

    FROM gitlab/gitlab-runner:alpine
    WORKDIR /app
    COPY . /app
    RUN apk add yarn && yarn install

    RUN yarn --version        # this layer prints 1.16.0

    RUN ng build --prod
    EXPOSE 3000
    CMD ["yarn", "run", "start"]

above is the docker file I have created

    docker build -t runner:1 .

I was able to build the image successfully

    docker run -p 3000:3000 runner:1

but when I try to run the container it gives me below error

`*FATAL: Command yarn not found.*`

not sure about the behavior, if it is able to install yarn (apk add yarn) in base images and install the dependencies using yarn install then how it is not able to find the yarn command while running the container? Where I am going wrong.

Also at which directory yarn is installed in the alpine ?

I know it is not an efficient docker file, but I am trying to run the container first before optimizing it.


Solution

  • It outputs the version. It means the yarn is installed already. You could find the path the same as you find the version.

    RUN which yarn

    Step 6/10 : RUN which yarn
     ---> Running in 0f633b81f2ed
    /usr/bin/yarn
    

    We can see the /usr/bin/ has added to PATH.

     Step 7/11 : RUN echo $PATH
     ---> Running in fc3f40b6bfd9
    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    

    But I couldn't figure out why isn't reading yarn from the PATH.

    So, we have set the PATH explicitly in our Dockerfile.

    ENV PATH=${PATH} 
    

    But still, the issue persists. Now we have to separate yarn and commands as ENTRYPOINT and CMD respectively in the Dockerfile.

    ENTRYPOINT ["yarn"]
    CMD ["run", "start"]
    

    Updated Dockerfile

    FROM gitlab/gitlab-runner:alpine
    
    ENV PATH=${PATH}
    
    WORKDIR /app
    COPY . /app
    RUN apk add yarn && yarn install
    
    RUN yarn --version        # this layer prints 1.16.0
    RUN ng build --prod
    
    EXPOSE 3000
    ENTRYPOINT ["yarn"]
    CMD ["run", "start"]
    ---
    
    $ docker run -p 3000:3000 harik8/yarn:latest 
    yarn run v1.16.0
    error Couldn't find a package.json file in "/app"
    info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
    

    The behaviours of the base image look unusual. It'd be better to go through it.