RUN instruction in your Dockerfile, you can seamlessly integrate this utility for network diagnostics without increasing the container footprint unnecessarily.
Understanding Feature Management in Modern Windows Containers
Historically, enabling features in Windows Server containers involved using theAdd-WindowsFeature cmdlet or the dism command-line tool. However, Microsoft has deprecated these methods for newer operating system versions, particularly Windows Server 2019 LTSC and 2022 LTSC. The primary reason for this shift is that these newer OS versions utilize a more modular component store managed through PowerShell. If you attempt to use Add-WindowsFeature on an LTSC-based image, the build will fail because the cmdlet simply does not exist in the default environment.
For developers working with Windows containers, it is crucial to recognize that "Nano Server" and "Windows Server Core" are no longer the only distinct choices; we now have LTSC variants that behave differently. While some older guides suggest using Install-WindowsFeature, this is technically incorrect for the current generation of Windows Server containers. Instead, the supported method relies entirely on the Enable-WindowsOptionalFeature cmdlet available in PowerShell. This change requires updating your Dockerfiles to reflect the correct syntax, ensuring that your builds remain stable across different host environments.
The Correct PowerShell Approach
The recommended way to add telnet support is by leveraging theEnable-WindowsOptionalFeature command. This cmdlet interacts directly with the Windows Component Store (WinSxS) to install optional features. When constructing your Dockerfile, you should invoke PowerShell within a RUN instruction to execute this command. It is important to include the -Online parameter, which targets the running operating system instance within the container, and the -NoRestart parameter to prevent unnecessary reboot cycles during the build phase. This results in a cleaner and faster build process, as the container does not need to restart between layer creation.
Constructing the Dockerfile
Creating a custom image that includes telnet.exe is straightforward once you know the correct syntax. You start with a standard Windows base image, such asmcr.microsoft.com/windows/servercore:ltsc2022. From there, you add a step to enable the client feature. Below is a complete example of a minimal Dockerfile that successfully installs the telnet client.
FROM mcr.microsoft.com/windows/servercore:ltsc2022
# Enable the Telnet Client feature
RUN powershell -Command \
Enable-WindowsOptionalFeature -Online -FeatureName TelnetClient -NoRestart
# Optional: Verify installation
RUN telnet localhost 80
CMD ["cmd"]
In this example, the FROM statement pulls the latest LTSC 2022 server core image. The RUN command executes PowerShell to enable the TelnetClient feature. Note that the feature name is case-sensitive and must match exactly what is registered in the Windows Component Store. After the build completes, the resulting image will contain telnet.exe in its system path, allowing you to use it immediately upon container startup.
Common Pitfalls and Troubleshooting
Many users encounter errors when trying to adapt old scripts to new container bases. One common mistake is assuming that all Windows container images support the same set of PowerShell modules. For instance, if you are using a very minimal base image, you might find that PowerShell itself is not fully featured. However, standard Server Core images include the necessary infrastructure forEnable-WindowsOptionalFeature. If you receive an error stating that the feature cannot be found, double-check the spelling of TelnetClient. Another frequent issue arises from network policies; while telnet can connect to ports, firewalls on the host machine or within the container network configuration may block outbound connections. Always test connectivity using simple tools like Test-NetConnection in PowerShell before relying on telnet for complex debugging.
Alternative Diagnostic Tools
While telnet is useful for basic port testing, modern Windows containers often benefit from more robust networking diagnostic tools. PowerShell providesTest-NetConnection, which offers detailed output about TCP handshake success, latency, and route information. Additionally, tools like curl are frequently pre-installed or easily added to images for HTTP-specific testing. If you are building a production-grade container, consider whether telnet is truly necessary or if built-in PowerShell cmdlets can achieve the same diagnostic goals without adding extra binaries to your image. This aligns with best practices for keeping container images lean and secure. For more advanced scenarios involving graphical interfaces or complex application deployments, you might also want to explore how other applications are handled in native kernel-based environments, as discussed in our guide on Running GUI Applications in Native Kernel-based Windows Containers. Understanding the broader context of application deployment helps in making informed decisions about which utilities to include in your final container image.
Frequently Asked Questions
Does Nano Server support Enable-WindowsOptionalFeature?
No, Nano Server does not include the full PowerShell module required for Enable-WindowsOptionalFeature. This cmdlet is primarily available in Server Core and Desktop Experience images. If you are using Nano Server, you generally cannot install telnet or similar optional features because they are not part of the minimal component set.
Why did my previous Dockerfile fail with Add-WindowsFeature?
The Add-WindowsFeature cmdlet was removed in Windows Server 2016 and later versions. If you are using LTSC 2022 or 2019 images, this cmdlet no longer exists. You must switch to Enable-WindowsOptionalFeature to successfully install features during the build process.
Can I install telnet at runtime instead of build time?
Technically yes, but it is highly discouraged. Installing features at runtime requires the container to have write access to the system drive and may trigger restarts. Building the feature into the image layer ensures consistency and allows you to verify the installation during the CI/CD pipeline.
Is telnet secure to use in production containers?
Telnet transmits data in plaintext, including credentials, making it insecure for production environments. It is recommended only for internal debugging or isolated testing networks. For secure remote access or file transfer, use SSH or SFTP equivalents instead.
What if I need other optional features besides telnet?
You can install multiple features by chaining commands or listing them in a single Enable-WindowsOptionalFeature call. For example, you can add PowershellISE or NetFx4-AdvShr using the same syntax. Always ensure the feature name matches the official Windows documentation.