Skip to content

Bug: get_exposed_port() waits forever, but is fixed by starting the container twice #709

Closed
@vemonet

Description

@vemonet

Describe the bug

Using container.get_exposed_port() waits forever. Adding an additional container.start() starts a 2nd container, but somehow fixes the issue

To Reproduce

with DockerContainer(TRIPLESTORE_IMAGE) as container:
    container.with_exposed_ports(7200)
    delay = wait_for_logs(container, "Started GraphDB")
    print("DELAY OK", delay)
    host = container.get_container_host_ip()
    print("HOST OK", host)
    port = container.get_exposed_port(7200)
    # STUCK HERE
    print("PORT OK", port)
    base_url = f"http://{host}:{port}"
    print(f"GraphDB started in {delay:.0f}s at {base_url}")

Output (wait forever after last line):

tests/test_graphdb.py using host unix:///run/user/1000/docker.sock
using host unix:///run/user/1000/docker.sock
Pulling image testcontainers/ryuk:0.8.1
Container started: 158a1102d52d
Waiting for container <Container: 158a1102d52d> with image testcontainers/ryuk:0.8.1 to be ready ...
Pulling image ontotext/graphdb:10.7.4
Container started: a478700f1fb6
DELAY OK 12.14159607887268
HOST OK localhost
Waiting for container <Container: a478700f1fb6> with image ontotext/graphdb:10.7.4 to be ready ...

Funny dirty fix: you can make it work by adding a container.start(). It starts a second container, but for some reason the wait_for_logs then behave as expected (take ~16s for me)

from testcontainers.core.container import DockerContainer
from testcontainers.core.waiting_utils import wait_for_logs

with DockerContainer("ontotext/graphdb:10.7.4") as container:
    container.with_exposed_ports(7200)
    container.start()
    delay = wait_for_logs(container, "Started GraphDB")
    base_url = f"http://{container.get_container_host_ip()}:{container.get_exposed_port(7200)}"
    print(f"GraphDB started in {delay:.0f}s at {base_url}")

Output:

tests/test_graphdb.py using host unix:///run/user/1000/docker.sock
using host unix:///run/user/1000/docker.sock
Pulling image testcontainers/ryuk:0.8.1
Container started: 5c72a83ac187
Waiting for container <Container: 5c72a83ac187> with image testcontainers/ryuk:0.8.1 to be ready ...
Pulling image ontotext/graphdb:10.7.4
Container started: a6784a71d471
Pulling image ontotext/graphdb:10.7.4
Container started: 2ab2ceeb17fb
Waiting for container <Container: 2ab2ceeb17fb> with image ontotext/graphdb:10.7.4 to be ready ...
GraphDB started in 15s

Runtime environment

Provide a summary of your runtime environment. Which operating system, python version, and docker version are you using? What is the version of `testcontainers-python` you are using? You can run the following commands to get the relevant information.
# Get the operating system information (on a unix os).
$ uname -a
Linux vit-404 6.8.0-103045-tuxedo #45~22.04.1tux1 SMP PREEMPT_DYNAMIC Thu Sep 12 00:07:22 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

# Get the python version.
$ python --version
Python 3.10.15

# Get the docker version and other docker information.
$ docker info
Client: Docker Engine - Community
 Version:    27.3.1
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.17.1
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.29.7
    Path:     /usr/libexec/docker/cli-plugins/docker-compose

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 46
 Server Version: 27.3.1
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  userxattr: true
 Logging Driver: json-file
 Cgroup Driver: systemd
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 7f7fdf5fed64eb6a7caf99b3e12efcf9d60e311c
 runc version: v1.1.14-0-g2c9f560
 init version: de40ad0
 Security Options:
  seccomp
   Profile: builtin
  rootless
  cgroupns
 Kernel Version: 6.8.0-103045-tuxedo
 Operating System: Ubuntu 22.04.5 LTS
 OSType: linux
 Architecture: x86_64
 CPUs: 20
 Total Memory: 31.09GiB
 Name: vit-404
 ID: ff990512-c6e3-473a-8751-b09df4f55130
 Docker Root Dir: /home/vemonet/.local/share/docker
 Debug Mode: false
 Experimental: false
 Insecure Registries:
  127.0.0.0/8

# Get all python packages.
$ pip freeze
anyio==4.6.0
backports.tarfile==1.2.0
certifi==2024.8.30
cffi==1.17.1
charset-normalizer==3.3.2
click==8.1.7
coverage==7.6.1
cryptography==43.0.1
distlib==0.3.8
docker==7.1.0
Dumper==1.2.0
exceptiongroup==1.2.2
filelock==3.16.1
h11==0.14.0
hatch==1.12.0
hatchling==1.25.0
httpcore==1.0.5
httpx==0.27.2
hyperlink==21.0.0
idna==3.10
importlib_metadata==8.5.0
iniconfig==2.0.0
jaraco.classes==3.4.0
jaraco.context==6.0.1
jaraco.functools==4.1.0
jeepney==0.8.0
keyring==25.4.1
kgsteward @ file:///home/vemonet/dev/sparql/kgsteward
markdown-it-py==3.0.0
mdurl==0.1.2
more-itertools==10.5.0
packaging==24.1
pathlib==1.0.1
pathspec==0.12.1
pexpect==4.9.0
platformdirs==4.3.6
pluggy==1.5.0
ptyprocess==0.7.0
pycparser==2.22
Pygments==2.18.0
pytest==8.3.3
pytest-cov==5.0.0
PyYAML==6.0.2
requests==2.32.3
rich==13.9.1
ruff==0.6.8
SecretStorage==3.3.3
shellingham==1.5.4
sniffio==1.3.1
termcolor==2.4.0
testcontainers==4.8.1
tomli==2.0.1
tomli_w==1.0.0
tomlkit==0.13.2
trove-classifiers==2024.9.12
typing_extensions==4.12.2
urllib3==2.2.3
userpath==1.9.2
uv==0.4.17
virtualenv==20.26.6
wrapt==1.16.0
zipp==3.20.2
zstandard==0.23.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions