Bear Su's Blog

gem install fail with docker in docker

Table of Contents

打算照著 GitLab 文件,用 dind 的方式建置 Container Image,但在執行 CI Job 時,收到了錯誤訊息

You don't have write permission for the /usr/local/bundle

.gitlab-ci.yml 內容(參考官方文件)如下:

build:
  image: docker:19.03.12
  stage: build
  services:
    - docker:19.03.12-dind
  script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker build -t $CI_REGISTRY/group/project/image:latest .
    - docker push $CI_REGISTRY/group/project/image:latest

專案使用的 Dockerfile 內容如下:

FROM ruby:2.7.4-alpine

RUN apk add --no-cache --update build-base tzdata yarn mysql-dev libc6-compat
RUN gem install bundler --no-document

查了一下,發現因為 Alpine 3.14 images can fail on Docker versions older than 20.10,所以只要升級 docker 版本就可以了。將 .gitlab-ci.yml 修改如下:

stages:
  - build

build:
  image: docker:20.10.12
  stage: build
  services:
    - docker:20.10.12-dind
  script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker build -t $CI_REGISTRY/group/project/image:latest .
    - docker push $CI_REGISTRY/group/project/image:latest

如果覺得這篇文章對您有所幫助,歡迎贊助我一杯咖啡 ☕️

祝您有美好的一天 ❤️