You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
testcontainers wait indefinitely without effects when running in Gitlab-CI #43
I'm running a test based on testcontainers-python in a container running in Gitlab CI.
Basically the CI spawns a docker-in-docker container next to the container running the tests. I then set the environment variable DOCKER_HOST so that it can be used by testcontainers.
But in practice the test just get stuck, so I ran it with pytest -s to see the logs of testcontainers and got:
tests/test.py
Pulling image postgres:10
Container started: 4a1115dc98
Waiting to be ready...
And then it gets stuck indefinitely (I waited 20 minutes).
For the record, testcontainers-java works perfectly with this same situation, so it should be possible to make this work.
I'm not sure how I can get more debug information to understand what is happening, but shouldn't it timeout normally at least?
Reacted by Greg Hilston, ismail simsek and Sergei Borisov
$ python test.py
Pulling image mysql:5.7.17
Container started: f2ea27afde
Waiting to be ready...
Traceback (most recent call last):
File "test.py", line 12, in <module>
config = MySqlContainer('mysql:5.7.17').start()
File "/root/.local/lib/python3.6/site-packages/testcontainers/core/generic.py", line 49, in start
self._connect()
File "/root/.local/lib/python3.6/site-packages/wrapt/wrappers.py", line 603, in __call__
args, kwargs)
File "/root/.local/lib/python3.6/site-packages/testcontainers/core/waiting_utils.py", line 50, in wrapper
args, kwargs, exception))
testcontainers.core.exceptions.TimeoutException: Wait time exceeded 120 sec.
Method _connect, args () , kwargs {}.
Exception (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on '172.18.0.2' (timed out)")
(Background on this error at: http://sqlalche.me/e/e3q8)
@tuvshuud it's not Gitlab-CI but Docker images based on official Python images where this freezing happens (using -v /var/run/docker.sock:/var/run/docker.sock argument to docker run). Didn't create a new bug as it seemed related.
@tillahoffmann Do you think we need more verbosity in _connect() , so that others can troubleshoot easily? Because of missing packages, wrong container ip address or port that is being used, testcontainer process may hang until timeout.
There may be an opportunity to update the exceptions we catch in wait_container_is_ready to only include TimeoutErrors, for example. In other words, any exception that is not a TimeoutError, such as an ImportError, would just be raised as normal. Although we'd have to make sure we don't break anything by accident. What do you think?
Pulling image postgres:latest
⠏
Container started: 2010c8ddfd
Waiting to be ready...
⠏E
======================================================================
ERROR: test (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: test
Traceback (most recent call last):
File "/usr/local/lib/python3.8/unittest/loader.py", line 436, in _find_test_path
module = self._get_module_from_name(name)
File "/usr/local/lib/python3.8/unittest/loader.py", line 377, in _get_module_from_name
__import__(name)
File "/app/tests/test.py", line 13, in <module>
postgres = postgres_container.start()
File "/usr/local/lib/python3.8/site-packages/testcontainers/core/generic.py", line 42, in start
self._connect()
File "/usr/local/lib/python3.8/site-packages/testcontainers/core/waiting_utils.py", line 46, in wrapper
raise TimeoutException(
testcontainers.core.exceptions.TimeoutException: Wait time exceeded 120 sec.
Method _connect, args () , kwargs {}.
Exception (psycopg2.OperationalError) could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 32788?
could not connect to server: Address not available
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 32788?
(Background on this error at: http://sqlalche.me/e/e3q8)
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (errors=1)
@karmux#96 merged into master but hasn’t released into patch version yet. Meanwhile you may try with pip install git+https://github.com/testcontainers/testcontainers-python.git
3.0.3 is now on pypi so you should be able to revert to pip install testcontainers without the need to install from master. Thanks for the fix, @tuvshuud.
@tillahoffmann Mine is still hanging. I updated testcontainer to 3.0.3 from above link. Not sure what am i doing wrong.
rootdir: /Users/ypatel/postgres
collecting ... testing
collected 1 item
test_db_containers.py
Pulling image localhost/opensource/postgres/postgresql10:10.12
⠼
Container started: 75a7bd9cd9
Waiting to be ready...
F
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/testcontainers/core/container.py:64: in enter
return self.start()
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/testcontainers/core/generic.py:42: in start
self._connect()
wrapped = <bound method DbContainer._connect of <testcontainers.postgres.PostgresContainer object at 0x7feb9af0aeb0>>
instance = <testcontainers.postgres.PostgresContainer object at 0x7feb9af0aeb0>, args = (), kwargs = {}
@wrapt.decorator
def wrapper(wrapped, instance, args, kwargs):
exception = None
print(crayons.yellow("Waiting to be ready..."))
with blindspin.spinner():
for _ in range(0, config.MAX_TRIES):
try:
return wrapped(*args, **kwargs)
except Exception as e:
time.sleep(config.SLEEP_TIME)
exception = e
raise TimeoutException(
"""Wait time exceeded {0} sec.
Method {1}, args {2} , kwargs {3}.
Exception {4}""".format(config.MAX_TRIES,
wrapped.__name__,
args, kwargs, exception))
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/testcontainers/core/waiting_utils.py:46: TimeoutException
====================================================================== short test summary info =======================================================================
FAILED test_db_containers.py::test_docker_run_postgress - testcontainers.core.exceptions.TimeoutException: Wait time exceeded 120 sec.
In my case I am running MySQL Test container within docker, and when I do MysqlContainer.start(), it tries by default to connect to docker_host ip, which is causing the issue.
Do I have to add some flag in docker run command?
In CI this was resolved my passing --network=host in docker run command, but I am still facing issues with running it in my local system. I am working on mac
@tuvshuud I spent several hours trying to figure this issue out and once I pulled in your improve-host-detection branch it all started working as expected. I would love to see this fix merged. Any thoughts what we can do to help make that happen?
In my case I had a pytest fixture that works perfectly fine when run locally. However, when I tried to run it via docker-compose I see the db container come up, but SQLAlchemy fromthe original container is never able to reach it: Exception (psycopg2.OperationalError) connection to server at "172.17.0.1", port 55582 failed: Connection refused
I am happy to know there is a solution. Would be great if this solution were merged.
@willgme happy to hear that it resolves your issue. To be merged, we need to refactor some codes to pass the all test cases and test on windows and other DinD environments. I just don't have time these days
Activity
victornoel commentedon Nov 28, 2019
See also #25 for a different but maybe related problem and https://www.testcontainers.org/supported_docker_environment/continuous_integration/gitlab_ci/ for the how to use testcontainers-java in gitlab-ci
GregHilston commentedon Jan 9, 2020
I currently get a timeout
I'm using the
mysqlcontainer and the exact log output is:I can see the container running when I check with
$ docker ps. Also, I can make a new issue if that's helpfulGregHilston commentedon Jan 9, 2020
Turns out I can't read and apologize for posting in the first place.
$ pip3 install pymysqlfixed everything haismailsimsek commentedon Feb 10, 2020
having same issue with gitlab cicd
ismailsimsek commentedon Feb 21, 2020
im using docker:dind with gitlab cicd pipeline.
adding wait for mysql to boot and changing mysql host-name to 'docker' solved my problem
tillahoffmann commentedon Apr 28, 2020
Fixed in #85. Feel free to reopen if problems persist.
victornoel commentedon Apr 28, 2020
Excellent, I can confirm this works :)
karmux commentedon May 21, 2020
Version 3.0 gets stuck for me at the same place but 2.6 works fine when using docker-in-docker with mounted socket.
tuvshuud commentedon May 23, 2020
@karmux I will double check. Meanwhile, can you provide your .gitlab-ci.yml here?
karmux commentedon May 28, 2020
@tuvshuud it's not Gitlab-CI but Docker images based on official Python images where this freezing happens (using
-v /var/run/docker.sock:/var/run/docker.sockargument todocker run). Didn't create a new bug as it seemed related.tillahoffmann commentedon May 29, 2020
@karmux, could you post a minimal reproducible example please so we can have a look?
karmux commentedon May 29, 2020
@tillahoffmann
Dockerfile:
tests/test.py:
Docker build:
Docker run:
@tuvshuud , sorry PR #96 didn't fix it.
If I install
testcontainers[postgresql]==2.6then it works.tuvshuud commentedon Jun 2, 2020
As @tillahoffmann mentioned here, you have to install required additional dependencies explicitly.
So your
Dockerfileshould haveSQLAlchemy,psycopg2packages installed.I've checked and #96 fixed it. @karmux
@tillahoffmann Do you think we need more verbosity in _connect() , so that others can troubleshoot easily? Because of missing packages, wrong container ip address or port that is being used, testcontainer process may hang until timeout.
tillahoffmann commentedon Jun 3, 2020
There may be an opportunity to update the exceptions we catch in
wait_container_is_readyto only includeTimeoutErrors, for example. In other words, any exception that is not aTimeoutError, such as anImportError, would just be raised as normal. Although we'd have to make sure we don't break anything by accident. What do you think?testcontainers-python/testcontainers/core/waiting_utils.py
Lines 40 to 45 in 87fb2c1
1 remaining item
karmux commentedon Jun 4, 2020
Still waiting till it gets timeout with 3.0.2.
tuvshuud commentedon Jun 5, 2020
@karmux #96 merged into master but hasn’t released into patch version yet. Meanwhile you may try with
pip install git+https://github.com/testcontainers/testcontainers-python.gitkarmux commentedon Jun 5, 2020
Works now! Thank you @tuvshuud and @tillahoffmann!
tillahoffmann commentedon Jun 8, 2020
3.0.3is now on pypi so you should be able to revert topip install testcontainerswithout the need to install frommaster. Thanks for the fix, @tuvshuud.ypatel26 commentedon Jun 8, 2020
@tillahoffmann Mine is still hanging. I updated testcontainer to 3.0.3 from above link. Not sure what am i doing wrong.
rootdir: /Users/ypatel/postgres
collecting ... testing
collected 1 item
test_db_containers.py
Pulling image localhost/opensource/postgres/postgresql10:10.12
⠼
Container started: 75a7bd9cd9
Waiting to be ready...
F
============================================================================== FAILURES ==============================================================================
_____________________________________________________________________ test_docker_run_postgress ______________________________________________________________________
test_db_containers.py:20:
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/testcontainers/core/container.py:64: in enter
return self.start()
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/testcontainers/core/generic.py:42: in start
self._connect()
wrapped = <bound method DbContainer._connect of <testcontainers.postgres.PostgresContainer object at 0x7feb9af0aeb0>>
instance = <testcontainers.postgres.PostgresContainer object at 0x7feb9af0aeb0>, args = (), kwargs = {}
E testcontainers.core.exceptions.TimeoutException: Wait time exceeded 120 sec.
E Method _connect, args () , kwargs {}.
E Exception (psycopg2.OperationalError) server closed the connection unexpectedly
E This probably means the server terminated abnormally
E before or while processing the request.
E
E (Background on this error at: http://sqlalche.me/e/e3q8)
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/testcontainers/core/waiting_utils.py:46: TimeoutException
====================================================================== short test summary info =======================================================================
FAILED test_db_containers.py::test_docker_run_postgress - testcontainers.core.exceptions.TimeoutException: Wait time exceeded 120 sec.
aashish-agarwal-de commentedon Jun 5, 2021
In my case I am running MySQL Test container within docker, and when I do MysqlContainer.start(), it tries by default to connect to docker_host ip, which is causing the issue.
Do I have to add some flag in docker run command?
tuvshuud commentedon Jun 5, 2021
@aashish-agarwal-de you can use TC_HOST environment variable to set correct host address if host detection in the library is not working correctly.
aashish-agarwal-de commentedon Jun 5, 2021
It is detecting the gateway host, byt sql alchemy is not able to connect to that ip.
It is only able to connect to host.docker.internal
aashish-agarwal-de commentedon Jun 5, 2021
Is there a way to pass the hostname to MySQL test container, so that it does try to connect to gatwway ip in docker?
aashish-agarwal-de commentedon Jun 5, 2021
In CI this was resolved my passing --network=host in docker run command, but I am still facing issues with running it in my local system. I am working on mac
tharwan commentedon Oct 13, 2021
I am also working on a Mac and I am facing the same issue
tuvshuud commentedon Oct 24, 2021
@tharwan @aashish-agarwal-de I was able reproduce this issue and made changes. Can you confirm if this works for you?
tuvshuud commentedon Oct 24, 2021
There is a bug that TC_HOST environment variable is not respected when set in docker in docker environment.
willgme commentedon Feb 10, 2022
@tuvshuud I spent several hours trying to figure this issue out and once I pulled in your
improve-host-detectionbranch it all started working as expected. I would love to see this fix merged. Any thoughts what we can do to help make that happen?In my case I had a
pytestfixture that works perfectly fine when run locally. However, when I tried to run it viadocker-composeI see the db container come up, but SQLAlchemy fromthe original container is never able to reach it:Exception (psycopg2.OperationalError) connection to server at "172.17.0.1", port 55582 failed: Connection refusedI am happy to know there is a solution. Would be great if this solution were merged.
tuvshuud commentedon Feb 10, 2022
@willgme happy to hear that it resolves your issue. To be merged, we need to refactor some codes to pass the all test cases and test on windows and other DinD environments. I just don't have time these days