Skip to content

fix: make labs work on a "From Sources" install - #583

Open
PRAteek-singHWY wants to merge 1 commit into
adeyosemanputra:masterfrom
PRAteek-singHWY:fix/source-install-labs
Open

PRAteek-singHWY wants to merge 1 commit into
adeyosemanputra:masterfrom
PRAteek-singHWY:fix/source-install-labs

Conversation

@PRAteek-singHWY

Copy link
Copy Markdown

Summary

Labs couldn't be started on a "From Sources" install. PyGoat now creates the my_network Docker network when it's missing, and the README's Traefik steps now work on Linux and on Docker Desktop (macOS, Windows). I tested it end to end with fresh installs on Linux and macOS, and Docker Compose still works the same.

Problem / Motivation

  • Lab containers go on the my_network network, but only Docker Compose creates it, so on a source install every lab start failed with "network my_network not found".
  • On Docker Desktop, a Traefik binary running on the host can't reach the lab containers, so labs timed out with 504 even once the network existed.
  • The Windows steps started Traefik from its own folder, so it never loaded traefik.yml and PyGoat reported "Traefik reverse proxy is not reachable".
  • Traefik v3.3 and older can't talk to Docker 29, so they never pick up lab containers. The README didn't say which version to use.

Fixes #582

Solution / Approach

  • Network: a new _ensure_lab_network() returns the lab network (still DOCKER_NETWORK, default my_network) and creates it as a bridge network if it doesn't exist. _create_lab_container calls it before creating the container. When it already exists, which is always the case under Docker Compose, nothing changes.
  • Creating it only once: while testing I found that two lab starts at the same moment both create the network. Docker then has two networks called my_network and every lab start fails with "network my_network is ambiguous". So the creation runs under a file lock, the same kind _user_lab_lock already uses, and checks again once it has the lock. I moved the lock code into a small _file_lock() helper so both locks share it. _user_lab_lock works the same as before.
  • README: Traefik is started with --configFile=traefik.yml from the PyGoat folder, the Linux steps ask for v3.6 or later, and Docker Desktop users get a single docker run command (works in Bash and PowerShell) that runs Traefik in Docker on my_network.

Files Changed

File Change
challenge/views.py Add _ensure_lab_network() and use it in _create_lab_container. Move the lock code into _file_lock()
challenge/tests.py Tests for creating a missing network, reusing an existing one, the DOCKER_NETWORK setting, and parallel lab starts creating the network only once
README.md Rewrite the Traefik part of "From Sources" for Linux and Docker Desktop

Impact

  • Docker Compose installs are unaffected, since my_network already exists there. I ran the Compose stack with this branch: labs start, the owner gets the lab, anonymous gets 401 and another user gets 403, and there's still one my_network.
  • Fresh source install on Linux, Debian with its own Docker daemon, following the new README: the first lab start creates my_network and the lab loads through Traefik. Starting three labs, stopping one or all, and restarting PyGoat all work.
  • Fresh source install on macOS with Docker Desktop and Docker 29, with the new README commands: labs start and load through Traefik.
  • Two users starting labs at the same moment with no network: without the lock this made two networks in 3 out of 3 runs, and with it there was exactly one network in every run, both with runserver and with gunicorn running 4 workers.
  • The full test suite passes (59 tests). The new tests for the missing network fail on master, and the parallel test fails if the lock is removed. flake8 shows no new warnings.
  • I couldn't test on Windows itself. The Docker Desktop command is the same one I ran on macOS, and the old Windows steps fail for the reason I reproduced on Linux.

Lab containers go on the my_network Docker network, but only Docker
Compose creates it, so on a source install every lab start failed with
"network my_network not found". PyGoat now creates the network when
it's missing. Creation runs under a file lock, because two lab starts
at the same moment otherwise create two networks with the same name and
Docker can then start no lab at all.

The README's Traefik steps didn't work either: on Docker Desktop a
Traefik binary on the host can't reach the lab containers, the Windows
steps never loaded traefik.yml, and Traefik v3.3 and older can't talk
to Docker 29. Traefik now starts with --configFile=traefik.yml, the
Linux steps ask for v3.6 or later, and Docker Desktop users run Traefik
in Docker on my_network.

Fixes adeyosemanputra#582

Signed-off-by: PRAteek-singHWY <prateek23022004@gmail.com>
@PRAteek-singHWY

Copy link
Copy Markdown
Author

Hi @RupakBiswas-2304 This PR fixes #582, where none of the labs could be started on a "From Sources" install.
PyGoat now creates the my_network Docker network when it's missing, under a lock so two lab starts at the same moment can't create two networks with the same name. The README's Traefik steps are updated too: Traefik loads PyGoat's traefik.yml, the Linux steps ask for v3.6 or later since older versions can't talk to Docker 29, and Docker Desktop users run Traefik in Docker because a binary on the host can't reach the lab containers.

I tested it end to end with fresh source installs on Linux and macOS, and the Docker Compose setup still works the same.

Happy to iterate based on your review.
Thank you.

This branch has not been deployed

No deployments
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.

[Bug]: Labs can't be started on a "From Sources" install (missing my_network, and the Traefik steps don't work on Docker Desktop)

1 participant