I would rather copy a stack that runs than write the same compose file again from memory.
Stacks > mystack > Create template from stack
Open the deployed stack and press Create template from stack. Portainer asks for a title, description, platform and type, fills the web editor with the stack’s compose file and creates the custom template when you press Create custom template.
Check the file before you save. Anything specific to this one deployment, such as hostnames or real passwords, belongs out of the template, or in variables if you have Business Edition.
Further reading: Portainer docs: Create a template from a deployed stack