Update to latest version

I want to update from the old version (2.2.31.1) to the latest version (2.4.7) without losing data. Is it possible? If yes, please guide me on how to perform the update. I tried docker-compose pull but only updates images to the latest version and creates a duplicate of images (old ones and new ones). Tried changing the old docker-compose.yaml file to match the new version but also creates duplicate images.