Docker build fails in the pipeline with npm error while builds local builds successfully
Asked by r.abbasi:

My docker file which is building successfully local, is as following   

```
FROM node:14.1-alpine AS builder

COPY . /opt/web

WORKDIR /opt/web

RUN npm install

RUN npx browserslist@latest --update-db

ENV PATH="/opt/web/node_modules/.bin:$PATH"

RUN npm run build

FROM nginx:1.17-alpine

RUN apk --no-cache add curl

COPY --from=builder /opt/web/build /usr/share/nginx/html

RUN chgrp -R 0 /var/* && chmod -R g=u /var/*

CMD ["nginx", "-g", "daemon off;"]
```

When I run the same docker file for the build throught he pipeline, I am getting following error.  
![error.png](/t5/image/serverpage/image-id/171892i66AD87A2194747B1/image-size/large?v=v2&px=999 "error.png")  
My bitbucket pipeline yml file is as following, I did try both atlassian/default-image:3 and node image, however getting same error.   

```
# Template docker-push

# This template allows you to build and push your docker image to a Docker Hub account.

# The workflow allows running tests, code linting and security scans on feature branches (as well as master).

# The docker image will be validated and pushed to the docker registry after the code is merged to master.

# Prerequisites: $DOCKERHUB_USERNAME, $DOCKERHUB_PASSWORD setup as deployment variables

# image: atlassian/default-image:3

image: node:14.1-alpine

pipelines:

branches:

feature/devops:

 - step:

name: Build and Test

script:

 - IMAGE_NAME=$BITBUCKET_REPO_SLUG

 - docker build . --file Dockerfile --tag ${IMAGE_NAME}

 - docker save ${IMAGE_NAME} --output "${IMAGE_NAME}.tar"

services:

 - docker

caches:

 - docker

artifacts:

 - "*.tar"
```


Answers:
author: r.abbasi
body: The final solution, which worked for me was to unset the CI before running the npm run build, along with using the npm in the bitbucket pipeline, instead of the docker file.

Here I am pasting my updated pipeline, for the successfull build

```
image: atlassian/default-image:3

pipelines:

branches:

feature/devops:

 - step:

name: Build and Test

script:

 - npm cache clean --force

 - rm -rf node_modules

 - npm install

 - unset CI

 - npm run build

 - IMAGE_NAME=${BITBUCKET_REPO_SLUG}

 - docker build . --file Dockerfile --tag ${IMAGE_NAME}

 - docker save ${IMAGE_NAME} --output "${IMAGE_NAME}.tar"

services:

 - docker

caches:

 - docker

artifacts:

 - "*.tar"
```

comments: author: Ahmad Shah
body: Thanks [@r.abbasi](/t5/user/viewprofilepage/user-id/4558896) it works


author: Igor Stoyanov
body: [@r.abbasi](/t5/user/viewprofilepage/user-id/4558896) hi. Do you have warnings in your local build? This was pointed here: <https://stackoverflow.com/a/58701621>

Regards, Igor.


author: r.abbasi
body: [@Igor Stoyanov](/t5/user/viewprofilepage/user-id/4550492) Yeh, I had warnings in the build. Thanks for the support.


author: Igor Stoyanov
body: [@r.abbasi](/t5/user/viewprofilepage/user-id/4558896) hi. Thanks for your question. Try to [follow Build and push a Docker image guide](https://support.atlassian.com/bitbucket-cloud/docs/build-and-push-a-docker-image-to-a-container-registry/).

Also the suggestion is not to use node:14.1-alpine, because alpine is minimalistic, instead use the image node:14.1

Regards, Igor

comments: author: r.abbasi
body: I am getting the same error for the following very simple pipeline

```
image: node:14.17.6

pipelines:

branches:

feature/devops:

 - step:

name: Build and Test

script:

 - npm install

 - npm run build
```


author: Igor Stoyanov
body: [@r.abbasi](/t5/user/viewprofilepage/user-id/4558896) hi. Try to look here <https://stackoverflow.com/questions/42308879/how-to-solve-npm-error-npm-err-code-elifecycle>


author: Igor Stoyanov
body: Also similar question: <https://community.atlassian.com/t5/Bitbucket-questions/Pipe-error-when-build-test/qaq-p/1099191>


author: r.abbasi
body: ok, Thanks [@Igor Stoyanov](/t5/user/viewprofilepage/user-id/4550492) . I am trying the first comment, as the second one isn't valid in my case.


author: r.abbasi
body: I am still getting the same error, while the build is successfull locally. Looks like something wrong with the bitbucket pipeline for node containerized app. look at the updated pipeline file.

```
image: node:16.13.1

pipelines:

branches:

feature/devops:

 - step:

name: Build and Test

script:

 - IMAGE_NAME=$BITBUCKET_REPO_SLUG

 - docker build . --file Dockerfile --tag ${IMAGE_NAME}

 - docker save ${IMAGE_NAME} --output "${IMAGE_NAME}.tar"

services:

 - docker

caches:

 - docker

artifacts:

 - "*.tar"
```


author: Igor Stoyanov
body: @ hi. Since you do not use npm in bitbucket-pipeline.yml use

```
image: atlassian/default-image:3
```

also check and update commands inside your Dockerfile according to <https://stackoverflow.com/questions/42308879/how-to-solve-npm-error-npm-err-code-elifecycle>


author: r.abbasi
body: I did follow that as well, but didn't work. Looks like some conflict between node and bitbucket pipelines.


Tags: bitbucket-pipelines

build

docker

nginx

node