共用方式為


在 Azure App Service 中開啟容器的 SSH 工作階段

安全殼層 (SSH) 可用來從遠端執行容器的系統管理命令。 App Service 會直接將 SSH 支援提供給裝載在 Windows 自定義容器中的應用程式。

Windows 自定義容器不需要任何特殊設定, 瀏覽器 SSH 工作階段 才能運作。 不支援透過 Azure CLI 的 SSH 工作階段。

Linux App Service SSH

安全殼層 (SSH) 可用來從遠端執行容器的系統管理命令。 App Service 直接將 SSH 支援提供給裝載在 Linux 容器中的應用程式(內建或自定義)。

內建的Linux容器已具備啟用 SSH 作業階段的必要設定。 Linux 自定義容器需要額外的設定,才能啟用 SSH 會話。 請參閱 啟用 SSH

Linux App Service SSH

您也可以使用 SSH 和 SFTP,直接從本機開發電腦連線到容器。

在瀏覽器中開啟 SSH 工作階段

若要透過容器直接開啟 SSH 工作階段,您的應用程式應在執行中。

在瀏覽器中貼入下列 URL,並以您的應用程式名稱取代 <app-name>

https://<app-name>.scm.azurewebsites.net/webssh/host

如果您尚未經過驗證,必須向您的 Azure 訂用帳戶進行驗證才能連線。 驗證之後,您會看到瀏覽器中的殼層,您可以在其中執行您容器內的命令。

SSH 連線

使用 Azure CLI 開啟 SSH 工作階段

您可以使用 TCP 信道,透過已驗證的 WebSocket 連線,在開發電腦與 Linux 容器之間建立網路連線。 它可讓您從所選的用戶端,開啟您的容器在 App Service 中執行的 SSH 工作階段。

若要開始,您必須安裝 Azure CLI。 若要查看未安裝 Azure CLI 時的運作方式,請開啟 Azure Cloud Shell

使用 az webapp create-remote-connection 命令,開啟您應用程式的遠端連線。 為您的應用程式指定 <subscription-id><group-name><app-name>

az webapp create-remote-connection --subscription <subscription-id> --resource-group <resource-group-name> -n <app-name> &

提示

命令尾端的 & 只是為了方便您使用 Cloud Shell。 它會在背景執行程序,以便您在相同的殼層中執行下一個命令。

注意

如果此命令失敗,請確定使用下列命令停用遠端偵錯

az webapp config set --resource-group <resource-group-name> -n <app-name> --remote-debugging-enabled=false

命令輸出會為您提供開啟 SSH 工作階段所需的資訊。

Verifying if app is running....
App is running. Trying to establish tunnel connection...
Opening tunnel on addr: 127.0.0.1
Opening tunnel on port: <port-output>
SSH is available { username: root, password: Docker! }
Ctrl + C to close

使用輸出 (<port-output>) 中提供的本機埠,使用您選擇的客戶端,開啟與容器的 SSH 工作階段。 例如,使用 linux ssh 命令,您可以執行單一命令,例如 java -version

ssh root@127.0.0.1 -m hmac-sha1 -p <port-output> java -version

或者,若要輸入完整的SSH會話,請直接執行:

ssh root@127.0.0.1 -m hmac-sha1 -p <port-output>

在出現提示時,輸入 yes 繼續連線。 系統會接著提示您輸入密碼。 使用稍早所示的 Docker!

Warning: Permanently added '[127.0.0.1]:21382' (ECDSA) to the list of known hosts.
root@127.0.0.1's password:

驗證後,您應會看到工作階段歡迎畫面。

  _____
  /  _  \ __________ _________   ____
 /  /_\  \___   /  |  \_  __ \_/ __ \
/    |    \/    /|  |  /|  | \/\  ___/
\____|__  /_____ \____/ |__|    \___  >
        \/      \/                  \/
A P P   S E R V I C E   O N   L I N U X

0e690efa93e2:~#

您現在已連線到您的連接器。

嘗試執行 top 命令。 您應能夠在程序清單中看到您應用程式的程序。 在下列範例輸出中,它是具有 PID 263 的程序。

Mem: 1578756K used, 127032K free, 8744K shrd, 201592K buff, 341348K cached
CPU:   3% usr   3% sys   0% nic  92% idle   0% io   0% irq   0% sirq
Load average: 0.07 0.04 0.08 4/765 45738
  PID  PPID USER     STAT   VSZ %VSZ CPU %CPU COMMAND
    1     0 root     S     1528   0%   0   0% /sbin/init
  235     1 root     S     632m  38%   0   0% PM2 v2.10.3: God Daemon (/root/.pm2)
  263   235 root     S     630m  38%   0   0% node /home/site/wwwroot/app.js
  482   291 root     S     7368   0%   0   0% sshd: root@pts/0
45513   291 root     S     7356   0%   0   0% sshd: root@pts/1
  291     1 root     S     7324   0%   0   0% /usr/sbin/sshd
  490   482 root     S     1540   0%   0   0% -ash
45539 45513 root     S     1540   0%   0   0% -ash
45678 45539 root     R     1536   0%   0   0% top
45733     1 root     Z        0   0%   0   0% [init]
45734     1 root     Z        0   0%   0   0% [init]
45735     1 root     Z        0   0%   0   0% [init]
45736     1 root     Z        0   0%   0   0% [init]
45737     1 root     Z        0   0%   0   0% [init]
45738     1 root     Z        0   0%   0   0% [init]

下一步

您可以在 Azure 論壇張貼問題和疑難。

如需「用於容器的 Web App」的詳細資訊,請參閱: