To ensure that a screen session persists in a Docker container, you need to make sure that the screen session is properly initialized and that the necessary environment is maintained. Your current script is almost correct, but you may need to adjust the way the screen session is created and ensure that the screen socket directory is accessible.
Here’s an improved version of your script:
#!/bin/sh
mkdir -p /run/screen
screen -S my_screen -dm bash -c 'cd /path/to/project && npm run start'
screen -list > /path/to/scr_list.log
Make sure to replace /path/to/project and /path/to/scr_list.log with the actual paths in your container. Additionally, ensure that the Docker container is not running in a way that restricts access to the /run/screen directory.
After running this script, you should be able to connect to the screen session using screen -r my_screen and see the output as expected. If you still encounter issues, check the permissions of the screen socket directory and ensure that the Docker container is not exiting immediately after the script runs.