Use kamal network for containers

Create a `kamal` network for the proxy and app containers to run in.
This allows the proxy to refer to the app containers by name.
This commit is contained in:
Donal McBreen
2024-03-20 09:26:00 +00:00
parent 9c4747ec0c
commit 00061ce7aa
16 changed files with 51 additions and 50 deletions

View File

@@ -41,8 +41,7 @@ class Kamal::Cli::App < Kamal::Cli::Base
if role.running_proxy?
version = capture_with_info(*app.current_running_version, raise_on_non_zero_exit: false).strip
ip = capture_with_info(*app.internal_ip(version: version), raise_on_non_zero_exit: false).strip
execute *KAMAL.proxy.deploy(ip)
execute *KAMAL.proxy.deploy(app.container_name(version))
end
end
end
@@ -60,12 +59,12 @@ class Kamal::Cli::App < Kamal::Cli::Base
version = capture_with_info(*app.current_running_version, raise_on_non_zero_exit: false).strip
execute *KAMAL.auditor.record("Stopped app", role: role), verbosity: :debug
execute *app.stop, raise_on_non_zero_exit: false
if role.running_proxy?
ip = capture_with_info(*app.internal_ip(version: version), raise_on_non_zero_exit: false).strip
execute *KAMAL.proxy.remove(ip), raise_on_non_zero_exit: false
execute *KAMAL.proxy.remove(app.container_name(version)), raise_on_non_zero_exit: false
end
execute *app.stop, raise_on_non_zero_exit: false
end
end
end

View File

@@ -48,8 +48,7 @@ class Kamal::Cli::App::Boot
audit "Booted app version #{version}"
execute *app.run(hostname: "#{host}-#{SecureRandom.hex(6)}")
if running_proxy?
ip = capture_with_info(*app.internal_ip(version: version), raise_on_non_zero_exit: false).strip
execute *KAMAL.proxy.deploy(ip)
execute *KAMAL.proxy.deploy(app.container_name(version))
end
end

View File

@@ -14,6 +14,14 @@ class Kamal::Cli::Server < Kamal::Cli::Base
end
execute(*KAMAL.server.ensure_run_directory)
begin
execute(*KAMAL.docker.create_kamal_network)
rescue SSHKit::Command::Failed => e
if e.message !~ /network with name kamal already exists/
raise
end
end
end
if missing.any?

View File

@@ -15,6 +15,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}\"",
@@ -43,10 +44,6 @@ class Kamal::Commands::App < Kamal::Commands::Base
xargs(config.stop_wait_time ? docker(:stop, "-t", config.stop_wait_time) : docker(:stop))
end
def internal_ip(version: nil)
docker :inspect, "--format '{{ .NetworkSettings.IPAddress }}'", container_name(version)
end
def info
docker :ps, *filter_args
end
@@ -60,6 +57,10 @@ class Kamal::Commands::App < Kamal::Commands::Base
container_id_for(container_name: container_name(version), only_running: only_running)
end
def container_name(version = nil)
[ role.container_prefix, version || config.version ].compact.join("-")
end
def current_running_version
list_versions("--latest", statuses: ACTIVE_DOCKER_STATUSES)
end
@@ -81,10 +82,6 @@ class Kamal::Commands::App < Kamal::Commands::Base
private
def container_name(version = nil)
[ role.container_prefix, version || config.version ].compact.join("-")
end
def filter_args(statuses: nil)
argumentize "--filter", filters(statuses: statuses)
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_kamal_network
docker :network, :create, :kamal
end
private
def get_docker
shell \

View File

@@ -10,6 +10,7 @@ class Kamal::Commands::Proxy < Kamal::Commands::Base
"--name", container_name,
"--detach",
"--restart", "unless-stopped",
"--network kamal",
*publish_args,
"--volume", "/var/run/docker.sock:/var/run/docker.sock",
*config.logging_args,