1

The Node JS app that I'm trying to deploy in Kubernetes runs on express js as a backend framework.The repository is managed via Bitbucket. The application is a microservice and the pipeline manifest file for building the Docker image is written this way:

options:
  docker: true
image: node:14.17.0
pipelines:
  branches:
    test:
      - step:
          services:
           - docker
          name: Build and push new docker image
          deployment: dev
          script:
            - yarn install
            - yarn build
            - yarn test
            - yarn lint
            - make deploy.dev
            - docker login -u $DOCKER_HUB_USERNAME -p $DOCKER_HUB_PASSWORD
            - docker build -t testapp/helloapp:latest -f ./Dockerfile .
            - docker push testapp/helloapp
          caches:
            - docker # adds docker layer caching

The K8s cluster is hosted on cloud but does not have the internal Load Balancer of their own. The K8s cluster version is v1.22.4 and MetalLB v0.11.0 is configured to serve the Load Balancing purpose. To expose the K8s service- Cloudflare Tunnel is configured as a K8s deployment.

So, this is the manifest file set-up used for building the Docker image. The pipeline deploys successfully and in the Kubernetes part, this is the service and deployment manifest:

apiVersion: v1
kind: Service
metadata:
 name: helloapp
 labels:
   app: helloapp
spec:
 type: NodePort
 ports:
 - port: 5000
   targetPort: 7000
   protocol: TCP
   name: https
 selector:
   app: helloapp

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: helloapp
  labels:
    app: helloapp
spec:
  replicas: 3
  selector:
    matchLabels:
      app: helloapp
  template:
    metadata:
      labels:
        app: helloapp
    spec:
      imagePullSecrets:
      - name: regcred
      containers:
      - name: helloapp
        image: testapp/helloapp:latest

Also, here is the Dockerfile snippet to give more clarity on what I have been doing:

FROM node:14.17.0

WORKDIR /app

COPY package.json /app

RUN npm install

COPY . /app

CMD node app.js

EXPOSE 8100

Just to give a context, the service and deployment works fine, with no CrashLoopBackOff or any other errors. My doubt here is, there is dist directory which is not getting deployed to Docker Hub as it is generated during npm build. How can I deploy the app along with the dist directory without having to worry about security risks? Any feedbacks and suggestions on where I could add a script to pull the dist directory would be appreciated.

Sebastian
  • 49
  • 1
  • 8
  • What’s your Dockerfile? – Isolated Dec 06 '21 at 17:36
  • @Isolated updated – Sebastian Dec 06 '21 at 17:43
  • Update your Dockerfile to COPY dist to /app instead of everything. – Isolated Dec 06 '21 at 17:47
  • “without having to worry about security risks”, what security risks are you worried about? Are you copying across sensitive files? (.env for example) – Isolated Dec 06 '21 at 17:48
  • There are some stuffs inside dist which are part of .ENV file. So, that's something I was referring to. – Sebastian Dec 06 '21 at 17:57
  • 1
    which files are added to dist? If your .env is being moved to dist then that is an issue. You can inject environment variables using both Docker and Kubernetes. You could also add .env to .dockerignore (in addition to any other sensitive files, although I prefer to be explicit) https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/ – Isolated Dec 06 '21 at 23:34
  • Which version of Kubernetes did you use and how did you set up the cluster? Did you use bare metal installation or some cloud providor? It is important to reproduce your problem. – Mikołaj Głodziak Dec 07 '21 at 10:50
  • @MikołajGłodziak updated. – Sebastian Dec 08 '21 at 18:18
  • Where exactly is the problem? Do you have some errors and logs? – Mikołaj Głodziak Dec 10 '21 at 13:57
  • @MikołajGłodziak when I hit `curl http://a.b.c.d:30001`, the connection gets refused. I know the issue is because I am not able to import `dist` directory to K8s. So, what I'm asking is how can I copy `dist` directory- because what I have tried is a multitude of options; including the removal of `dist` directory from `.gitignore`. Also, similarly I tried the answer accepted [here](https://stackoverflow.com/questions/59693994/dist-folder-is-not-generated-while-doing-npm-build-in-a-dockerfile) and it does not work in my case. – Sebastian Dec 10 '21 at 15:28
  • 1
    what is the exact returned result from `curl`? Is is possible to hardcode your directory inside your image? – Mikołaj Głodziak Dec 14 '21 at 11:29

1 Answers1

1

Eventually, I could resolve the issue. The issue was trivial yet bothering. In the Dockerfile, there was a missing script, i.e., npm run build. So, here is the final Dockerfile I used it for building the dist directory along with other requirements:

FROM node:14.17.0

WORKDIR /app

COPY package.json /app

RUN npm install

RUN npm run build <------ the missing script

COPY . /app

CMD node app.js

EXPOSE 8100

This way, the entire dist directory gets built inside the container. Also, I removed all the .ENV dependencies from dist directory and stored as Kubernetes secret in base64 format.

Sebastian
  • 49
  • 1
  • 8