如何使用docker-compose v3.1管理金鑰值?
介紹
作為開發者,我們經常需要將私密資料(包括密碼、API 金鑰和資料庫憑據)整合到我們的應用程式中。將這些變數硬編碼到我們的程式碼或配置檔案中不僅不安全,而且在需要時管理和更改它們也可能很困難。
使用環境變數是一種管理金鑰值的方法,它允許我們將敏感資料與我們的程式碼庫和配置檔案分開。在本文中,我們將探討如何使用 docker-compose v3.1 管理金鑰值,並將它們作為環境變數注入到我們的容器中。
前提條件
要學習本教程,您需要在您的機器上安裝 Docker 和 docker-compose v3.1。您可以使用以下命令在您的終端中檢視是否已安裝這些實用程式:
$ docker --version $ docker-compose --version
方法
我們可以使用多種方法來使用 docker-compose v3.1 管理金鑰值。
其中一些方法包括:
使用環境變數
使用 .env 檔案
現在讓我們詳細討論這些方法並舉例說明。
使用環境變數
使用 docker-compose v3.1 管理金鑰值的一種方法是使用環境變數。環境變數是在執行時傳遞給容器的鍵值對。它們可以在 docker-compose 檔案中設定,也可以從主機傳遞。
示例 1
要在 docker-compose 檔案中設定環境變數,我們可以在我們要設定變數的服務下使用 `environment` 鍵。
步驟 1 - 在您的程式碼編輯器中導航到您的專案目錄。
要使用終端導航,請使用以下命令:
$cd /directory-path
步驟 2 - 在您的 `docker-compose.yml` 檔案中,在我們要設定變數的服務下指定 `environment` 鍵。
version: "3.1" services: web: image: nginx:latest ports: - 8080:80 environment: - API_KEY=123456
步驟 3 - 在同一目錄中新增名為“Dockerfile”的相應 Dockerfile,其中不包含以下內容:
FROM nginx:latest EXPOSE 80 ENV API_KEY=123456
步驟 4 - 現在透過在終端中執行以下命令來執行和構建此 docker-compose:
$docker-compose up
輸出
[+] Running 1/1 - Container examp2-web-1 Recreated 0.9s Attaching to examp2-web-1 examp2-web-1 | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration examp2-web-1 | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ examp2-web-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh … examp2-web-1 | 2023/01/09 17:53:54 [notice] 1#1: start worker process 38 examp2-web-1 | 2023/01/09 17:53:54 [notice] 1#1: start worker process 39 examp2-web-1 | 2023/01/09 17:53:54 [notice] 1#1: start worker process 40
使用 .env 檔案
使用 docker-compose v3.1 管理金鑰值的另一種方法是使用 .env 檔案。.env 檔案是一個包含在執行時傳遞給容器的鍵值對列表的檔案。docker-compose 檔案和 .env 檔案必須都在同一目錄中。
步驟 1 - 在您的程式碼編輯器中導航到您的專案目錄。
步驟 2 - 在您的專案目錄中建立一個名為 .env 的檔案。
步驟 3 - 要將 .env 檔案與 docker-compose v3.1 一起使用,我們可以使用 `api key` 命令在 .env 檔案中設定環境變數,如下所示:
API_KEY=123456
步驟 4 - 使用以下命令在終端中執行 .env 檔案。
$ cat .env
輸出
API_KEY=123456
步驟 5 - 在同一目錄中建立一個 `docker-compose.yml` 檔案,然後使用 `${VAR_NAME}` 語法引用這些環境變數:
version: "3.1" services: web: image: nginx:latest ports: - 8080:80 environment: - API_KEY=${API_KEY}
步驟 6 - 使用以下命令在終端中輸出 `docker-compose.yml` 檔案的內容:
$ cat docker-compose.yml
輸出
For Output Code pre classversion: "3.1" services: web: environment: - API_KEY=${API_KEY}
步驟 7 - 使用終端在終端中執行此檔案:
$ docker-compose up
輸出
[+] Running 1/0 - Container examp2-web-1 Created 0.0s Attaching to examp2-web-1 examp2-web-1 | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration examp2-web-1 | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ examp2-web-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh examp2-web-1 | 10-listen-on-ipv6-by-default.sh: info: IPv6 listen already enabled examp2-web-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh examp2-web-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh examp2-web-1 | /docker-entrypoint.sh: Configuration complete; ready for start up examp2-web-1 | 2023/01/09 18:04:52 [notice] 1#1: using the "epoll" event method examp2-web-1 | 2023/01/09 18:04:52 [notice] 1#1: nginx/1.23.3 examp2-web-1 | 2023/01/09 18:04:52 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) examp2-web-1 | 2023/01/09 18:04:52 [notice] 1#1: OS: Linux … examp2-web-1 | 2023/01/09 18:04:52 [notice] 1#1: start worker process 29 examp2-web-1 | 2023/01/09 18:04:52 [notice] 1#1: start worker process 30 examp2-web-1 | 2023/01/09 18:04:52 [notice] 1#1: start worker process 31 examp2-web-1 | 2023/01/09 18:04:52 [notice] 1#1: start worker process 32 examp2-web-1 | 2023/01/09 18:04:52 [notice] 1#1: start worker process 33
結論
在本文中,我們探討了使用 docker-compose v3.1 管理金鑰值的幾種方法。我們可以使用環境變數、.env 檔案和 Docker 金鑰以安全的方式儲存和管理敏感資料。我們還查看了各種實現示例。透過使用這些方法,我們可以避免在程式碼庫中以純文字形式儲存金鑰值,並降低安全漏洞的風險。