Fix container running state detection for different Docker API versions
Some checks are pending
Test and Build Docker Image / test (push) Waiting to run
Test and Build Docker Image / build (push) Blocked by required conditions

- 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:
James Paterni 2025-07-15 08:18:46 -04:00
parent f775f7586c
commit 1fa85dac55
2 changed files with 39 additions and 4 deletions

View file

@ -39,11 +39,32 @@ class Baktainer::Container
end end
def state 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 end
def running? 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 end
def engine def engine

View file

@ -99,8 +99,22 @@ class Baktainer::ContainerValidator
end end
def validate_container_running def validate_container_running
state = @container.info['State']&.[]('Status') # Check both possible state formats - Docker API can return different structures
if state.nil? || state != 'running' 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' raise Baktainer::ValidationError, 'Container not running'
end end
end end