A simple demonstration of message queuing with RabbitMQ, Docker, and Python. This project shows how to send and receive messages using a producer-consumer pattern with containerized services.
This project implements a Work Queue (Task Queue) pattern using RabbitMQ - a messaging system that distributes time-consuming tasks among multiple workers. This is ideal for background job processing where you want to avoid doing resource-intensive tasks immediately and waiting for them to complete.
This implementation includes production-ready reliability features:
- ✅ Durable Queues: Queue survives RabbitMQ broker restarts
- ✅ Persistent Messages: Messages survive broker restarts
- ✅ Manual Acknowledgments: Messages aren't lost if a worker fails during processing
- ✅ Fair Dispatch: Workers receive only one message at a time (prevents overload)
- Producer: Sends task messages to a RabbitMQ queue
- Consumer: Receives and processes tasks from the queue
- RabbitMQ: Message broker that manages the queue and ensures delivery
- Docker: Everything runs in containers for easy setup
┌─────────────────────────────────────────────────────────────────────────────┐
│ 🐳 DOCKER ENVIRONMENT │
│ │
│ ┌───────────────────┐ ┌───────────────────┐ │
│ │ PRODUCER │ │ CONSUMER │ │
│ │ (Container) │ │ (Container) │ │
│ ├───────────────────┤ ├───────────────────┤ │
│ │ script: │ │ script: │ │
│ │ producer.py │ │ consumer.py │ │
│ │ │ │ │ │
│ │ action: │ │ action: │ │
│ │ • Sends msg │ │ • Receives msg │ │
│ │ • Exits │ │ • Processes │ │
│ └─────────┬─────────┘ └─────────▲─────────┘ │
│ │ │ │
│ │ (AMQP) │ (AMQP) │
│ │ "Hello RabbitMQ!" │ │
│ │ │ │
│ ┌─────────▼─────────────────────────────────────┴─────────┐ │
│ │ RABBITMQ CONTAINER │ │
│ │ Image: rabbitmq:3-management │ │
│ │ │ │
│ │ ┌─────────────────────────────────────────────────┐ │ │
│ │ │ ≡ QUEUE: task_queue │ │ │
│ │ │ (durable, persistent) │ │ │
│ │ └─────────────────────────────────────────────────┘ │ │
│ │ │ │
│ │ INTERNAL PORTS: │ │
│ │ • 5672 (AMQP Messaging) │ │
│ │ • 15672 (Management UI) │ │
│ └──────────────────────────┬──────────────────────────────┘ │
│ │ │
│ ══════════════════════════════╪══════════════════════════════════ │
│ NETWORK BRIDGE: │ rabbitmq-net │
│ ══════════════════════════════╪══════════════════════════════════ │
│ │ │
└────────────────────────────────┼────────────────────────────────────────────┘
│
▼
┌───────────────────────┐
│ HOST MACHINE │
└───────────┬───────────┘
│
┌──────────────┴──────────────┐
│ Exposed Ports (Mapped) │
├─────────────────────────────┤
│ 1. App Access (AMQP) │
│ localhost:5672 │
│ │
│ 2. Management Dashboard │
│ http://localhost:15672 │
└─────────────────────────────┘
Login: guest / guest
Start all services (RabbitMQ + Producer + Consumer):
docker compose -f docker-compose.full.yml up -dCheck the logs to see messages being sent and received:
docker compose -f docker-compose.full.yml logs -fStop everything:
docker compose -f docker-compose.full.yml downStart just RabbitMQ:
docker compose up -dAdd the producer:
docker compose --profile producer up -dAdd the consumer:
docker compose --profile consumer up -dCheck logs:
docker compose logs -fStop everything:
docker compose downTerminal 1: Monitor consumer logs in real-time
docker compose -f docker-compose.full.yml logs -f consumerYou'll see:
[*] Waiting for messages. To exit press CTRL+C
[x] Received: Your message here
[x] Done
Terminal 2: Send messages to the queue
# Send a single message
docker compose -f docker-compose.full.yml run --rm producer python producer.py "Hello World"
# Send message with dots (simulates longer processing - 1 second per dot)
docker compose -f docker-compose.full.yml run --rm producer python producer.py "Task with work..."
# Send multiple messages
docker compose -f docker-compose.full.yml run --rm producer python producer.py "Message 1"
docker compose -f docker-compose.full.yml run --rm producer python producer.py "Message 2"
docker compose -f docker-compose.full.yml run --rm producer python producer.py "Message 3"Run the consumer in the foreground to see output immediately:
# Stop background consumer
docker compose -f docker-compose.full.yml stop consumer
# Run consumer in foreground
docker compose -f docker-compose.full.yml run --rm consumer python consumer.pyThen send messages from another terminal and watch them process in real-time.
Access the RabbitMQ Management Interface:
- URL: http://localhost:15672
- Username:
guest - Password:
guest
View queues, messages, and connections in real-time.
rabbitmq-tutorial/
├── app/
│ ├── producer.py # Sends messages to queue
│ ├── consumer.py # Receives messages from queue
│ └── test.py # Test without RabbitMQ
├── config/
│ └── requirements.txt # Python dependencies (pika)
├── docker/
│ ├── Dockerfile.producer # Producer container image
│ ├── Dockerfile.consumer # Consumer container image
│ └── .dockerignore
├── docker-compose.yml # Setup with profiles (selective start)
├── docker-compose.full.yml # Setup without profiles (start all)
└── README.md
# Start everything
docker compose -f docker-compose.full.yml up -d
# View logs (all services)
docker compose -f docker-compose.full.yml logs -f
# View logs (specific service)
docker compose -f docker-compose.full.yml logs -f producer
docker compose -f docker-compose.full.yml logs -f consumer
# Check status
docker compose -f docker-compose.full.yml ps
# Stop everything
docker compose -f docker-compose.full.yml down# Start only RabbitMQ
docker compose up -d
# Start RabbitMQ + Producer
docker compose --profile producer up -d
# Start RabbitMQ + Consumer
docker compose --profile consumer up -d
# Start all services
docker compose --profile producer --profile consumer up -d
# View logs
docker compose logs -f
# Stop everything
docker compose down- Docker Desktop installed and running
- Python 3.8+ (only if running locally, not in Docker)
Docker not running:
# Start Docker Desktop first, then verify:
docker --versionSee what's running:
docker compose psView RabbitMQ container logs:
docker logs rabbitmqCheck queue status:
- Visit http://localhost:15672
- Go to "Queues" tab
- Look for
task_queue
- Connects to RabbitMQ
- Creates a durable queue called
task_queue - Sends persistent messages
- Default message: "Hello RabbitMQ!"
- Exits after sending
- Connects to RabbitMQ
- Listens to
task_queue - Processes messages (simulates work by counting dots)
- Sends acknowledgment after processing
- Runs continuously waiting for messages
1. Producer → RabbitMQ queue
2. RabbitMQ → Consumer
3. Consumer processes → Acknowledges
4. Message removed from queue
# Install dependencies
pip install -r config/requirements.txt
# Terminal 1 - Start consumer
python3 app/consumer.py
# Terminal 2 - Send messages
python3 app/producer.py "First message"
python3 app/producer.py "Second message..."
python3 app/producer.py "Third message....."Messages with more dots take longer to process (1 second per dot).
python3 app/test.pyThis simulates the message flow without requiring RabbitMQ.
- Message queue concepts (producer/consumer pattern)
- Docker containerization and networking
- RabbitMQ basics (queues, durability, acknowledgments)
- Python messaging with pika library
MIT License - feel free to use for learning and projects.