3

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/ .
3
  • Possible duplicate of can not run docker latest on gitlab-ci runner Commented Sep 10, 2018 at 0:56
  • You are using image: node:10.9.0 in your build step. That image does not contain docker executable, so you cannot execute docker commands in it. Commented Sep 11, 2018 at 7:07
  • I know node image I am using doesn't have docker in it. But if I want to run docker while building, how can I install docker before executing docker command on node image Commented Sep 11, 2018 at 21:02

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

So you took python image ( as I took node image in my case ) and then run install docker before running docker command ... Is that correct ?? If you can provide me example of how and where you write script block, that would be nice ...
I copied the .install_docker block straight from our gitlab ci yml. Just insert the call to the script (*install_docker) before your first npm install command
1

On my projects I usually do the building steps inside the Dockerfile and use the docker:image on my .gitlab-ci.yml so the Docker installed is the only dependency I need on the runner.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.