# 容器的匯出與匯入

## **容器的匯出**

**匯出運作的容器之前，我們先attach容器，並安裝一些常用工具**<br>

**docker attach firstubuntu**

**root\@4eb602fae6d4:/# apt update**

**root\@4eb602fae6d4:/# apt install net-tools tasksel**

**root\@4eb602fae6d4:/# cd /root; echo "someText" > testfile**<br>

**安裝net-tools套件，我們才可以使用諸如ifconfig這個指令**<br>

**按下 ctrl + P 然後 ctrl + Q 跳離容器，讓它繼續在背景執行**

**docker ps 可以看到firstubuntu容器仍在執行**

**docker export firstubuntu > ubuntu-\`date +%Y%m%d\`.tar**<br>

**匯出的容器快照檔案將丟棄所有的歷史記錄和原始資料訊息，如ENTRYPOINT（即僅保存容器當時的快照狀態）**<br>

## **容器的匯入**

**匯入容器的包，可以重新作為一個映像檔使用，並包含之前安裝的套件與新增的檔案**

**由於匯出的容器快照檔案將丟棄所有的歷史記錄和原始資料訊息，如ENTRYPOINT，所以，我們須重新指定他的ENTRYPOINT，如下**

**docker import --change 'CMD \["/bin/bash"]' ubuntu-xxxxxx.tar ubuntu1804:v1**

**匯入此容器，就會生成新的images，你可以看到新的image ubuntu1804:v1**<br>

![](/files/-LjtMjOQFIIXi1ng-ftu)

### **測試由新的映像檔產生容器，並設定其主機名稱**

**docker run -itd --name test --hostname test ubuntu1804:v1**<br>

**docker attach test**<br>

**root\@test:\~#**

**root\@test:\~# cd root; cat testfile**<br>

**測試的結果，可以發現，由新的映像檔產生的容器，之前安裝的套件與新增檔案都在**<br>

**基本操作至此，雖不困難，但都是文字指令，如果有圖形界面操作，則可以省去許多麻煩，像是要看到目前執行的容器狀態，容器的使用狀況等，如果都要使用指令，也是相當累人的，因此，一個簡潔易懂的UI是必須的，這裡，我推薦使用portainer**<br>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kawsing.gitbook.io/opensystem/docker-cong-an-zhuang-dao-ying-yong-ru-men-pian/rong-qi-de-hui-chu-yu-hui-ru.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
