Customizable Traefik host port

Allow users to free up port 80 on the host machine, without losing
Traefik's Docker routing super-powers.
This commit is contained in:
Chris Lowder
2023-03-04 21:41:08 +00:00
parent 8c69990dbb
commit 963b96ff62
3 changed files with 19 additions and 1 deletions

View File

@@ -354,6 +354,15 @@ traefik:
This will start the traefik container with `--accesslog=true accesslog.format=json`. 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 ### Configuring build args for new images
Build arguments that aren't secret can also be configured: Build arguments that aren't secret can also be configured:

View File

@@ -4,7 +4,7 @@ class Mrsk::Commands::Traefik < Mrsk::Commands::Base
"--detach", "--detach",
"--restart", "unless-stopped", "--restart", "unless-stopped",
"--log-opt", "max-size=#{MAX_LOG_SIZE}", "--log-opt", "max-size=#{MAX_LOG_SIZE}",
"--publish", "80:80", "--publish", port,
"--volume", "/var/run/docker.sock:/var/run/docker.sock", "--volume", "/var/run/docker.sock:/var/run/docker.sock",
"traefik", "traefik",
"--providers.docker", "--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" docker :image, :prune, "--all", "--force", "--filter", "label=org.opencontainers.image.title=Traefik"
end end
def port
"#{config.raw_config.traefik.fetch("host_port", "80")}:80"
end
private private
def cmd_args def cmd_args
(config.raw_config.dig(:traefik, "args") || { }).collect { |(key, value)| [ "--#{key}", value ] }.flatten (config.raw_config.dig(:traefik, "args") || { }).collect { |(key, value)| [ "--#{key}", value ] }.flatten

View File

@@ -12,6 +12,11 @@ class CommandsTraefikTest < ActiveSupport::TestCase
assert_equal \ 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", "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(" ") 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 end
test "traefik start" do test "traefik start" do