如何使用 Docker Compose v3 直接掛載 NFS 共享/捲到容器中?
Docker 是一個廣泛用於開發和管理容器化應用程式的工具。它使程式設計師能夠將他們的應用程式及其依賴項組合到小型、可移植的容器中,這些容器易於在任何平臺上設定和使用。使用 Docker Compose v3 直接在容器內掛載網路檔案系統 (NFS) 共享或卷是 Docker 的一項實用功能。
在本文中,我們將探討如何使用 Docker Compose v3 直接在容器中掛載 NFS 共享或卷。
使用 Docker Compose v3 直接掛載容器中 NFS 共享/卷的方法
以下是一些我們需要理解和學習才能使用 Docker Compose v3 直接掛載容器中 NFS 共享/卷的重要術語和事實:
目標路徑 − “target” 鍵用於指定應在容器內掛載 NFS 共享或卷的位置。可以使用容器內任何您希望掛載卷的目錄路徑作為此路徑。
卷型別 − 使用 “type” 鍵指示正在掛載的卷型別。對於 NFS 共享或卷,應將型別設定為“volume”。
源路徑 − 使用 “source” 鍵指定主機上 NFS 共享或卷的路徑。此處可以使用指向主機系統上 NFS 共享或磁碟的任何目錄路徑。
卷選項 − “volume” 鍵可用於指定其他卷選項。其中一個選項是 “nocopy”;可以將其設定為 true 以阻止將卷的內容複製到容器中。這有助於最佳化最終映像的大小並減少層數。
我們現在將透過一個示例更好地理解和演示這一點。
示例
步驟 1 − 為您的專案建立一個新目錄並導航到該目錄:
$ mkdir directoryname $ cd directoryname
步驟 2 − 在此新目錄中建立一個名為 docker-compose.yml 的檔案,內容如下:
version: "3" services: web-server: image: nginx:latest ports: - 80:80 volumes: - type: volume source: nfs-volume target: /nfs volume: nocopy: true volumes: nfs-volume: driver_opts: type: "nfs" o: "addr=10.40.0.199,nolock,soft,rw" device: ":/var/data"
這指示在檔案中使用的 Docker Compose 版本,為服務命名,指定合適的映象來定義它,在服務下新增一個 volumes 鍵,並指定卷的型別、源路徑和目標路徑。此外,它還將 volume 鍵下的 nocopy 選項設定為 true,以阻止將卷的內容複製到容器中。
步驟 3 − 在同一目錄中建立一個 Dockerfile,內容如下:
FROM nginx:latest COPY . /usr/share/nginx/html EXPOSE 80 CMD ["nginx", "-g", "daemon off;"]
在此示例中,我們使用 `nginx:latest` 映象將主機系統上 `/mnt/nfs/share` 中儲存的 NFS 共享或卷掛載到容器內的 `/app`。為了防止將卷的內容複製到容器中,將 `nocopy` 選項指定為 `true`。
步驟 4 − 使用 `docker-compose up` 命令啟動帶有已掛載卷的容器:
docker-compose up
輸出
`docker-compose up` 命令的輸出應類似於此:
[+] Running 2/2 - Network examp_default Created 0.9s - Container examp-web-1 Created 0.1s Attaching to examp-web-1 examp-web-1 | * Serving Flask app 'app' examp-web-1 | * Debug mode: on examp-web-1 | WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. examp-web-1 | * Running on all addresses (0.0.0.0) examp-web-1 | * Running on http://127.0.0.1:5000 examp-web-1 | * Running on http://172.19.0.2:5000 examp-web-1 | Press CTRL+C to quit examp-web-1 | * Restarting with stat examp-web-1 | * Debugger is active! examp-web-1 | * Debugger PIN: 630-981-535 examp-web-1 | 172.19.0.1 - - [09/Jan/2023 16:46:53] "GET / HTTP/1.1" 200 - examp-web-1 | 172.19.0.1 - - [09/Jan/2023 16:46:53] "GET /favicon.ico HTTP/1.1" 404 - Gracefully stopping... (press Ctrl+C again to force) [+] Running 1/1 - Container examp-web-1 Stopped 2.0s canceled PS C:\DikshaDen\docker-apps\examp> cd .. PS C:\DikshaDen\docker-apps> docker-compose up no configuration file provided: not found PS C:\DikshaDen\docker-apps> docker-compose up no configuration file provided: not found PS C:\DikshaDen\docker-apps> docker-compose up --build no configuration file provided: not found PS C:\DikshaDen\docker-apps> docker-compose up --build no configuration file provided: not found PS C:\DikshaDen\docker-apps> docker-compose build no configuration file provided: not found PS C:\DikshaDen\docker-apps> cd mount-nfs PS C:\DikshaDen\docker-apps\mount-nfs> docker-compose up --build [+] Running 7/7 - web-server Pulled 40.6s - 3f4ca61aafcd Pull complete 18.0s - 50c68654b16f Pull complete 19.3s - 3ed295c083ec Pull complete 19.4s - 40b838968eea Pull complete 19.6s - 88d3ab68332d Pull complete 19.7s - 5f63362a3fa3 Pull complete 19.8s time="2023-01-09T22:48:42+05:30" level=warning msg="Found orphan containers ([mount-nfs-web-1]) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up."
此輸出演示了主機系統的 `/mnt/nfs/share` NFS 共享或卷已使用 `nginx:latest` 映象掛載到容器的 `/app`。為了防止將卷的內容複製到容器中,`nocopy` 選項設定為 `true`。
結論
在本文中,我們探討了如何使用 Docker Compose v3 直接在 Docker 容器中掛載網路檔案系統 (NFS) 共享或卷。我們討論了相關的各種術語和事實,例如卷型別、源路徑、目標路徑和卷選項。我們還提供了一個示例程式碼和輸出,以顯示該過程的工作方式。您可以按照本文中的說明,成功地使用 Docker Compose v3 在 Docker 容器中掛載 NFS 共享或卷。這將使您能夠訪問外部 NFS 伺服器上的儲存資料或在容器之間傳輸檔案。