wmash
wmash

Reputation: 4202

GitLab CI Docker WORKDIR not being created

I am trying to deploy my NodeJS repo to a DO droplet via GitLab CI. I have been following this guide to do so. What is odd is that the deployment pipeline seems to succeed but if I SSH into the box, I can see that the app is not running as has failed to find a package.json in /usr/src/app which is the WORKDIR my Dockerfile is pointing to.

gitlab-ci.yml

cache:
  key: "${CI_COMMIT_REF_NAME} node:latest"
  paths:
    - node_modules/
    - .yarn

stages:
  - build
  - release
  - deploy

build:
  stage: build
  image: node:latest
  script:
    - yarn
  artifacts:
    paths:
      - node_modules/

release:
  stage: release
  image: docker:latest
  only:
    - master
  services:
    - docker:dind
  variables:
    DOCKER_DRIVER: "overlay"
  before_script:
    - docker version
    - docker info
    - docker login -u ${CI_REGISTRY_USER} -p ${CI_BUILD_TOKEN} ${CI_REGISTRY}
  script:
    - docker build -t ${CI_REGISTRY}/${CI_PROJECT_PATH}:latest --pull .
    - docker push ${CI_REGISTRY}/${CI_PROJECT_PATH}:latest
  after_script:
    - docker logout ${CI_REGISTRY}

deploy:
  stage: deploy
  image: gitlab/dind:latest
  only:
    - master
  environment: production
  when: manual
  before_script:
    - mkdir -p ~/.ssh
    - echo "${DEPLOY_SERVER_PRIVATE_KEY}" | tr -d '\r' > ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/id_rsa
    - eval "$(ssh-agent -s)"
    - ssh-add ~/.ssh/id_rsa
    - ssh-keyscan -H ${DEPLOYMENT_SERVER_IP} >> ~/.ssh/known_hosts
  script:
    - printf "DB_URL=${DB_URL}\nDB_NAME=${DB_NAME}\nPORT=3000" > .env
    - scp -r ./.env ./docker-compose.yml root@${DEPLOYMENT_SERVER_IP}:~/
    - ssh root@${DEPLOYMENT_SERVER_IP} "docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}; docker-compose rm -sf scraper; docker pull ${CI_REGISTRY}/${CI_PROJECT_PATH}:latest; docker-compose up -d"

Dockerfile

FROM node:10

WORKDIR /usr/src/app

COPY package.json ./
RUN yarn

COPY . .
EXPOSE 3000

CMD [ "yarn", "start" ]

docker-compose.yml

version: "3"
services:
  scraper:
    build: .
    image: registry.gitlab.com/arby-better/scraper:latest
    volumes:
      - .:/usr/src/app
      - /usr/src/app/node_modules
    ports:
      - 3000:3000
    environment:
      - NODE_ENV=production
    env_file:
      - .env

I'm using GitLab Shared Runners for my pipeline. My pipeline looks like it executes completely fine but for this symlink failure at the end:

enter image description here

...which I don't think is anything to worry about. If I SSH into my box & go to where the docker compose was copied & inspect:

enter image description here

Docker has not created /usr/src/app.

Versions:

My DO box is Docker 1-click btw. Any help appreciated!

EDIT

I have altered my Dockerfile to attempt to force the dir creation so have added RUN mkdir -p /usr/src/app before the line declaring it as the working dir. This still does not create the directory...

When I look at the container status' (docker-compose ps), I can see that the containers are in an exit state & have exited with code either 1 or 254...any idea as to why?

Upvotes: 2

Views: 3777

Answers (1)

BMitch
BMitch

Reputation: 264761

Your compose file is designed for a development environment, where the code directory is replaced by a volume mount to the code on the developers machine. You don't have this persistent directory in production, nor should you be depending on code outside of the image in production, defeating the purpose of copying it into your image.

version: "3"
services:
  scraper:
    build: .
    image: registry.gitlab.com/arby-better/scraper:latest
    # Comment out or delete these lines, they do not belong in production
    #volumes:
    #  - .:/usr/src/app
    #  - /usr/src/app/node_modules
    ports:
      - 3000:3000
    environment:
      - NODE_ENV=production
    env_file:
      - .env

Upvotes: 3

Related Questions