Skip to content

Commit 9c6f68c

Browse files
committed
Stop app container by container id
1 parent bebf5a3 commit 9c6f68c

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

lib/kamal/cli/app.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,16 @@ def start
6666
end
6767

6868
desc "stop", "Stop app container on servers"
69+
option :container_id, desc: "Docker container ID to stop (instead of stopping all containers)"
6970
def stop
71+
container_id = options[:container_id]
72+
7073
with_lock do
7174
on(KAMAL.app_hosts) do |host|
7275
roles = KAMAL.roles_on(host)
7376

7477
roles.each do |role|
75-
app = KAMAL.app(role: role, host: host)
78+
app = KAMAL.app(role: role, host: host, container_id: container_id)
7679
execute *KAMAL.auditor.record("Stopped app", role: role), verbosity: :debug
7780

7881
if role.running_proxy?

lib/kamal/commander.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ def accessory_names
8181
config.accessories&.collect(&:name) || []
8282
end
8383

84-
def app(role: nil, host: nil)
85-
Kamal::Commands::App.new(config, role: role, host: host)
84+
def app(role: nil, host: nil, container_id: nil)
85+
Kamal::Commands::App.new(config, role: role, host: host, container_id: container_id)
8686
end
8787

8888
def accessory(name)

lib/kamal/commands/app.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ class Kamal::Commands::App < Kamal::Commands::Base
33

44
ACTIVE_DOCKER_STATUSES = [ :running, :restarting ]
55

6-
attr_reader :role, :host
6+
attr_reader :role, :host, :container_id
77

88
delegate :container_name, to: :role
99

10-
def initialize(config, role: nil, host: nil)
10+
def initialize(config, role: nil, host: nil, container_id: nil)
1111
super(config)
1212
@role = role
1313
@host = host
14+
@container_id = container_id
1415
end
1516

1617
def run(hostname: nil)
@@ -54,7 +55,7 @@ def info
5455

5556

5657
def current_running_container_id
57-
current_running_container(format: "--quiet")
58+
container_id || current_running_container(format: "--quiet")
5859
end
5960

6061
def container_id_for_version(version, only_running: false)

test/cli/app_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,12 @@ class CliAppTest < CliTestCase
250250
end
251251
end
252252

253+
test "stop with container id" do
254+
run_command("stop", "--container-id", "abcd1234").tap do |output|
255+
assert_match "abcd1234 | xargs docker stop", output
256+
end
257+
end
258+
253259
test "stale_containers" do
254260
SSHKit::Backend::Abstract.any_instance.expects(:capture_with_info)
255261
.with(:docker, :ps, "--filter", "label=service=app", "--filter", "label=destination=", "--filter", "label=role=web", "--format", "\"{{.Names}}\"", "|", "while read line; do echo ${line#app-web-}; done", raise_on_non_zero_exit: false)

0 commit comments

Comments
 (0)