From 963b96ff622df6aa0fb32991bd4288dc7955ee33 Mon Sep 17 00:00:00 2001 From: Chris Lowder Date: Sat, 4 Mar 2023 21:41:08 +0000 Subject: [PATCH] Customizable Traefik host port Allow users to free up port 80 on the host machine, without losing Traefik's Docker routing super-powers. --- README.md | 9 +++++++++ lib/mrsk/commands/traefik.rb | 6 +++++- test/commands/traefik_test.rb | 5 +++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 83b5fb7e..58db9525 100644 --- a/README.md +++ b/README.md @@ -354,6 +354,15 @@ traefik: This will start the traefik container with `--accesslog=true accesslog.format=json`. +### Traefik's host port binding + +By default Traefik binds to port 80 of the host machine, it can be configured to use an alternative port: + +```yaml +traefik: + host_port: 8080 +``` + ### Configuring build args for new images Build arguments that aren't secret can also be configured: diff --git a/lib/mrsk/commands/traefik.rb b/lib/mrsk/commands/traefik.rb index 67f7c09b..b8ae8c28 100644 --- a/lib/mrsk/commands/traefik.rb +++ b/lib/mrsk/commands/traefik.rb @@ -4,7 +4,7 @@ class Mrsk::Commands::Traefik < Mrsk::Commands::Base "--detach", "--restart", "unless-stopped", "--log-opt", "max-size=#{MAX_LOG_SIZE}", - "--publish", "80:80", + "--publish", port, "--volume", "/var/run/docker.sock:/var/run/docker.sock", "traefik", "--providers.docker", @@ -45,6 +45,10 @@ class Mrsk::Commands::Traefik < Mrsk::Commands::Base docker :image, :prune, "--all", "--force", "--filter", "label=org.opencontainers.image.title=Traefik" end + def port + "#{config.raw_config.traefik.fetch("host_port", "80")}:80" + end + private def cmd_args (config.raw_config.dig(:traefik, "args") || { }).collect { |(key, value)| [ "--#{key}", value ] }.flatten diff --git a/test/commands/traefik_test.rb b/test/commands/traefik_test.rb index 6d162138..ae0d471e 100644 --- a/test/commands/traefik_test.rb +++ b/test/commands/traefik_test.rb @@ -12,6 +12,11 @@ class CommandsTraefikTest < ActiveSupport::TestCase assert_equal \ "docker run --name traefik --detach --restart unless-stopped --log-opt max-size=10m --publish 80:80 --volume /var/run/docker.sock:/var/run/docker.sock traefik --providers.docker --log.level=DEBUG --accesslog.format json --metrics.prometheus.buckets 0.1,0.3,1.2,5.0", new_command.run.join(" ") + + @config[:traefik]["host_port"] = "8080" + assert_equal \ + "docker run --name traefik --detach --restart unless-stopped --log-opt max-size=10m --publish 8080:80 --volume /var/run/docker.sock:/var/run/docker.sock traefik --providers.docker --log.level=DEBUG --accesslog.format json --metrics.prometheus.buckets 0.1,0.3,1.2,5.0", + new_command.run.join(" ") end test "traefik start" do