1

I'm trying to build docker image and push to gitlab registry.

I'm using gitlab-shared runner.

There is no problems for npm install but how to build docker image on next job?

image: node:14.17.3

stages:
  - install
  - build


install_dependencies:
  stage: install
  tags:
    - gitlab-org-docker
  script:
    - pwd
    - npm install

build-job:
  stage: build
  image: docker
  tags:
    - gitlab-org-docker
  script:
    - echo "Compiling the code..."
    - echo "build"
    - docker build -t bmw-frontend-app .
    - docker ps

enter image description here

I had forgot about dind. dind means Docker in Docker.

I succeed like below.

image: docker:19.03.13

cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
  - node_modules/
  - .next/

stages:
  - build

services:
  - docker:19.03.13-dind

build-job:
  stage: build
  before_script:
    - echo docker login $DOCKER_REGISTRY --username $DOCKER_REGISTRY_USER --password-stdin
  tags:
    - gitlab-org-docker
  script:
    - echo "Compiling the code..."
    - echo "build"
    - docker build -t registry.gitlab.com/areum.lee1/bmwgs_frontend . 
    - docker login registry.gitlab.com -u $DOCKER_REGISTRY_USER -p $DOCKER_REGISTRY_PASS
    - docker push registry.gitlab.com/areum.lee1/bmwgs_frontend

  1. I modified outer image to image: docker:19.03.12
  2. append service block.

enter image description here

Matthew
  • 125
  • 2
  • 11
  • If you mention the node_modules from ```stage: install``` for ```stage: build```, you can try with **cache** (```https://docs.gitlab.com/ee/ci/caching/```). I haven't tried with dind or shared runner but I think It can work. – Franxi Hidro Oct 15 '21 at 06:39

1 Answers1

0

You should check out GitLab's documentation about using docker-in-docker as it has a great walkthrough; in this particular case you're missing the dind service that actually executes the docker commands, that's why you're getting the connect error.

Link: https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#docker-in-docker-with-tls-enabled

Patrick
  • 2,885
  • 1
  • 14
  • 20
  • Thanks I've missed about dind. But I tried to find about this on above document but I hadn't then I uploded this question on this and I hadn't found about docker build after npm install. I think you don't have experience about npm install. Isn't it? – Matthew Oct 15 '21 at 04:18
  • Sorry Matthew, I'm having a little bit of trouble understanding your English, so let me make sure I understand the question you're asking: You didn't actually mean to ask a question about `dind`, you actually meant to ask how you could run `npm install` in one job and then use the results from the `npm install` in the next job? If so, you should check out the `artifacts` keyword for your CI/CD which will save a set of files/directories from your job, and download those files into the same spot in subsequent jobs. – Patrick Oct 15 '21 at 18:48
  • Thanks Patrick, I solved this problem like above. I changed image to docker from node. – Matthew Oct 20 '21 at 07:32