我不建议用docker executor,主要是里面的逻辑太复杂,限制太多,建议还是用shell executor,也可以支持docker的全部功能,实现方式如下:
1). 需要基于gitlab-runner镜像制作带docker工具和其他工具的镜像
FROM gitlab/gitlab-runner:ubuntu
ARG DEBIAN_FRONTEND=noninteractive
ENV TZ=Asia/Shanghai
RUN set -eux;\
apt update && \
apt install -y --no-install-recommends \
apt-transport-https ca-certificates\
procps \
curl wget \
xz-utils\
file \
git vim fzf build-essential && \
ln -snf /usr/share/zoneinfo/$TZ /etc/localtime &&\
echo $TZ > /etc/timezone && \
apt install -y --no-install-recommends tzdata && \
dpkg-reconfigure --frontend noninteractive tzdata
# Add locales
RUN set -o errexit -o nounset \
&& apt update -y && apt install -y locales \
&& locale-gen en_GB.UTF-8 \
&& echo 'LANG=en_GB.UTF-8' > /etc/locale.conf \
&& echo 'LC_ALL=en_GB.UTF-8' >> /etc/locale.conf \
&& echo 'LANGUAGE=en_GB:en' >> /etc/locale.conf
ENV LANG=en_GB.UTF-8
ENV LANGUAGE=en_GB.UTF-8
ENV LC_ALL=en_GB.UTF-8
RUN \
set -xe \
&& apt install -y --no-install-recommends \
# net tool
gnupg tcpdump net-tools inetutils-ping telnet \
strace lsof htop iptraf sysstat bind9-utils iproute2 \
ca-certificates wget curl iptables \
# build tool
unzip zip python3-pip bzip2 git vim jq
# tools
COPY --from=mikefarah/yq /usr/bin/yq /usr/bin/yq
# docker
COPY --from=seanly/toolset:docker \
/install/docker/usr/bin/* \
/usr/bin/
# docker plugins
COPY --from=seanly/toolset:docker \
/root/.docker/ \
/root/.docker/
# kubernetes
COPY --from=seanly/toolset:helm /usr/bin/helm /usr/bin/helm
COPY --from=seanly/toolset:kustomize /usr/bin/kustomize /usr/bin/kustomize
COPY --from=seanly/toolset:kubectl /usr/bin/kubectl /usr/bin/kubectl
CMD ["run", "--user=root", "--working-directory=/home/gitlab-runner"]
2). 构建镜像
3). 使用docker-compose 启动gitlab-runner,注意这里启动两个服务,另外一个是dind,下面是参考的docker-compose文件
version: '3'
services:
gitlab-runner:
image: seanly/appset:gitlab-runner-1
container_name: gitlab-runner
restart: always
volumes:
- docker-certs:/certs/client:ro
- runner-workspace:/data/workspace:rw
environment:
DOCKER_HOST: tcp://runner-docker:2376
DOCKER_CERT_PATH: /certs/client
DOCKER_TLS_VERIFY: 1
RUNNER_NAME: ${RUNNER_NAME}
REGISTER_NON_INTERACTIVE: true
CI_SERVER_URL: ${CI_SERVER_URL}
REGISTRATION_TOKEN: ${REGISTRATION_TOKEN}
RUNNER_TAG_LIST: "docker,default,dind"
REGISTER_RUN_UNTAGGED: true
REGISTER_LOCKED: true
RUNNER_EXECUTOR: shell
CI_SERVER_TOKEN: ${CI_SERVER_TOKEN}
RUNNER_BUILDS_DIR: /data/workspace/builds
RUNNER_CACHE_DIR: /data/workspace/cache
labels:
- traefik.enable=false
runner-docker:
image: docker:dind
restart: unless-stopped
container_name: runner-docker
command: --storage-driver=overlay2
environment:
- DOCKER_TLS_CERTDIR=/certs
- DOCKER_TLS_SAN=DNS:runner-docker
privileged: true
volumes:
- docker-certs:/certs/client
- runner-workspace:/data/workspace:rw
labels:
- traefik.enable=false
volumes:
docker-certs:
runner-workspace:
networks:
default:
name: opsbox-network
external: true
通过这种方式gitlab-runner里面的docker链接的是另外容器的dockerd服务。 |