Fix container running state detection for different Docker API versions
- 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 <noreply@anthropic.com>
This commit is contained in:
parent
f775f7586c
commit
1fa85dac55
2 changed files with 39 additions and 4 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue