Create proxy and app containers in a kamal network

This commit is contained in:
Donal McBreen
2024-09-05 14:31:37 +01:00
parent 13bdf50ceb
commit 63ebeda489
8 changed files with 24 additions and 14 deletions

View File

@@ -3,6 +3,12 @@ class Kamal::Cli::Proxy < Kamal::Cli::Base
def boot
raise_unless_kamal_proxy_enabled!
with_lock do
on(KAMAL.hosts) do |host|
execute *KAMAL.docker.create_network
rescue SSHKit::Command::Failed => e
raise unless e.message.include?("already exists")
end
on(KAMAL.traefik_hosts) do |host|
execute *KAMAL.registry.login
execute *KAMAL.traefik_or_proxy(host).start_or_run

View File

@@ -5,6 +5,8 @@ class Kamal::Commands::App < Kamal::Commands::Base
attr_reader :role, :host
delegate :container_name, to: :role
def initialize(config, role: nil, host: nil)
super(config)
@role = role
@@ -35,6 +37,7 @@ class Kamal::Commands::App < Kamal::Commands::Base
"--detach",
"--restart unless-stopped",
"--name", container_name,
"--network", "kamal",
*([ "--hostname", hostname ] if hostname),
"-e", "KAMAL_CONTAINER_NAME=\"#{container_name}\"",
"-e", "KAMAL_VERSION=\"#{config.version}\"",
@@ -92,10 +95,6 @@ class Kamal::Commands::App < Kamal::Commands::Base
end
private
def container_name(version = nil)
[ role.container_prefix, version || config.version ].compact.join("-")
end
def latest_image_id
docker :image, :ls, *argumentize("--filter", "reference=#{config.latest_image}"), "--format", "'{{.ID}}'"
end

View File

@@ -32,7 +32,7 @@ module Kamal::Commands::App::Containers
def container_endpoint(version:)
pipe \
container_id_for(container_name: container_name(version)),
xargs(docker(:inspect, "--format", "'{{.NetworkSettings.IPAddress}}{{range $k, $v := .NetworkSettings.Ports}}{{printf \":%s\" $k}}{{break}}{{end}}'")),
xargs(docker(:inspect, "--format", "'{{index .NetworkSettings.Networks.kamal.Aliases 0}}{{range $k, $v := .NetworkSettings.Ports}}{{printf \":%s\" $k}}{{break}}{{end}}'")),
[ :sed, "-e", "'s/\\/tcp$//'" ]
end
end

View File

@@ -19,6 +19,10 @@ class Kamal::Commands::Docker < Kamal::Commands::Base
[ '[ "${EUID:-$(id -u)}" -eq 0 ] || command -v sudo >/dev/null || command -v su >/dev/null' ]
end
def create_network
docker :network, :create, :kamal
end
private
def get_docker
shell \

View File

@@ -12,6 +12,7 @@ class Kamal::Commands::Proxy < Kamal::Commands::Base
def run
docker :run,
"--name", container_name,
"--network", "kamal",
"--detach",
"--restart", "unless-stopped",
*proxy_config.publish_args,