From 1fa85dac550c584f4af9b9e851633724c3a9c310 Mon Sep 17 00:00:00 2001 From: James Paterni Date: Tue, 15 Jul 2025 08:18:46 -0400 Subject: [PATCH] Fix container running state detection for different Docker API versions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Handle both string and hash formats for container.info['State'] - Add fallback to container.json['State'] when needed - Fix "Container not running" error when containers are actually running - Support multiple Docker API response formats The Docker API gem can return State as either: - A simple string: "running" - A hash: {"Status": "running", "Running": true, ...} This fix ensures compatibility with different Docker daemon versions and API response formats. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- app/lib/baktainer/container.rb | 25 ++++++++++++++++++++++-- app/lib/baktainer/container_validator.rb | 18 +++++++++++++++-- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/app/lib/baktainer/container.rb b/app/lib/baktainer/container.rb index c7bd1d6..faa7439 100644 --- a/app/lib/baktainer/container.rb +++ b/app/lib/baktainer/container.rb @@ -39,11 +39,32 @@ class Baktainer::Container end def state - @container.info['State']&.[]('Status') + # Handle different Docker API response formats + state_data = @container.info['State'] + + if state_data.is_a?(String) + state_data + elsif state_data.is_a?(Hash) + state_data['Status'] + else + # Fallback to json method if info doesn't provide expected format + @container.json['State']['Status'] rescue 'unknown' + end end def running? - state == 'running' + # Check multiple ways to determine if container is running + state_data = @container.info['State'] + + if state_data.is_a?(String) + state_data == 'running' + elsif state_data.is_a?(Hash) + state_data['Status'] == 'running' || state_data['Running'] == true + else + # Fallback to json method + json_state = @container.json['State'] rescue nil + json_state && (json_state['Status'] == 'running' || json_state['Running'] == true) + end end def engine diff --git a/app/lib/baktainer/container_validator.rb b/app/lib/baktainer/container_validator.rb index a334cb3..b71e0b0 100644 --- a/app/lib/baktainer/container_validator.rb +++ b/app/lib/baktainer/container_validator.rb @@ -99,8 +99,22 @@ class Baktainer::ContainerValidator end def validate_container_running - state = @container.info['State']&.[]('Status') - if state.nil? || state != 'running' + # Check both possible state formats - Docker API can return different structures + state = @container.info['State'] + + # If State is a string, use it directly + if state.is_a?(String) + running = state == 'running' + # If State is a hash, check the Status field + elsif state.is_a?(Hash) + running = state['Status'] == 'running' || state['Running'] == true + # Otherwise check the json method which returns full state + else + json_state = @container.json['State'] rescue nil + running = json_state && (json_state['Status'] == 'running' || json_state['Running'] == true) + end + + unless running raise Baktainer::ValidationError, 'Container not running' end end