Home > Mobile >  How to use docker inside Alpine/Any docker image in gitlab ci
How to use docker inside Alpine/Any docker image in gitlab ci

Time:06-23

I want to build and test my app using dockerfile located in other private repository. For that I'm using Alpine official docker image in which i run a bash script for cloning my private repo and running docker for building the docker image. This is how my .gitlab-ci.yml looks like.

    image: alpine:3.15
stages:
  - main

main-job:
  stage: main
  script:
    - apk add --update docker openrc
    - rc-update add docker boot
    - apk add bash git curl
    - bash build.sh $GH_TOKEN $REPO

And I have simple script in build.sh

git clone https://${GH_TOKEN}@github.com/${REPO} source
cd source || exit 1
docker container prune --force || true
docker build . --rm --force-rm --compress --no-cache=true --pull --file Dockerfile -t test-app
docker image ls
docker run --privileged --rm -i test-app

But Docker don't start and spams error.

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

Also tried with other command in ubuntu docker like service start docker , dockerd, service restart docker and others. But nothing seems to works as i guess we can't run docker inside a docker or something.

Can we have any alternative idea to it?

CodePudding user response:

I‘d suggest you build and push your built image to dockerhub. Then you can start the container referencing your prebuilt image.

CodePudding user response:

Looks like you don’t have a docker agent running. You can use the docker in docker service by adding the following:

services:
  - docker:dind

See the GitLab-ci docs on building docker images for more info: https://docs.gitlab.com/ee/ci/docker/using_docker_build.html

CodePudding user response:

I've meet the same issue. May be you have to permit gitlab-runner on your host.

sudo usermod -aG docker gitlab-runner
  • Related