Docker is recently producing a randomized mapped port that fails with a 404

I work on a build process that has been slowly evolving over the last few years. The applications are all SpringBoot projects built by Maven. During the build process, The Jenkins pipeline script creates a Docker image and then we run a “smoke test” where the build simply runs the image, and then the build script uses “inspect” to get the random port that Docker mapped to 8080 within the container, and then it tries to curl to a heartbeat actuator. This has been running fine like this for many services for quite a while.

Recently, there have been a small number of services that simply get a 404 on the heartbeat call. One of those services simply worked again when the build was rerun, without changing anything. For the other services, this behavior is consistent.

I tried changing the build script so that in addition to making the curl call from “outside” the container to the random port, I also used “docker exec” to run the same curl call to the actuator, using the internal port of 8080. What I’m finding is that the internal curl call is working fine, but the “outside” call to the random mapped port is getting a 404.

None of this process has anything that is specific to the service being built. It does the same thing, albeit getting a random port, for every service build.

I may just end up changing the build process so it uses the result from the “internal” curl, but I’m concerned that I don’t understand why the random port isn’t working in some cases.

I’m just guessing, but is it possible the algorithm that Docker uses to determine the port doesn’t account for something else running on the host that is listening on that port? Even if that were the case, it’s odd that the failure scenario is ALWAYS a 404.

I could use any ideas for tracking this down.