Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose & pm2: container exiting immediately

I am a lowly frontend developer trying to improve by backend skills, and to that end I have written my API in nodejs and am using GitLab, AWS EC2, docker and pm2 to serve the backend.

I am nearly there with a basic CI/CD setup in GitLab, but I have a problem in that my docker container appears to be exiting immediately upon running pm2 and I don't know how to get it to persist. Can you help?

My very simple docker-compose.yml looks like this:

version: '3.7'
services:
  api:
    image: some-repo/some-image:latest

My .gitlab-c.yml looks like this:

image: docker:18.09.7

variables:
  DOCKER_REPO: some-repo
  IMAGE_BASE_NAME: some-image
  IMAGE: $DOCKER_REPO/$IMAGE_BASE_NAME:$CI_COMMIT_REF_SLUG

services:
  - docker:18.09.7-dind

before_script:
  - docker login -u "$DOCKER_REGISTRY_USER" -p "$DOCKER_REGISTRY_PASSWORD"

after_script:
  - docker logout

stages:
  - build
  - deploy

build:
  stage: build
  script:
    - docker build . -t $IMAGE -f $PWD/staging.Dockerfile
    - docker push $IMAGE
    - echo $PWD

deploy:
  stage: deploy
  variables:
    RELEASE_IMAGE: $DOCKER_REPO/$IMAGE_BASE_NAME:latest
  script:
    - docker pull $IMAGE
    - docker tag $IMAGE $IMAGE
    - docker push $IMAGE
    - docker tag $IMAGE $RELEASE_IMAGE
    - docker push $RELEASE_IMAGE
    - apk add openssh-client
    - mkdir -p ~/.ssh
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
    - chmod 600 $AWS_KEY
    - ssh-keyscan www.gitlab.com >> ~/.ssh/known_hosts
    - ssh -i $AWS_KEY $AWS_URL "cd /home/ubuntu"
    - ssh -i $AWS_KEY $AWS_URL "docker system prune -a -f"
    - ssh -i $AWS_KEY $AWS_URL "docker login -u $DOCKER_REGISTRY_USER -p $DOCKER_REGISTRY_PASSWORD"
    - ssh -i $AWS_KEY $AWS_URL "docker pull $RELEASE_IMAGE"
    - ssh -i $AWS_KEY $AWS_URL "/snap/bin/docker-compose stop"
    - ssh -i $AWS_KEY $AWS_URL "/snap/bin/docker-compose up -d"
    - ssh -i $AWS_KEY $AWS_URL "docker logout"

My staging.Dockerfile looks like this:

FROM node:latest

LABEL author="Mark Norgate"

WORKDIR /var/www/api

ENV ENVIRONMENT_CONFIG=staging

COPY ./src ./src
COPY ./package.json ./package.json
COPY ./tsconfig.json ./tsconfig.json
COPY ./.pm2/staging/ecosystem.config.js ./ecosystem.config.js
COPY ./credentials/some-credentials.json ./some-credentials.json

RUN npm install
RUN npm install --global typescript
RUN tsc -p .
RUN npm install --global pm2

EXPOSE 3001
EXPOSE 3002
EXPOSE 27017

ENTRYPOINT ["pm2", "start", "/var/www/api/ecosystem.config.js"]

and my ecosystem.config.js looks like this:

module.exports = {
  apps : [{
    name: 'API',
    script: '/var/www/api/lib/server.js',
    env: {
      GOOGLE_APPLICATION_CREDENTIALS: '/var/www/api/some-credentials.json'
    }
  }]
};

Now it appears that this is all working; except that, as I say, the container exits as soon as this pm2 command is executed. I thought this might be a simple as running docker-compose up in detached mode, but this hasn't helped.

Can anyone spot my error/omission?

like image 548
serlingpa Avatar asked Apr 08 '20 14:04

serlingpa


Video Answer


1 Answers

You might try pm2-runtime instead of pm2 to facilitate the container integration - docs are here

like image 51
nrako Avatar answered Oct 16 '22 05:10

nrako