Skip to content

Commit d22a932

Browse files
authored
Docker Compose: give build container access to the readthedocs net (#12351)
When working locally, we give access to the `readthedocs` Docker network to the container created to perform the build. This gives that container access to hit resources like `web:8000`, which is used by the healthcheck API. With this, we don't need NGROK to be able to hit the servers 👍🏼
1 parent 3cc91c4 commit d22a932

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

readthedocs/doc_builder/environments.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,17 @@ def create_container(self):
825825
time_limit=self.container_time_limit,
826826
mem_limit=self.container_mem_limit,
827827
)
828+
829+
networking_config = None
830+
if settings.RTD_DOCKER_COMPOSE:
831+
# Create the container in the same network the web container is
832+
# running, so we can hit its healthcheck API.
833+
networking_config = client.create_networking_config(
834+
{
835+
settings.RTD_DOCKER_COMPOSE_NETWORK: client.create_endpoint_config(),
836+
}
837+
)
838+
828839
self.container = client.create_container(
829840
image=self.container_image,
830841
command=(
@@ -839,6 +850,7 @@ def create_container(self):
839850
detach=True,
840851
user=settings.RTD_DOCKER_USER,
841852
runtime="runsc", # gVisor runtime
853+
networking_config=networking_config,
842854
)
843855
client.start(container=self.container_id)
844856

@@ -863,22 +875,9 @@ def _run_background_healthcheck(self):
863875
build_id = self.build.get("id")
864876
build_builder = self.build.get("builder")
865877
healthcheck_url = reverse("build-healthcheck", kwargs={"pk": build_id})
866-
if settings.RTD_DOCKER_COMPOSE and "ngrok" in settings.PRODUCTION_DOMAIN:
867-
# NOTE: we do require using NGROK here to go over internet because I
868-
# didn't find a way to access the `web` container from inside the
869-
# container the `build` container created for this particular build
870-
# (there are 3 containers involved locally here: web, build, and user's build)
871-
#
872-
# This shouldn't happen in production, because we are not doing Docker in Docker.
873-
url = f"http://readthedocs.ngrok.io{healthcheck_url}"
874-
else:
875-
url = f"{settings.SLUMBER_API_HOST}{healthcheck_url}"
876-
877-
# Add the builder hostname to the URL
878-
url += f"?builder={build_builder}"
879-
878+
url = f"{settings.SLUMBER_API_HOST}{healthcheck_url}?builder={build_builder}"
880879
cmd = f"/bin/bash -c 'while true; do curl --max-time 2 -X POST {url}; sleep {settings.RTD_BUILD_HEALTHCHECK_DELAY}; done;'"
881-
log.debug("Healthcheck command to run.", command=cmd)
880+
log.info("Healthcheck command to run.", command=cmd)
882881

883882
client = self.get_client()
884883
exec_cmd = client.exec_create(

readthedocs/settings/docker_compose.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class DockerBaseSettings(CommunityBaseSettings):
1212

1313
DOCKER_ENABLE = True
1414
RTD_DOCKER_COMPOSE = True
15+
RTD_DOCKER_COMPOSE_NETWORK = "community_readthedocs"
1516
RTD_DOCKER_COMPOSE_VOLUME = "community_build-user-builds"
1617
RTD_DOCKER_USER = f"{os.geteuid()}:{os.getegid()}"
1718
DOCKER_LIMITS = {"memory": "2g", "time": 900}

0 commit comments

Comments
 (0)