Accepted answer

COPY inside your Dockerfile actually copies the files to the given path. Imagine this being carved in ice. Like once you copy everything, unless you re-build your image the files cannot be modified from outside. Where as the volume is actually creating a mounting point to the host. So if you are doing development where you need to change the code, and you use COPY you have to re-build the image everytime you want to have your changes to be in the container, whereas with volume you do not have to do rebuild your image the changes will be transfered to the container.

Use volumes during development or if you have anything you want to have a persistance, like databases and such. If you do not volume mount your database, whenever you restart your container data will be lost.

Use COPY when you are ready to ship the application and you are sure you do not modify the source code anymore.

For further reading please look at this : Docker ADD vs COPY vs VOLUME

I hope this answers your question.

Related Query

More Query from same tag