Docker-in-Docker with Gitlab Shared runner for building and pushing docker images to registry

前端 未结 1 1302
忘了有多久
忘了有多久 2020-12-30 09:03

Been trying to set-up Gitlab CI which can build a docker image, and came across that DinD was enabled initially only for separate runners and Blog Post suggest it would be e

1条回答
  •  一整个雨季
    2020-12-30 09:39

    The shared runners are now capable of building Docker images. Here is the job that you can use:

    stages:
      - build
      - test
      - deploy
    
    # ...
    # other jobs here
    # ...
    
    docker:image:
      stage: deploy
      image: docker:1.11
      services:
        - docker:dind
      script:
        - docker version
        - docker build -t $CI_REGISTRY_IMAGE:latest .
        # push only for tags
        - "[[ -z $CI_BUILD_TAG ]] && exit 0"
        - docker tag $CI_REGISTRY_IMAGE:latest $CI_REGISTRY_IMAGE:$CI_BUILD_TAG
        - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY
        - docker push $CI_REGISTRY_IMAGE:$CI_BUILD_TAG
    

    This job assumes that you are using the Container Registry provided by Gitlab. It pushes the images only when the build commit is tagged with a version number.

    • Documentation for Predefined variables.

    • Note that you will need to cache or generate as temporary artifacts of any dependencies for your service which are not committed in the repository. This is supposed to be done in other jobs. e.g. node_modules are not generally contained in the repository and must be cached from the build/test stage.

    0 讨论(0)
提交回复
热议问题