Of course using an image which has the required jdk as default will solve this issue. But in this case you even don't need the "jdk 'openjdk-17.0.5'" in your pipeline, because jenkins and the image don't have any other option as using this jdk.
If you really have an generic image which contains several jdk versions from which you select the proper one for your job via the tools jdk setting, you need to configure the jdks which are available in your image in the jenkins docker agent template settings. Define each available jdk as a "tool" under "node properties". To be able to select the jdks there you must define them before in the tools configuration of jenkins itself.
The "name" you define for the jdk must match the tool jdk name in your pipeline.