Skip to content
An`s Weblog
Go back

Building Images

1. Câu lệnh cơ bản:

docker image build -t <image-name>:<tag> .

2. Build với Dockerfile tuỳ chỉnh (f flag)

Khi có nhiều Dockerfile:

docker image build -t <image-name>:<tag> -f Dockerfile.test .

Docker sẽ dùng Dockerfile.test thay vì mặc định là Dockerfile.

3. Dùng flags bổ sung

FlagMục đích
--rmXoá container trung gian sau khi build xong
--force-rmXoá luôn container trung gian ngay khi build thất bại
--labelThêm metadata cho image
--ulimitGiới hạn tài nguyên container

Ví dụ:

docker image build -t myapp:v1 --label com.example.version=1.8 --rm .

4. Build từ Dockerfile qua stdin

Thay vì file vật lý, build từ nội dung nhập trực tiếp:

docker image build -t my-nginx --rm - <<EOF
FROM nginx:latest
VOLUME ["/data"]
EOF

5. Build image từ Git repo

docker image build -t <image-name>:<tag> https://github.com/user/repo.git

Tham số mở rộng:

https://github.com/user/repo.git#branch-name
https://github.com/user/repo.git#branch-name:subdir

6. Build từ file tar

Nén source + Dockerfile thành .tar.gz rồi build:

tar -zcvf myapp.tar.gz Dockerfile src/
docker image build -t myapp:from-tar - < myapp.tar.gz

Tổng kết lệnh

Phương phápMô tả
Build với Dockerfile mặc địnhdocker image build -t name .
Build với Dockerfile tuỳ chỉnh-f DockerfileName
Build từ stdindocker image build - < content
Build từ Git repodocker image build <repo-url>
Build từ tar filedocker image build - < file.tar.gz

Share this post:

Previous Post
Introduction to the Dockerfile
Next Post
Using dockerignore