Skip to content
An`s Weblog
Go back

Updating Containers with Watchtower

1. Giới thiệu về Watchtower


2. Quy trình triển khai Watchtower chi tiết

2.1. Chuẩn bị dự án thử nghiệm

git clone <repo-url> Watchtower
cd Watchtower
git checkout dockerfile

2.2. Kiểm tra Dockerfile dự án

Lưu ý: Cần đảm bảo Dockerfile chỉ copy các file nguồn cần thiết, tránh đưa file .git hoặc Dockerfile vào container image.

2.3. Đăng nhập Docker Hub

docker login -u <username>

2.4. Build Docker Image

docker image build -t <username>/my-express .

2.5. Push Image lên Docker Hub

docker image push <username>/my-express

2.6. Khởi chạy Container ứng dụng

docker container run -d \
  --name watch-app \
  -p 80:3000 \
  --restart always \
  <username>/my-express

2.7. Triển khai Watchtower Container

docker container run -d \
  --name Watchtower \
  --restart always \
  -v /var/run/docker.sock:/var/run/docker.sock \
  v2tec/watchtower \
  -i 15

Ghi chú: Tham số -i 15 thiết lập Watchtower quét Docker Hub định kỳ mỗi 15 giây.


3. Kiểm thử tính năng Tự động Cập nhật của Watchtower

3.1. Cải thiện Dockerfile với .dockerignore

Tạo file .dockerignore để loại bỏ các tệp không cần thiết:

Dockerfile
.git
.gitignore

Tạo thêm file mới và Rebuild Image (bỏ cache):

touch newfile.js

docker image build --no-cache -t <username>/my-express .
docker image push <username>/my-express

3.2. Kiểm tra kết quả tự động cập nhật

  1. Chờ khoảng 15-30 giây để Watchtower thực hiện chu kỳ quét.
  2. Kiểm tra trạng thái container:
docker container ls
  1. Truy cập vào container để xác nhận file mới đã được đồng bộ:
docker container exec -it watch-app /bin/bash
ls -la /var/node

Kết quả: Thấy file newfile.js xuất hiện bên trong container, đồng thời các file thuộc .dockerignore đã được loại bỏ thành công.


4. Kết luận


Share this post:

Previous Post
Using Multi-Stage Builds
Next Post
Managing Docker with Portainer