Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dockerfile: files created outside CMD disappear when using a volume

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

...
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 index.js

...
# 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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading