Skip to content

Commit 5eb7092

Browse files
authored
Merge pull request #103419 from PedroGuerraPT/patch-1
[Azure App Service] Enable SSH Section Refactor
2 parents b36a05f + a7995cd commit 5eb7092

File tree

1 file changed

+63
-41
lines changed

1 file changed

+63
-41
lines changed

articles/app-service/configure-custom-container.md

Lines changed: 63 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Configure a custom container
33
description: Learn how to configure a custom container in Azure App Service. This article shows the most common configuration tasks.
44

55
ms.topic: how-to
6-
ms.date: 10/22/2021
6+
ms.date: 01/04/2023
77
ms.custom: devx-track-azurepowershell, devx-track-azurecli
88
zone_pivot_groups: app-service-containers-windows-linux
99
---
@@ -363,13 +363,10 @@ Group Managed Service Accounts (gMSAs) are currently not supported in Windows co
363363

364364
## Enable SSH
365365

366-
SSH enables secure communication between a container and a client. In order for a custom container to support SSH, you must add it into your Docker image itself.
367-
368-
> [!TIP]
369-
> All built-in Linux containers in App Service have added the SSH instructions in their image repositories. You can go through the following instructions with the [Node.js 10.14 repository](https://github.com/Azure-App-Service/node/blob/master/10.14) to see how it's enabled there. The configuration in the Node.js built-in image is slightly different, but the same in principle.
370-
371-
- Add [an sshd_config file](https://man.openbsd.org/sshd_config) to your repository, like the following example.
366+
Secure Shell (SSH) is commonly used to execute administrative commands remotely from a command-line terminal. In order to enable the Azure portal SSH console feature with custom containers, the following steps are required:
372367

368+
1. Create a standard [sshd_config](https://man.openbsd.org/sshd_config) file with the following example contents and place it on the application project root directory:
369+
373370
```
374371
Port 2222
375372
ListenAddress 0.0.0.0
@@ -384,54 +381,79 @@ SSH enables secure communication between a container and a client. In order for
384381
PermitRootLogin yes
385382
Subsystem sftp internal-sftp
386383
```
387-
384+
388385
> [!NOTE]
389-
> This file configures OpenSSH and must include the following items:
386+
> This file configures OpenSSH and must include the following items in order to comply with the Azure portal SSH feature:
390387
> - `Port` must be set to 2222.
391388
> - `Ciphers` must include at least one item in this list: `aes128-cbc,3des-cbc,aes256-cbc`.
392389
> - `MACs` must include at least one item in this list: `hmac-sha1,hmac-sha1-96`.
393-
394-
- Add an ssh_setup script file to create the SSH keys [using ssh-keygen](https://man.openbsd.org/ssh-keygen.1) to your repository.
395-
390+
391+
2. Create an entrypoint script with the name `entrypoint.sh` (or change any existing entrypoint file) and add the command to start the SSH service, along with the application startup command. The following example demonstrates starting a Python application. Please replace the last command according to the project language/stack:
392+
393+
### [Debian](#tab/debian)
394+
395+
```Bash
396+
#!/bin/sh
397+
set -e
398+
service ssh start
399+
exec gunicorn -w 4 -b 0.0.0.0:8000 app:app
396400
```
401+
402+
### [Alpine](#tab/alpine)
403+
404+
```Bash
397405
#!/bin/sh
398-
399-
ssh-keygen -A
400-
401-
#prepare run dir
402-
if [ ! -d "/var/run/sshd" ]; then
403-
mkdir -p /var/run/sshd
404-
fi
406+
set -e
407+
/usr/sbin/sshd
408+
exec gunicorn -w 4 -b 0.0.0.0:8000 app:app
405409
```
406-
407-
- In your Dockerfile, add the following commands:
408-
410+
---
411+
412+
3. Add to the Dockerfile the following instructions according to the base image distribution. The same will copy the new files, install OpenSSH server, set proper permissions and configure the custom entrypoint, and expose the ports required by the application and SSH server, respectively:
413+
414+
### [Debian](#tab/debian)
415+
409416
```Dockerfile
410-
# Install OpenSSH and set the password for root to "Docker!". In this example, "apk add" is the install instruction for an Alpine Linux-based image.
411-
RUN apk add openssh \
412-
&& echo "root:Docker!" | chpasswd
413-
414-
# Copy the sshd_config file to the /etc/ssh/ directory
417+
COPY entrypoint.sh ./
418+
419+
# Start and enable SSH
420+
RUN apt-get update \
421+
&& apt-get install -y --no-install-recommends dialog \
422+
&& apt-get install -y --no-install-recommends openssh-server \
423+
&& echo "root:Docker!" | chpasswd \
424+
&& chmod u+x ./entrypoint.sh
415425
COPY sshd_config /etc/ssh/
416-
417-
# Copy and configure the ssh_setup file
418-
RUN mkdir -p /tmp
419-
COPY ssh_setup.sh /tmp
420-
RUN chmod +x /tmp/ssh_setup.sh \
421-
&& (sleep 1;/tmp/ssh_setup.sh 2>&1 > /dev/null)
422-
423-
# Open port 2222 for SSH access
424-
EXPOSE 80 2222
426+
427+
EXPOSE 8000 2222
428+
429+
ENTRYPOINT [ "./entrypoint.sh" ]
425430
```
426-
431+
432+
### [Alpine](#tab/alpine)
433+
434+
```Dockerfile
435+
COPY sshd_config /etc/ssh/
436+
COPY entrypoint.sh ./
437+
438+
# Start and enable SSH
439+
RUN apk add openssh \
440+
&& echo "root:Docker!" | chpasswd \
441+
&& chmod +x ./entrypoint.sh \
442+
&& cd /etc/ssh/ \
443+
&& ssh-keygen -A
444+
445+
EXPOSE 8000 2222
446+
447+
ENTRYPOINT [ "./entrypoint.sh" ]
448+
```
449+
---
450+
427451
> [!NOTE]
428452
> The root password must be exactly `Docker!` as it is used by App Service to let you access the SSH session with the container. This configuration doesn't allow external connections to the container. Port 2222 of the container is accessible only within the bridge network of a private virtual network and is not accessible to an attacker on the internet.
429453
430-
- In the start-up script for your container, start the SSH server.
454+
4. Rebuild and push the Docker image to the registry, and then test the Web App SSH feature on Azure portal.
431455
432-
```bash
433-
/usr/sbin/sshd
434-
```
456+
For further troubleshooting additional information is available at the Azure App Service OSS blog: [Enabling SSH on Linux Web App for Containers](https://azureossd.github.io/2022/04/27/2022-Enabling-SSH-on-Linux-Web-App-for-Containers/index.html#troubleshooting)
435457
436458
## Access diagnostic logs
437459

0 commit comments

Comments
 (0)