Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry fetching mapped host port when a container is created #14

Merged
merged 1 commit into from
Jul 18, 2024

Conversation

hexedpackets
Copy link
Contributor

I've been running into a race condition where the host port mapping is not available right after a container is created. Attempting to inspect the container too quickly results in a JSON blob that looks roughly like:

  "NetworkSettings" => %{
    "Ports" => %{
      "8080/tcp" => []
  },

which in turn results in the following exception:

09:48:03.330 module=gen_server [error] GenServer #PID<0.554.0> terminating
** (BadMapError) expected a map, got: nil
    (elixir 1.16.3) lib/map.ex:535: Map.get(nil, "HostPort", nil)
    (excontainers 0.3.0) lib/docker/container_state.ex:34: anonymous fn/1 in Docker.ContainerState.port_mapping/1
    (elixir 1.16.3) lib/enum.ex:1708: anonymous fn/3 in Enum.map/2
    (stdlib 4.3.1.4) maps.erl:411: :maps.fold_1/3
    (elixir 1.16.3) lib/enum.ex:2540: Enum.map/2
    (excontainers 0.3.0) lib/docker/container_state.ex:34: Docker.ContainerState.port_mapping/1
    (excontainers 0.3.0) lib/docker/container_state.ex:28: Docker.ContainerState.parse_docker_response/1
    (excontainers 0.3.0) lib/docker/api/containers.ex:50: Docker.Api.Containers.inspect/1
Last message (from Excontainers.ResourcesReaper): {:mapped_port, 8080}
** (EXIT from #PID<0.93.0>) exited in: GenServer.call(#PID<0.554.0>, {:mapped_port, 8080}, 5000)
    ** (EXIT) an exception was raised:
        ** (BadMapError) expected a map, got: nil
            (elixir 1.16.3) lib/map.ex:535: Map.get(nil, "HostPort", nil)
            (excontainers 0.3.0) lib/docker/container_state.ex:34: anonymous fn/1 in Docker.ContainerState.port_mapping/1
            (elixir 1.16.3) lib/enum.ex:1708: anonymous fn/3 in Enum.map/2
            (stdlib 4.3.1.4) maps.erl:411: :maps.fold_1/3
            (elixir 1.16.3) lib/enum.ex:2540: Enum.map/2
            (excontainers 0.3.0) lib/docker/container_state.ex:34: Docker.ContainerState.port_mapping/1
            (excontainers 0.3.0) lib/docker/container_state.ex:28: Docker.ContainerState.parse_docker_response/1
            (excontainers 0.3.0) lib/docker/api/containers.ex:50: Docker.Api.Containers.inspect/1

I see this error very frequently with the ResourcesReaper container, but I don't think it's specific to that. Retrying after a small delay seems to consistently resolve the problem. This PR implements retry logic for all calls to Docker.Containers.mapped_port/2

As a side note, the tests are failing for me both with and without this change.

@dallagi
Copy link
Owner

dallagi commented Jul 18, 2024

Thanks @hexedpackets for the contribution!

I'm gonna merge this, but note that excontainers is currently not maintained, and I'd suggest you to consider switching to testcontainers-elixir instead.

I've never used it but it seems to be maintained and it should be pretty similar API-wise, since when it started it borrowed much of the code from this project.

@dallagi dallagi merged commit 3fa638d into dallagi:master Jul 18, 2024
1 check failed
lauragrechenko added a commit to lauragrechenko/excontainers that referenced this pull request Nov 1, 2024
* Fix CI (#5)

* Add auto remove flag (#4)

* Fix build status badge

* Relax version of tesla (#6)

* Pull protocol impl out of CommandWaitStrategy module (#7)

* Update CHANGELOG

* Prepare to release 0.3.1

* Trap exits in container (#8)

* Remove coveralls

* Relicense to MIT

* Retry fetching mapped host port when a container is created (dallagi#14)

* Relax tesla to allow up to 1.12.0 (dallagi#15)

* Format

* remove unused files, fix merge issues

---------

Co-authored-by: Marco DallaG <marco.dallagiacoma@gmail.com>
Co-authored-by: Damir Vandic <info@dvic.io>
Co-authored-by: William <hexedpackets@gmail.com>
Co-authored-by: lauragrechenko <laura.grechenko@erlang-solutions.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants