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