Tích hợp Redis + Elasticsearch + Backend vào một Docker Image – Phần 4
Build Docker image all-in-one gồm Redis, Elasticsearch và Node.js backend có supervisor, tối ưu cho môi trường dev.
Phần 4: Gộp Redis + Elasticsearch + Backend vào một Docker Image – Multi-service tối thượng
Tóm tắt: Ở phần này, chúng ta sẽ kết hợp cả Redis, Elasticsearch và ứng dụng backend (Node.js) vào trong một Dockerfile duy nhất. Bài viết này không chỉ hướng dẫn build image, mà còn trình bày cách tối ưu RAM, quản lý tiến trình bằng supervisor
, và gợi ý cách gắn volume cho Redis/ES.
1. Mục tiêu cuối cùng
Tạo một Docker image chứa:
- ✅ Redis (cache/key-value store)
- ✅ Elasticsearch (search engine)
- ✅ Node.js backend (Express app)
- ✅ Supervisor để chạy song song 3 service
→ Chạy lên là có đủ mọi thứ: cache, search, backend.
2. Chuẩn bị cấu trúc thư mục
project/ ├── Dockerfile ├── index.js ├── package.json ├── supervisord.conf ├── elasticsearch.yml
Tổng hợp lại từ các phần trước:
index.js
: app Express sử dụng Redis và Elasticsearchsupervisord.conf
: quản lý 3 tiến trìnhelasticsearch.yml
: config ES đơn giản
3. File supervisord.conf
đa tiến trình
; supervisord.conf [supervisord] nodaemon=true [program:redis] command=redis-server autostart=true autorestart=true [program:elasticsearch] command=/elasticsearch/bin/elasticsearch directory=/elasticsearch autostart=true autorestart=true [program:app] command=node /app/index.js autostart=true autorestart=true
4. File elasticsearch.yml
cluster.name: docker-multiservice network.host: 0.0.0.0 http.port: 9200 discovery.type: single-node xpack.security.enabled: false
5. Dockerfile all-in-one
# Dockerfile FROM openjdk:17-slim as base # Cài gói cơ bản RUN apt-get update && apt-get install -y \ curl gnupg ca-certificates unzip supervisor redis-server \ && apt-get clean # Cài Node.js RUN curl -fsSL https://deb.nodesource.com/setup_18.x | bash - && \ apt-get install -y nodejs # Cài Elasticsearch ENV ES_VERSION=8.13.0 RUN curl -L -o elasticsearch.tar.gz https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-$ES_VERSION-linux-x86_64.tar.gz && \ tar -xzf elasticsearch.tar.gz && \ mv elasticsearch-$ES_VERSION /elasticsearch # Copy cấu hình Elasticsearch COPY elasticsearch.yml /elasticsearch/config/elasticsearch.yml # Copy app Node.js WORKDIR /app COPY package.json ./ RUN npm install COPY index.js . . # Copy file supervisor COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf # Mở cổng Redis, Elasticsearch, Backend EXPOSE 6379 9200 3000 # Lệnh chạy CMD ["/usr/bin/supervisord"]
6. Build & Run
docker build -t backend-with-es-redis .
docker run -p 3000:3000 -p 6379:6379 -p 9200:9200 backend-with-es-redis
7. Kiểm tra
- Elasticsearch:
curl http://localhost:9200
- Redis: kết nối từ Node.js bằng
ioredis
hoặcredis
- Backend:
http://localhost:3000
8. Đoạn code mẫu kết nối Redis + Elasticsearch
const Redis = require("ioredis"); const { Client } = require("@elastic/elasticsearch"); const express = require("express"); const app = express(); const redis = new Redis({ host: "localhost", port: 6379 }); const es = new Client({ node: "http://localhost:9200" }); app.get("/", async (req, res) => { const cache = await redis.get("welcome"); if (cache) return res.send("Redis: " + cache); await redis.set("welcome", "Xin chào từ Redis + Elasticsearch"); const esData = await es.search({ index: "_cat/indices", format: "json" }); res.json({ redis: "Set lần đầu", es: esData.body }); }); app.listen(3000, () => console.log("App chạy kèm Redis + ES"));
9. Một số lưu ý
- Đảm bảo RAM ít nhất 2–3GB cho Docker
- Không nên dùng image này cho production
- Volume:
- Redis:
/data
- ES:
/elasticsearch/data
- Redis:
- Log supervisor sẽ giúp bạn debug nếu service nào đó crash
10. Tổng kết
Bạn vừa hoàn thiện một image tích hợp cả Redis, Elasticsearch và Backend chỉ trong 1 Dockerfile. Đây là một nền tảng cực mạnh để tạo môi trường dev offline, demo khách hàng, CI/CD testing mà không cần setup nhiều container.
Tiếp theo (nếu bạn muốn): Tự động hóa build trên GitHub Actions & push image lên Docker Hub.
Blog: www.baotrongit.com
Tham gia cuộc trò chuyện