Skip to main content

[TIL] docker COPY không giống lệnh cp

Trong Dockerfile có lệnh (instruction) COPY dùng để copy file, thư mục. Nhưng cách hoạt động hơi khác lệnh cp trên UNIX.

cp file destdir

khi destdir là 1 thư mục đã tồn tại, cp sẽ copy file file tới destdir/file.

Còn COPY thì khác https://docs.docker.com/reference/dockerfile/#copy. Tất nhiên không ai đọc doc của syntax Dockerfile cả!

The COPY instruction copies new files or directories from and adds them to the filesystem of the image at the path You can specify multiple source files or directories with COPY. The last argument must always be the destination. If the destination path begins with a forward slash, it's interpreted as an absolute path, and the source files are copied into the specified destination relative to the root of the current build stage.

# create /abs/test.txt
COPY test.txt /abs/

Trailing slashes are significant. For example, COPY test.txt /abs creates a file at /abs, whereas COPY test.txt /abs/ creates /abs/test.txt.

If the destination path doesn't begin with a leading slash, it's interpreted as relative to the working directory of the build container.

WORKDIR /usr/src/app
# create /usr/src/app/rel/test.txt
COPY test.txt rel/

If destination doesn't exist, it's created, along with all missing directories in its path.

If the source is a file, and the destination doesn't end with a trailing slash, the source file will be written to the destination path as a file.

Khi build, COPY file destdir sẽ copy file, tạo 1 file mới tên là destdir.

Để có kết quả giống lệnh cp, cần thêm dấu / vào cuối destdir

COPY file destdir/

Trailing slashes are significant. For example, COPY test.txt /abs creates a file at /abs, whereas COPY test.txt /abs/ creates /abs/test.txt.

Kết luận

Hơi hơi giống không phải là giống, lệnh COPY có sự khác biệt khi thêm dấu / vào destination.

Hết.

HVN at https://pymi.vn and https://www.familug.org.

Ủng hộ tác giả 🍺

Comments

Popular posts from this blog

Tài liệu và hướng dẫn học Python

Để tiết kiệm thời gian, tốt nhất là đi học PyMI Updated: 130617 Sau đây là các tài liệu khuyên dùng: Vì nhiều lý do, nên học python2.7 tại thời điểm hiện tại (giờ là tháng 6/2013 - muốn biết tại sao thì tự tìm hiểu) python 3.5+ (giờ là tháng 2/2017) Chuẩn bị: 1. biết bật tắt máy 2. biết cài python 3. tập gõ 10 ngón - gõ 2 ngón hay 1 ngón cũng không sao, nhưng 10 ngón là cách dễ nhất để gõ nhanh nhất. Tài liệu - Nên dùng tài liệu tại trang chủ của Python làm chính, tham khảo thêm các tài liệu khác tại http://www.familug.org/2016/12/free-ebook.html Căn bản, mới học 1.1 Python PyMI.vn https://pymi.vn/tutorial/ 1.2. Python offical tutorial kết hợp làm bài tập trên HackerRank  (đề bài bằng tiếng Anh, nhưng Google translate 1 lúc cũng ra vì có nhiều ví dụ mẫu đi kèm). Học viên của Pymi.vn có rất nhiều học viên đã tự học với Learn Python the hard way nhưng chưa thấy ai thành công cả. Hai link dưới nên đọc sau khi đã nắm được những phần cơ bản của ngôn ngữ pytho...

The PyMiers

New FAMILUG