potato
potato

Reputation: 223

GitLab CI docker build failed

I want to create a CI pipeline on GitLab for my project.

And I have created two branches: my-server, my-client

My project folder structure is like this below:

ROOT:
.gitlab-ci.yml

backend/
--server.js
--Dockerfile
...

react-app/
--some src files...
--Dockerfile

And my .gitlab-ci.yml like this:

stages:
  - build
  - deploy

variables:
  CONTAINER_IMAGE: myusername/${CI_COMMIT_REF_NAME}:${CI_COMMIT_SHORT_SHA}

docker_build:
  image: docker:stable

  services:
    - docker:dind

  stage: build

  only:
    - my-server
    - my-client

  script:
    - docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD}
    - docker build -t ${CONTAINER_IMAGE} .
    - docker push ${CONTAINER_IMAGE}

However, the CI job is failed:

$ docker build -t ${CONTAINER_IMAGE} .
unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /builds/myusername/my-project/Dockerfile: no such file or directory

I don't know how to locate the pipeline to the relative folder (backend and react-app) for building the Dockerfile.

Upvotes: 0

Views: 1900

Answers (1)

Prashanna
Prashanna

Reputation: 1001

From the looks of it your DockerFile will be residing inside backend/ and react-app/.

So gitlab CI tries to find the DockerFile in root folder so kindly do

cd backend/
cd react-app/

in your first cmd of your script section

stages:
  - build
  - deploy

variables:   
  CONTAINER_IMAGE: myusername/${CI_COMMIT_REF_NAME}:${CI_COMMIT_SHORT_SHA}

docker_build:
  image: docker:stable

  services:
    - docker:dind

  stage: build

  only:
    - my-server
    - my-client

  script:
    - cd ${DOCKERFILE_PATH} #for backend project and cd react-app/ for frontend
    - docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD}
    - docker build -t ${CONTAINER_IMAGE} .
    - docker push ${CONTAINER_IMAGE}

Upvotes: 2

Related Questions