2

I have a Python program that uses the Docker Python SDK to run a container for a third-party docker image. Something like that in my code:

import docker
docker.from_env().containers.run(image="<other_image>")

My program is also packaged in Docker, and I would like to test with gitlab CI. So my .gitlab-ci.yml looks like that:

image: <my_image>:latest

stages:
  - Tests

pytest:
  stage: Tests
  script:
    - pytest

And the python code from above is covered by the tests. The problem is that the CI fails on docker.from_env() with docker.errors.DockerException: Error while fetching server API version: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory')).

I assume that is because there's no docker server accessible from within <my_image> container. I searched for solutions, and I found the docker:dind one + the "expose docker socket" i.e. /var/run/docker.sock:/var/run/docker.sock one, but I can't make it work.

All the examples that I found for docker:dind are for calling docker directly in the .gitlab-ci.yml, but I have found none that explains how to leverage it for using a Docker SDK in your own code. Also, the socket solution requires you to change the gitlab runner configuration, but I don't have access to it (I am using a gitlab free account and don't have the opportunity to host my own runner).

What I need is basically:

image:
  name: <my_image>:latest
  volumes:
    - /var/run/docker.sock:/var/run/docker.sock

But AFAIK it doesn't exist.

Is there any solution?

Update: I realize that this would actually work:

image: docker

services:
  - docker:dind

stages:
  - Tests

pytest:
  stage: Tests
  script:
    - docker run -v /var/run/docker.sock:/var/run/docker.sock <my_image>:latest pytest

But I would still be interested to know if it's possible to do it while still using my image as the base (for example, maybe I don't want to have pytest installed in my image, but I would do it with a before_script instruction in .gitlab-ci.yml... this I could also do in a script to be executed instead of running pytest directly, so I agree that this might not be a very good reason).

Adrien '
  • 21
  • 2

1 Answers1

0

You should be able to do this:

image: <your python/docker-SDK image>

pytest:
  services:
    - docker:dind
  variables:
    DOCKER_TLS_CERTDIR: ""
    DOCKER_HOST: tcp://docker:2375
  script:
    - pytest

Just like regular docker, the SDK will use these environment variables to configure the client correctly to use the docker:dind service.

sytech
  • 29,298
  • 3
  • 45
  • 86