Skip to content

Commit f3a58e4

Browse files
committed
wippppp
1 parent 44901d3 commit f3a58e4

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-0
lines changed

docker/build/.secrets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hereisthesensitivedata

docker/build/Dockerfile.unsafe

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FROM busybox
2+
COPY .secrets /
3+
RUN rm .secrets

docker/build/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Build tricks
2+
3+
## 1. Build secrets
4+
5+
Source: <https://docs.docker.com/build/building/secrets/>
6+
7+
A build secret is any piece of sensitive information, such as password or API token, consumed as part of your applications build process.
8+
9+
## 1.1. The wrong way to copy secret
10+
11+
Do you ever try to `COPY` a file with credentials from your Dockerfile and then remove it with `rm` when you don't need it anymore?
12+
13+
```Dockerfile
14+
FROM busybox
15+
COPY .secrets /tmp
16+
RUN rm /tmp/.secrets
17+
```
18+
19+
Well, **this is so wrong** because you are just deleting the file from that layer but the credentials are still in the layer above.
20+
21+
```shell
22+
$ docker build -t unsafe . -f Dockerfile.unsafe
23+
=> [internal] load build definition from Dockerfile.unsafe 0.1s
24+
=> => transferring dockerfile: 97B 0.0s
25+
=> [internal] load metadata for docker.io/library/busybox:latest 2.7s
26+
=> [auth] library/busybox:pull token for registry-1.docker.io 0.0s
27+
=> [internal] load .dockerignore 0.0s
28+
=> => transferring context: 2B 0.0s
29+
=> [internal] load build context 0.0s
30+
=> => transferring context: 58B 0.0s
31+
=> [1/3] FROM docker.io/library/busybox:latest@sha256:db142d433cdde11f10ae479dbf92f3b13d693fd1c91053da9979728cceb1dc68 0.6s
32+
=> => resolve docker.io/library/busybox:latest@sha256:db142d433cdde11f10ae479dbf92f3b13d693fd1c91053da9979728cceb1dc68 0.0s
33+
=> => sha256:db142d433cdde11f10ae479dbf92f3b13d693fd1c91053da9979728cceb1dc68 10.20kB / 10.20kB 0.0s
34+
=> => sha256:a3e1b257b47c09c9997212e53a0b570c1666501ad26e5bf33461304babab47c7 610B / 610B 0.0s
35+
=> => sha256:517b897a6a8312ce202a85c8a517d820b0fc5b6f5d14ec2a3267906f75680403 372B / 372B 0.0s
36+
=> => sha256:430378704d12f9a980f41ae1a29c587974e1f0234d5dab0765fa95a4d764622e 2.16MB / 2.16MB 0.4s
37+
=> => extracting sha256:430378704d12f9a980f41ae1a29c587974e1f0234d5dab0765fa95a4d764622e 0.1s
38+
=> [2/3] COPY .secrets /tmp 0.1s
39+
=> [3/3] RUN rm /tmp/.secrets 0.5s
40+
=> exporting to image 0.1s
41+
=> => exporting layers 0.0s
42+
=> => writing image sha256:c5ca80eed7e66549a42da612b81a0e914223e3eef40360f87f0dd84d0090e333 0.0s
43+
=> => naming to docker.io/library/unsafe 0.0s
44+
45+
$ docker history unsafe
46+
47+
$ docker run -it --rm
48+
```

docker/overlayfs/README.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Docker and OverlayFS
2+
3+
Source:
4+
5+
- <https://www.kernel.org/doc/Documentation/filesystems/overlayfs.txt>
6+
7+
Trong bài post này, chúng ta sẽ nghiên cứu về OverlayFS và cách Docker ứng dụng OverlayFS để làm việc với các layers. Trước tiên, hãy cùng tìm hiểu về OverlayFS
8+
9+
## 1. OverlayFS
10+
11+
OverlayFS, một dạng "union filesystem" cho phép nhiều filesystem có thể nằm chồng lên nhau (overlaid). Thay đổi được ghi nhận ở `upper` filesystem, trong khi `lower` filesystem giữ nguyên, giúp giảm dung lượng lưu trữ.
12+
13+
Như vậy, hiểu đơn giản, OverlayFS bao gồm:
14+
15+
- `lower`: read-only, filesystem gốc, không thay đổi.
16+
- `upper`: read-write, filesystem lưu trữ các thay đổi (thay đổi/xóa/tạo mới).
17+
- `merged`: filesystem kết hợp dữ liệu của `upper``lower`.
18+
19+
![](./upper-lower-dir-768x371.png)
20+
21+
Để hiện thực hóa ý tưởng trên, cùng khởi tạo một overylay filesystem. Đầu tiên, khởi tạo các thư mục sau: `upper`, `lower`, `merged`. Bên cạnh đó, OverlayFS cần thêm một thư mục `work`. Đây là một thư mục tạm thời mà OverlayFS sử dụng để lưu trữ các thay đổi trong quá trình ghi vào filesystem. Khi bạn thực hiện thay đổi đối với tập tin trong `upper`, OverlayFS không ghi trực tiếp vào layer này mà thay vào đó lưu trữ các thay đổi vào `work` và sử dụng các thao tác kỹ thuật để "gộp" các thay đổi này lại.
22+
23+
```shell
24+
% mkdir /tmp/testoverlayfs
25+
$ cd /tmp/testpverlayfs
26+
$ mkdir upper lower merged work
27+
$ echo "lower file" > lower/in_lower.txt
28+
$ echo "upper file" > upper/in_upper.txt
29+
# in_both is in both directories
30+
$ echo "lower file" > lower/in_both.txt
31+
$ echo "upper file" > upper/in_both.txt
32+
```
33+
34+
Gộp thư mục `upper``lower` bằng lệnh `mount`:
35+
36+
```shell
37+
$ sudo mount -t overlay overlay -o lowerdir=/tmp/testoverlayfs/lower,upperdir=/tmp/testoverlayfs/upper,workdir=/tmp/testoverlayfs/work /tmp/testoverlayfs/merge
38+
```
39+
40+
Dùng `tree` để xem danh sách tệp tin trong các thư mục.
41+
42+
```shell
43+
$ tree
44+
.
45+
├── lower
46+
│ ├── in_both.txt
47+
│ └── in_lower.txt
48+
├── merged
49+
│ ├── in_both.txt
50+
│ ├── in_lower.txt
51+
│ └── in_upper.txt
52+
├── upper
53+
│ ├── in_both.txt
54+
│ └── in_upper.txt
55+
└── work
56+
└── work [error opening dir]
57+
58+
5 directories, 7 files
59+
```
60+
61+
Tệp tin `in_both.txt` có mặt trong cả `lower``upper`, vậy theo lý thuyết, nội dung tệp tin phải được đọc từ `upper`.
62+
63+
```shell
64+
$ cat merged/in_both.txt
65+
upper file
66+
```
67+
Tạo một tệp tin mới trong `merged`, tệp tin đồng thời cũng được khởi tạo trong `upper`.
68+
69+
```shell
70+
$ echo "new_file" > merged/new_file
71+
$ ls */new_file
72+
merged/new_file upper/new_file
73+
```
74+
75+
Sẽ ra sao nếu chúng ta xóa một tệp tin tại `merged`?
76+
77+
```shell
78+
$ rm merged/in_both.txt
79+
$ ls -la */in_both.txt
80+
-rw-rw-r-- 1 kiennt kiennt 11 Thg 12 3 12:33 lower/in_both.txt
81+
c--------- 2 root root 0, 0 Thg 12 3 13:09 upper/in_both.txt
82+
```
83+
84+
- `in_both.txt` vẫn còn ở thư mục `lower`, tệp tin không hề bị thay đổi.
85+
- Tệp tin đã bị xóa ở merged.
86+
-`upper`, vẫn còn tồn tại tệp tin `in_both.txt`? Thực ra, tệp tin này là một character device với device number 0/0. Kỹ thuật này được gọi là **whiteout**, đây là cách OverlayFS đánh dấu một tệp tin đã bị xóa tại `upper`.
87+
69.4 KB
Loading

0 commit comments

Comments
 (0)