Skip to main content

Docker Operations

Container management procedures for crocbot deployments, including image updates, rollback, and troubleshooting.

Quick Reference

OperationCommand
Startdocker start crocbot
Stopdocker stop crocbot
Restartdocker restart crocbot
Logsdocker logs -f crocbot
Shelldocker exec -it crocbot sh
Statusdocker ps -f name=crocbot

Container Management

Viewing Container Status

# Check if container is running
docker ps -f name=crocbot

# Include stopped containers
docker ps -a -f name=crocbot

# Detailed status
docker inspect crocbot | jq '.[0].State'

# Expected output for healthy container:
# {
#   "Status": "running",
#   "Running": true,
#   "Paused": false,
#   ...
# }

Resource Usage

# Real-time stats
docker stats crocbot --no-stream

# Memory and CPU details
docker stats crocbot --format "{{.Name}}: {{.MemUsage}} / {{.CPUPerc}}"

Container Inspection

# View all configuration
docker inspect crocbot

# Specific fields
docker inspect crocbot | jq '.[0].Config.Env'       # Environment
docker inspect crocbot | jq '.[0].Mounts'           # Volumes
docker inspect crocbot | jq '.[0].NetworkSettings'  # Network
docker inspect crocbot | jq '.[0].RestartCount'     # Restart count

Image Updates

Pull Latest Image

# Pull from registry
docker pull ghcr.io/your-org/crocbot:latest

# Verify new image
docker images | grep crocbot

Update Running Container

# 1. Pull new image
docker pull ghcr.io/your-org/crocbot:latest

# 2. Stop current container
docker stop crocbot

# 3. Remove old container
docker rm crocbot

# 4. Start with new image (preserves volumes)
docker run -d \
  --name crocbot \
  --restart unless-stopped \
  -p 18789:18789 \
  -v crocbot-state:/app/state \
  --env-file /path/to/.env \
  ghcr.io/your-org/crocbot:latest

# 5. Verify health
curl http://localhost:18789/health

Update with Docker Compose

# Pull and recreate
docker-compose pull
docker-compose up -d

# Verify
docker-compose ps

Tag-Based Updates

# List available tags
docker images ghcr.io/your-org/crocbot --format "{{.Tag}}"

# Update to specific version
docker pull ghcr.io/your-org/crocbot:2026.1.55
docker stop crocbot && docker rm crocbot
docker run -d --name crocbot ... ghcr.io/your-org/crocbot:2026.1.55

Rollback Procedures

Identify Previous Version

# List local images with timestamps
docker images ghcr.io/your-org/crocbot --format "{{.Tag}}\t{{.CreatedAt}}"

# Check what version is currently running
docker inspect crocbot | jq -r '.[0].Config.Image'

Rollback to Previous Version

# 1. Stop current container
docker stop crocbot

# 2. Remove container (preserves volumes)
docker rm crocbot

# 3. Run previous version
docker run -d \
  --name crocbot \
  --restart unless-stopped \
  -p 18789:18789 \
  -v crocbot-state:/app/state \
  --env-file /path/to/.env \
  ghcr.io/your-org/crocbot:2026.1.54

# 4. Verify rollback successful
curl http://localhost:18789/health
docker logs --tail 20 crocbot

Emergency Rollback Script

#!/bin/bash
# rollback.sh <previous-tag>

TAG=${1:-"previous"}
IMAGE="ghcr.io/your-org/crocbot:$TAG"

echo "Rolling back to $IMAGE..."

docker stop crocbot
docker rm crocbot
docker run -d \
  --name crocbot \
  --restart unless-stopped \
  -p 18789:18789 \
  -v crocbot-state:/app/state \
  --env-file /path/to/.env \
  "$IMAGE"

sleep 5
if curl -sf http://localhost:18789/health > /dev/null; then
  echo "Rollback successful"
else
  echo "Rollback failed - check logs"
  docker logs --tail 50 crocbot
  exit 1
fi

Volume Management

List Volumes

# All crocbot volumes
docker volume ls -f name=crocbot

# Volume details
docker volume inspect crocbot-state

Backup Volume

# Backup state directory
docker run --rm \
  -v crocbot-state:/data:ro \
  -v $(pwd):/backup \
  alpine tar czf /backup/crocbot-state-backup.tar.gz -C /data .

Restore Volume

# Stop container first
docker stop crocbot

# Restore from backup
docker run --rm \
  -v crocbot-state:/data \
  -v $(pwd):/backup \
  alpine tar xzf /backup/crocbot-state-backup.tar.gz -C /data

# Start container
docker start crocbot

Log Management

View Logs

# Follow logs
docker logs -f crocbot

# Last N lines
docker logs --tail 100 crocbot

# With timestamps
docker logs -f -t crocbot

# Since specific time
docker logs --since "1h" crocbot
docker logs --since "2026-02-04T10:00:00" crocbot

Export Logs

# Export to file
docker logs crocbot > crocbot-logs.txt 2>&1

# Export last 1000 lines
docker logs --tail 1000 crocbot > recent-logs.txt 2>&1

Log Rotation (Docker daemon)

Configure in /etc/docker/daemon.json:
{
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  }
}
Then restart Docker daemon:
sudo systemctl restart docker

Troubleshooting

Container Won’t Start

# Check container exit code
docker inspect crocbot | jq '.[0].State.ExitCode'

# View logs from failed start
docker logs crocbot

# Common exit codes:
# 0 - Normal exit
# 1 - Application error
# 137 - OOM killed (SIGKILL)
# 139 - Segfault

Container Restarts Repeatedly

# Check restart count
docker inspect crocbot | jq '.[0].RestartCount'

# Disable restart temporarily
docker update --restart no crocbot

# Debug manually
docker logs --tail 100 crocbot

Out of Memory (OOM)

# Check if OOM killed
docker inspect crocbot | jq '.[0].State.OOMKilled'

# View system logs for OOM events
dmesg | grep -i "killed process"

# Increase memory limit
docker update --memory 1g crocbot
# or recreate with:
docker run ... --memory 1g ...

Network Issues

# Check network configuration
docker network ls
docker inspect crocbot | jq '.[0].NetworkSettings.Networks'

# Test internal DNS
docker exec crocbot nslookup api.telegram.org

# Check port bindings
docker port crocbot

Disk Space

# Check Docker disk usage
docker system df

# Clean up unused resources
docker system prune -f

# Remove old images
docker image prune -a -f --filter "until=168h"  # older than 7 days

Health Checks

Docker Health Check

Configure in Dockerfile or compose:
healthcheck:
  test: ["CMD", "curl", "-f", "http://localhost:18789/health"]
  interval: 30s
  timeout: 10s
  retries: 3
  start_period: 30s

Check Health Status

# View health status
docker inspect crocbot | jq '.[0].State.Health'

# Health history
docker inspect crocbot | jq '.[0].State.Health.Log'

Docker Compose Operations

Basic Commands

# Start services
docker-compose up -d

# Stop services
docker-compose down

# Restart specific service
docker-compose restart crocbot

# View logs
docker-compose logs -f crocbot

# Pull and update
docker-compose pull && docker-compose up -d

Example docker-compose.yml

version: "3.8"
services:
  crocbot:
    image: ghcr.io/your-org/crocbot:latest
    container_name: crocbot
    restart: unless-stopped
    ports:
      - "18789:18789"
    volumes:
      - crocbot-state:/app/state
    env_file:
      - .env
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:18789/health"]
      interval: 30s
      timeout: 10s
      retries: 3

volumes:
  crocbot-state: