Search code examples
node.jsdockerexpressgitlabgitlab-ci-runner

"docker pull" requires exactly 1 argument error when using Gitlab Runner to deploy to Digital Ocean


Good Evening, I am trying to deploy my nodejs app and have it run using pm2 and also use Docker, but Docker is throwing ""docker pull" requires exactly 1 argument. See 'docker pull --help'. Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST] Pull an image or a repository from a registry" error. Any help is appreciated, thank you for your time.

# 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_SHORT_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:
    ##
  ## Install ssh-agent if not already installed, it is required by Docker.
  ## (change apt-get to yum if you use an RPM-based image)
  ##
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client git -y )'

  ##
  ## Run ssh-agent (inside the build environment)
  ##
  - eval $(ssh-agent -s)
  ##
  ## Create the SSH directory and give it the right permissions
  ##
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh
  ##
  ## Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
  ## We're using tr to fix line endings which makes ed25519 keys work
  ## without extra base64 encoding.
  ## https://gitlab.com/gitlab-examples/ssh-private-key/issues/1#note_48526556
  ##
  - 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
 

  ##
  ## Use ssh-keyscan to scan the keys of your private server. Replace gitlab.com
  ## with your own domain name. You can copy and repeat that command if you have
  ## more than one server to connect to.
  ##
  - ssh-keyscan gitlab.com >> ~/.ssh/known_hosts
  - chmod 644 ~/.ssh/known_hosts
  - ls -ld ~/.ssh/*
  - cat ~/.ssh/*
  ##
  ## Alternatively, assuming you created the SSH_SERVER_HOSTKEYS variable
  ## previously, uncomment the following two lines instead.
  ##
  #- echo "$SSH_SERVER_HOSTKEYS" > ~/.ssh/known_hosts'
  #- chmod 644 ~/.ssh/known_hosts

  ##
  ## You can optionally disable host key checking. Be aware that by adding that
  ## you are suspectible to man-in-the-middle attacks.
  ## WARNING: Use this only with the Docker executor, if you use it with shell
  ## you will overwrite your user's SSH config.
  ##
  #- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'

  ##
  ## Optionally, if you will be using any Git commands, set the user name and
  ## email.
  ##

  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'
    - ssh  -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP '. /etc/profile; pm2 reload all'
  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

  • The error message:

    "docker pull" requires exactly 1 argument
    

    means the line:

    ssh  -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP 'docker pull $TAG_COMMIT'
    

    is incorrect.

    • Either $TAG_COMMIT is empty (so no argument is passed to docker pull)
    • or $TAG_COMMIT has a space in it (making it two arguments passed to docker pull
    • or $TAG_COMMIT is not replaced by its value and remains empty within the ssh 'docker pull $TAG_COMMIT' session.
      Using double-quotes would help (ssh "docker pull $TAG_COMMIT"), ensuring the shell can interpret $TAG_COMMIT before sending it to the remote shell.

    I would double-check (with an echo) the value of:

    • TAG_COMMIT: $CI_REGISTRY_IMAGE/$CI_COMMIT_REF_NAME:$CI_COMMIT_SHORT_SHA
    • each of the component of that variable ($CI_REGISTRY_IMAGE, ...)

    That way, I can see why the argument passed to docker pull is incorrect.