Hãy tưởng tượng một kịch bản trong đó bạn đã tạo ra docker image hoặc docker container Docker mà bạn muốn lưu giữ và chia sẻ nó với người khác hoặc đồng nghiệp khác của mình. Các phương pháp dưới đây sẽ giúp bạn đạt được nó.

Bốn Docker CLI cơ bản đi vào hoạt động:

  • – docker export Xuất hệ thống tệp của container dưới dạng file nén tar
  • – docker import Import từ tarball để tạo image từ file tar
  • – docker save Lưu một hoặc nhiều image vào kho lưu trữ tar (được truyền trực tuyến tới STDOUT theo mặc định)
  • – docker load Load image từ kho lưu trữ tar hoặc STDIN

Yêu cầu

  • Tạo một tài khoản với DockerHub
  • Mở Nền tảng PWD trên trình duyệt của bạn
  • Nhấp vào Thêm phiên bản mới ở bên trái màn hình để hiển thị phiên bản Alpine OS ở bên phải

Tạo container Nginx

$ docker run -d -p 80:80 nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
a5a6f2f73cd8: Pull complete
1ba02017c4b2: Pull complete
33b176c904de: Pull complete
Digest: sha256:5d32f60db294b5deb55d078cd4feb410ad88e6fe77500c87d3970eca97f54dba
Status: Downloaded newer image for nginx:latest
df2caf9283e84a15bb2321a17aabe84e3e0762ec82fc180e2a4c15fcf0f96588

Hiển thị container đang chạy

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
df2caf9283e8        nginx               "nginx -g 'daemon of…"   35 seconds ago      Up 34 seconds       0.0.0.0:80->80/tcp   vigorous_jang

Export container ra tar file.

$ docker export df2 > nginx.tar

Bạn có thể commit container này dưới dạng image mới, nhưng bạn cũng có thể sử dụng lệnh import Docker:

$ docker import - mynginx < nginx.tar
sha256:aaaed50d250a671042e8dc383c6e05012e245f5eaf555d10c40be63f6028ee7b
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mynginx             latest              aaaed50d250a        25 seconds ago      107MB
nginx               latest              568c4670fa80        2 weeks ago         109MB

Nếu bạn muốn chia sẻ image này với một trong những cộng tác viên của mình, bạn có thể tải tệp tar lên webserver và để cộng tác viên của bạn tải xuống và sử dụng lệnh import trên server Docker của anh ấy.

Nếu bạn muốn export các image mà bạn đã commit, bạn có thể sử dụng các lệnh sau:

$ docker save -o mynginx1.tar nginx
$ ls -l
total 218756
-rw-------    1 root     root     112844800 Dec 18 02:53 mynginx1.tar
-rw-r--r--    1 root     root     111158784 Dec 18 02:50 nginx.tar
$ docker rmi mynginx
Untagged: mynginx:latest
Deleted: sha256:aaaed50d250a671042e8dc383c6e05012e245f5eaf555d10c40be63f6028ee7b
Deleted: sha256:41135ad184eaac0f5c4f46e4768555738303d30ab161a7431d28a5ccf1778a0f

Bây giờ hãy xóa tất cả image và container đang chạy và thử chạy lệnh bên dưới để import image Docker vào hệ thống của bạn:

$ docker images
REPOSITORY        TAG            IMAGE ID        CREATED         SIZE
$ docker load < mynginx1.tar
Loaded image: nginx:latest
root@192.168.0.33 ~$ docker images
REPOSITORY      TAG             IMAGE ID          CREATED             SIZE
nginx           latest         568c4670fa80        2 weeks ago        109MB
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments