Last active
September 25, 2023 10:07
-
-
Save hmajid2301/592b3cd79b5222d523bd32c436445426 to your computer and use it in GitHub Desktop.
This is an example of how you can use docker-compose within Gitlab CI. Here I am running integration tests, where the tests themselves kill and start Docker containers.
The `docker-compose.test.yml` starts up the container in which we will run our te
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tests:integration: | |
stage: test | |
image: hmajid2301/dind-docker-compose | |
script: | |
- sh tests/run_integration_tests.sh; | |
after_script: | |
- docker-compose -f docker-compose.test.yml down |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: "3" | |
services: | |
test_app: | |
container_name: my_container | |
build: | |
context: . | |
dockerfile: docker/flask/Dockerfile | |
volumes: | |
- /var/run/docker.sock:/var/run/docker.sock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: "3" | |
services: | |
service1: | |
container_name: container1 | |
build: | |
context: tests | |
dockerfile: Dockerfile1 | |
ports: | |
- 88:88 | |
service2: | |
container_name: container2 | |
build: | |
dockerfile: Dockerfile2 | |
ports: | |
- 99:99 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
docker-compose -f docker-compose.test.yml up --build -d | |
docker exec my_container bash -c "pip install pytest pytest-flask docker-compose docker lovely-pytest-docker" | |
docker cp ./tests/ my_contianer:/app/ | |
docker cp ./docker-compose.yml my_container:/app/ | |
docker exec my_container ash -c "pytest -s tests/test_integration.py" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import time | |
from unittest import mock | |
import docker as docker_py | |
import pytest | |
from my_app import create_app | |
docker_client = docker_py.from_env() | |
docker_compose_network_name = None | |
docker_compose = None | |
def kill_container(container_name): | |
container = get_container(container_name) | |
container.kill() | |
container.remove() | |
def get_container(container_name): | |
containers = docker_client.containers.list() | |
for container in containers: | |
if container.name == container_name: | |
return container | |
def run_container(image_name, **args): | |
docker_client.containers.run(image_name, detach=True, **args) | |
@pytest.fixture(scope="session") | |
def docker_compose_files(pytestconfig): | |
"""Get the docker-compose.yml absolute path. | |
Override this fixture in your tests if you need a custom location. | |
""" | |
return ["/app/docker-compose.yml"] | |
@pytest.fixture(scope="session", autouse=True) | |
def docker(docker_services): | |
global docker_compose | |
docker_compose = docker_services | |
@pytest.fixture(scope="session", autouse=True) | |
def setup(): | |
service_names = ["service1", "service2"] | |
[docker_compose.start(service) for service in service_names] | |
containers = docker_client.containers.list() | |
yield | |
container_names = ["container1", "container2"] | |
try: | |
[kill_container(container) for container in container_names] | |
except AttributeError: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment