Skip to content
An`s Weblog
Go back

Tagging

Mục tiêu bài học:

Vấn đề với cách tag thông thường

Ví dụ thường thấy khi build image:

v1, v2, v3...

Nhược điểm: Không biết phiên bản source code nào đã build ra image đó.

Giải pháp tốt hơn: Dùng commit hash

Bước 1: Di chuyển vào thư mục source code:

cd ~/docker_images/weather-app/src

Bước 2: Lấy commit hash mới nhất:

git log -1 --pretty=%h

Lệnh trả về mã commit rút gọn, ví dụ: 7d9a7f3.

Bước 3: Quay về thư mục gốc project:

cd ~/docker_images/weather-app

Bước 4: Build image và gắn tag là commit hash:

docker image build -t linuxacademy/weather-app:<commit-hash> .

Ví dụ:

docker image build -t linuxacademy/weather-app:7d9a7f3 .

Build nhanh vì đã cache các layer trước đó.


Dùng docker image tag để tạo tag mới từ image cũ

Cú pháp:

docker image tag <source-image>:<tag> <target-image>:<tag>

Ví dụ: Tạo thêm tag latest:

docker image tag linuxacademy/weather-app:7d9a7f3 linuxacademy/weather-app:latest

Kiểm tra:

docker image ls

Kết quả:

REPOSITORY                   TAG         IMAGE ID       ...
linuxacademy/weather-app     latest      abc123
linuxacademy/weather-app     7d9a7f3      abc123

Tổng kết

LệnhÝ nghĩa
git log -1 --pretty=%hLấy commit hash ngắn
docker image build -tBuild image và tag
docker image tagTạo tag mới từ image đã có
docker image lsLiệt kê danh sách images

Share this post:

Previous Post
Distributing Images on Docker Hub
Next Post
Using Multi-Stage Builds