1

My structure is as the following

  • Code
    • package.json
    • all the other directories and files of my project
  • Dockerfile
  • .gitlab-ci.yml
  • Read Me

when I'm trying to create a docker image using Dockerfile the image doesn't work because it always says that Code folder does not exist - it doesn't show the error in the implementation but shows the error when I run the image.

Here is my dockerfile

FROM node:18.1.0-buster-slim 
RUN apt-get update && apt-get -y upgrade   

WORKDIR /code
EXPOSE 8443
COPY /code/package.json /code/package.json // I believe the error is here
RUN npm install
COPY /code/. /code // I believe the error is here


# Jave Config
RUN echo 'deb http://ftp.debian.org/debian stretch-backports main' | tee /etc/apt/sources.list.d/stretch-backports.list
RUN apt-get update && apt-get -y upgrade && \
    apt-get install -y openjdk-11-jre-headless && \
    apt-get clean;


CMD ["node","app.js"] 

I have also tried to put the dockerfile inside Code folder which is more appropriate but I did not know how to reference that in gitlab-ci.yml. The only solution worked to me is when extracting all the files and folders inside Code and but them on the root folder, but this solution is not fit my current structure.

This is also my yaml config in case you would like to have a look.

stages:
  - build-docker-image
docker-build:
  # Use the official docker image.
  image: docker:20.10.16
  stage: build-docker-image
  only:
      - dev
  tags:
    - builder
  
  services:
    - name: docker:20.10.16-dind
      
  before_script:
    - something here...
  # Default branch leaves tag empty (= latest tag)
  # All other branches are tagged with the escaped branch name (commit ref slug)
  script:
    - |
      if [[ "$CI_COMMIT_BRANCH" == "$CI_DEFAULT_BRANCH" ]]; then
        tag=""
        echo "Running on default branch '$CI_DEFAULT_BRANCH': tag = 'latest'"
      else
        tag=":$CI_COMMIT_REF_SLUG"
        echo "Running on branch '$CI_COMMIT_BRANCH': tag = $tag"
      fi
    - docker build --pull -t "$CI_REGISTRY_IMAGE${tag}" .
    - docker push "$CI_REGISTRY_IMAGE${tag}"


Fadi
  • 585
  • 4
  • 22
  • Does the `RUN npm install` step work successfully? If you `docker run --rm "$CI_REGISTRY_IMAGE" ls /code`, is your application there? How are you eventually launching the container? – David Maze May 31 '22 at 10:39
  • @DavidMaze - the run npm works and all other steps work as well - where should I put this command: docker run --rm "$CI_REGISTRY_IMAGE" ls /code ? before_script? – Fadi May 31 '22 at 10:43
  • @Lety no luck - Step 5/10 : COPY ["package.json", "./"] COPY failed: file not found in build context or excluded by .dockerignore: stat package.json: file does not exist – Fadi May 31 '22 at 10:49
  • mmmh are you sure that @Hans Kilian answer is not fine? his answer should work – Lety May 31 '22 at 10:51
  • @lety i think i knew the issue - is that the npm install doesn't work - this is why it gave me an error Cannot find module 'express' - what do you think? – Fadi May 31 '22 at 12:39
  • 1
    Did you read this: https://stackoverflow.com/questions/17162308/node-js-error-cannot-find-module-express – Lety May 31 '22 at 15:39

1 Answers1

3

You try to copy /code/package.json with an absolute path. You should use a relative path for the source. I like using relative paths for the destination as well, like this

FROM node:18.1.0-buster-slim 
RUN apt-get update && apt-get -y upgrade   

WORKDIR /code
EXPOSE 8443
COPY code/package.json .
RUN npm install
COPY code/. .
    
# Jave Config
RUN echo 'deb http://ftp.debian.org/debian stretch-backports main' | tee /etc/apt/sources.list.d/stretch-backports.list
RUN apt-get update && apt-get -y upgrade && \
    apt-get install -y openjdk-11-jre-headless && \
    apt-get clean;

CMD ["node","app.js"] 
Hans Kilian
  • 18,948
  • 1
  • 26
  • 35
  • Thank you very much - it works - I just also had to do 'npm install express' on my local. – Fadi May 31 '22 at 17:14