Search code examples
node.jsdockerexpressgitlabgitlab-ci-runner

Docker container exits with code 2 with deploying node app to Digital Ocean via Gitlab CI/CD pipeline


Good morning all, I am trying to deploy my node app to Digital Ocean via a Gitlab CI/CD pipeline. The pipeline is successful and deploys to DO, but the container exits with code (2). My Node App uses port 3000. I am using pm2 to run the server, but open to not using pm2. Below is my docker file, and my .yml file.

# ssh-keyscan gitlab.com >> authorized_keys: use this command to add gitlab ssh keys to sever. Run on server terminal
# cat id_rsa.pub >> authorized_keys Run this command on the sever on the terminal. 
# Both COMMANDS ABOVE ARE necessary.

stages:
  - build
  - publish
  - deploy

variables:
  TAG_LATEST: $CI_REGISTRY_IMAGE/$CI_COMMIT_REF_NAME:latest
  TAG_COMMIT: $CI_REGISTRY_IMAGE/$CI_COMMIT_REF_NAME:$CI_COMMIT_SHA

build-Node:
  image: node:latest
  stage: build
  script:
    - npm install
    - echo   "ACCOUNT_SID=$ACCOUNT_SID" >> .env
    - echo   "AUTH_TOKEN=$AUTH_TOKEN" >> .env
    - echo   "API_KEY=$API_KEY" >> .env
    - echo   "API_SECRET=$API_SECRET" >> .env
    - echo   "PHONE_NUMBER=$PHONE_NUMBER" >> .env
    - echo    "sengrid_api=$sengrid_api" >> .env

build-Docker:
  image: docker:latest
  stage: build
  services:
    - docker:dind
  script:
    - docker build . -t $TAG_COMMIT -t $TAG_LATEST 
    - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY
    - docker push $TAG_COMMIT
    - docker push $TAG_LATEST

deploy:
  image: ubuntu:latest
  stage: deploy
  tags:
    - deployment
  before_script:
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client git -y )'
  - eval $(ssh-agent -s)
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh
  - echo "$SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_rsa
  - echo "$SSH_PUBLIC_KEY" | tr -d '\r' > ~/.ssh/id_rsa.pub
  - chmod 600 ~/.ssh/*
  - chmod 644 ~/.ssh/*.pub
  - ssh-add
  - ssh-keyscan gitlab.com >> ~/.ssh/known_hosts
  - chmod 644 ~/.ssh/known_hosts
  - ls -ld ~/.ssh/*
  script:
    - ssh   -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP "docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY"
    - ssh  -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP "docker pull $TAG_COMMIT"
    - ssh  -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP "docker container rm -f my-app || true"
    - ssh  -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP "docker run -d -p 80:3000 --name my-app $TAG_COMMIT"
  environment:
    name: production
    url: http://167.172.225.124
  only:
    - master
FROM node:12.18.3
    # make the starting directory the current one
     WORKDIR /
    # COPY Package.json 
    COPY package*.json / 
     # install the dependencines within the app
      RUN npm install
     # Install pm2 
      RUN npm install pm2 -g
     # Copy Source Code 
        COPY . .
        

    # Have docker container use port 3000, that is the port that the node app is set to
     EXPOSE 3000
   # Start the node app 
   CMD ["pm2-runtime", "./bin/www"]
        


Solution

  • I took the echo statements and put them before the dock build command in the docker build stage. I wasn't using artifacts.