When creating a file using the RUN command, any files created in the designated shared volume folder dissapear when they are trying to be accessed in the CMD command
Where the volumes are:
... volumes: - './hostpersist:/usr/src/app/persist'
e.g., the following Dockerfile does not create a file, the file is not visible by the
index.js script, nor by the host filesystem
... WORKDIR /usr/src/app ... # creates file in persist/file.txt RUN node migrate.js # persist/file.txt does not exist CMD [ "node", "index.js" ]
When putting both scripts to run in the CMD, the file is created and visible by
... # persist/file.txt exists CMD node migrate.js && node index.js
Is there any way to keep file changes persistent without stuffing everything into CMD?
The volume is mounted AFTER the container is created (instantiated), thus overwriting the content you’re creating during runtime of the container image (which is RUN do_something_on_/usr/src/app/persist).
If you leave out the volumes: declaration in your docker-compose file, you will see, that the files created by your node migrate.js call will be available in your container.
So, summing up, I’d go for the runtime installation, which is your CMD call to both node executions. These will definitely be called AFTER the container is created and your local host volume is mounted.