Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building Docker Image within Gitlab Runner Container

I am trying to setup a build server that is running Ubuntu Linux 18.04 as a docker host.

host has three docker containers running - Docker Registry - Gitlab Server - Gitlab Runner (to build Angular Apps)

I want Gitlab Runner container to build docker image with nginx and compiled Angular code and push it to Docker Registry.

I have manage to setup all three containers running and Gitlab runner is building angular project but challenge I am facing is building docker image in Gitlab Runner container.

Docker command is not available within Gitlab Runner container to build docker image.

Is this possible ??

enter image description here

I have tried to install docker.io within Gitlab Runner container so after time of build, it can have docker command available but still not luck. It still says docker not available.

Here is my .gitlab-ci.yml file

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

here is my nginx.conf file

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;
        }
    }
}

here is the Dockerfile I want to use to make a build

FROM nginx:alpine

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

WORKDIR /usr/share/nginx/html
COPY dist/ .
like image 857
microchip78 Avatar asked Jul 14 '26 08:07

microchip78


1 Answers

In the gitlab documentation there is a reference to how to build docker files in the Gitlab-CI pipelines. The cleanest and nicest way is described here which works extremly well.

In our case, we needed some extra compiling in our pipeline so we used python:3.6.5 docker image and just installed the docker in it.

Important: Make sure your gitlab-runner docker executor has 'privileged' set to true

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

First we defined the "install_docker" at the top of the gitlab-ci.yml

.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

Then we use it when needed inside the job

script:
  - *install_docker
like image 126
Totem Avatar answered Jul 16 '26 09:07

Totem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!