avast / pytest-docker

Docker-based integration tests
MIT License
406 stars 68 forks source link

Docker in docker #96

Closed Ce11an closed 11 months ago

Ce11an commented 11 months ago

I have managed to implement pytest-docker to work locally with localstack - It's been great.

I would like some advice on what is considered best practice when running pytest-docker in a CI job. I'm specially working on GitLab, which does allow for docker in docker. However, it's proving difficult to implement. Any suggestions from past experiences would be helpful. Thanks 😊

n1ngu commented 11 months ago

I am running tests with pytest-docker on GitLabCI with no differences to local execution. What issues are you encountering?

Ce11an commented 11 months ago

I have a command: docker not found error when using a python 3.10 image in GitLab CI. I've explored using the docker in docker service along with the docker image, but using that you need to install Python to run the tests. As the docker image is alpine, a lot of build tools are required to be installed to install my Python dependencies. Lots of debugging... 😅

Are you using a similar approach? I am hoping I am missing something obvious!

I have realised this issue is similar to #65

n1ngu commented 11 months ago

You need both docker and python in the same job, so you either install python in a docker image or install docker in a python image.

This

test:
  image: docker:latest
  services:
    - docker:dind
  script:
    - apk update
    - apk add py3-pip
    - python3 -m pip install pytest-docker
    - python3 -m pytest

works for me but something like

test:
  image: python:latest
  services:
    - docker:dind
  script:
    - apt-get update
    - apt-get install --yes docker-compose
    - python3 -m pip install pytest-docker
    - python3 -m pytest

should work as well.

Chose whichever makes your life easier, e.g. what makes installing all your deps easier. Just maybe try to cache apt, apk, and pip litter in either case. Or maybe try to build a docker image that features all that beforehand.

Ce11an commented 11 months ago

Thanks. I have tried this, but can get very messy working out Python versions and I have some heavy ML Python dependencies that need several build tools. Definitely a solution for a more lightweight application!

I have opted for a solution using docker-compose without pytest-docker 😢

I will close this ticket - thanks, again!