首页 文章

在Gitlab Runner容器中构建Docker镜像

提问于
浏览
1

我正在尝试设置一个运行Ubuntu Linux 18.04作为docker主机的构建服务器 .

主机有三个docker容器运行 - Docker Registry - Gitlab Server - Gitlab Runner(用于构建Angular Apps)

我希望Gitlab Runner容器用nginx构建docker镜像并编译Angular代码并将其推送到Docker Registry .

我已设法设置所有三个容器正在运行,Gitlab运行器正在构建角度项目,但我面临的挑战是在Gitlab Runner容器中构建docker镜像 .

在Gitlab Runner容器中,Docker命令不可用于构建docker镜像 .

这可能吗 ??

我曾尝试在Gitlab Runner容器中安装docker.io,因此在构建之后,它可以使用docker命令,但仍然没有运气 . 它仍然说码头不可用 .

这是我的.gitlab-ci.yml文件

stages:
 - build

build:
  stage: build
  image: node:10.9.0
  script:
    - npm install -g @angular/cli
    - npm install -g typescript
    - npm install
    - ng build --prod
    - docker image build -t tag/to/image .
    - docker push tag/to/image
  tags:
    - angular
  cache:
    paths:
      - node_modules/
  artifacts:
    expire_in: 1 week
    paths:
      - dist/*
  only:
    - master

这是我的nginx.conf文件

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    server {
        listen 80;
        server_name  localhost;

        root   /usr/share/nginx/html;
        index  index.html index.htm;
        include /etc/nginx/mime.types;

        gzip on;
        gzip_min_length 1000;
        gzip_proxied expired no-cache no-store private auth;
        gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        location / {
            try_files $uri $uri/ /index.html;
        }
    }
}

这是我想用来构建的Dockerfile

FROM nginx:alpine

COPY nginx.conf /etc/nginx/nginx.conf

WORKDIR /usr/share/nginx/html
COPY dist/ .

2 回答

  • 1

    在我的项目中,我通常在Dockerfile中执行构建步骤并在我的.gitlab-ci.yml上使用 docker:image ,因此安装的Docker是我在运行器上需要的唯一依赖项 .

  • 2

    在gitlab文档中,有一个关于如何在Gitlab-CI管道中构建docker文件的参考 . 最清洁和最好的方式被描述为here,其工作极其顺利 .

    在我们的例子中,我们需要在管道中进行一些额外的编译,因此我们使用了python:3.6.5 docker image并在其中安装了docker .

    重要提示:确保您的gitlab-runner docker executor将'privileged'设置为true

    executor = "docker"
    [runners.docker]
      privileged = true
    

    首先,我们在gitlab-ci.yml的顶部定义了“install_docker”

    .install_docker: &install_docker |
          apt-get update
          apt-get -y install apt-transport-https ca-certificates curl software-properties-common
          curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
          add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
          apt-get update
          apt-get -y install docker-ce
    

    然后我们在工作中需要时使用它

    script:
      - *install_docker
    

相关问题