Sfoglia il codice sorgente

fix: Don't remove pytest container when running with host network mode

When the container runs with host networking instead of the default bridge, the `$HOSTNAME` / `/etc/hostname` reflects that of the host instead of the container ID , which causes the pytest container to get removed accidentally.

Using a container name instead we can more reliably target the container to avoid removing it, should we need to run with host networking instead.
polarathene 3 anni fa
parent
commit
b2b4c71997
2 ha cambiato i file con 9 aggiunte e 7 eliminazioni
  1. 4 2
      test/conftest.py
  2. 5 5
      test/pytest.sh

+ 4 - 2
test/conftest.py

@@ -28,7 +28,9 @@ FORCE_CONTAINER_IPV6 = False  # ugly global state to consider containers' IPv6 a
 
 
 docker_client = docker.from_env()
-test_container = socket.gethostname()
+
+# Name of pytest container to reference if it's being used for running tests
+test_container = 'nginx-proxy-pytest'
 
 
 ###############################################################################
@@ -260,7 +262,7 @@ def restore_urllib_dns_resolver(getaddrinfo_func):
 
 def remove_all_containers():
     for container in docker_client.containers.list(all=True):
-        if PYTEST_RUNNING_IN_CONTAINER and container.id.startswith(test_container):
+        if PYTEST_RUNNING_IN_CONTAINER and container.name == test_container:
             continue  # pytest is running within a Docker container, so we do not want to remove that particular container
         logging.info(f"removing container {container.name}")
         container.remove(v=True, force=True)

+ 5 - 5
test/pytest.sh

@@ -18,8 +18,8 @@ docker build -t nginx-proxy-tester -f "${DIR}/requirements/Dockerfile-nginx-prox
 
 # run the nginx-proxy-tester container setting the correct value for the working dir in order for 
 # docker-compose to work properly when run from within that container.
-exec docker run --rm -it \
---volume /var/run/docker.sock:/var/run/docker.sock \
---volume "${DIR}:${DIR}" \
---workdir "${DIR}" \
-nginx-proxy-tester "${ARGS[@]}"
+exec docker run --rm -it --name "nginx-proxy-pytest" \
+  --volume "/var/run/docker.sock:/var/run/docker.sock" \
+  --volume "${DIR}:${DIR}" \
+  --workdir "${DIR}" \
+  nginx-proxy-tester "${ARGS[@]}"