How to install docker in gitlab-ci-runner?

I am using a java:8 image to build a gradle project in gitlab.com. I want to install docker in the runner also to build a docker image out of the gradle built artifacts. The command I am using in .gitlab-ci.yml is as follows

- curl -sSL https://get.docker.com/ | sh
- newgrp docker
- docker info

Now at “docker info” the build is failing with the below error

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

So how do I install docker in a java:8 image? Or is there any gitlab ci image with java8 and docker built-in?

You are stepping into docker-in-docker space: https://docs.gitlab.com/ce/ci/docker/using_docker_build.html#using-docker-build

Also Google Docker-in-Docker

Thanks for the reply. But can you elaborate how do I configure the runner? I am running the CI in gitlab.com’s shared runner.

I dont think this is possible on the gitlab.com sharer runners, you should set up an private runner since I dont believe they would allow socket mounting on the shared service.

I made it work and here is the .gitlab-ci.yml for reference

image: docker:latest

services:
  - docker:dind

variables:
  DOCKER_DRIVER: overlay

stages:
  - build
  - package

gradle-build:
  image: java:8
  stage: build
  script:
    - ./gradlew build
  artifacts:
    paths:
      - build/*

docker-build:
  stage: package
  script:
    - cd build
    - docker build -t $IMAGE_NAME .
    - docker login -u $DOCKER_USER -p $DOCKER_PASS
    - docker push $IMAGE_NAME

Cool, good to know.