I believe there is an easy way to copy files into a docker volume that has already been mounted to a container.
docker cp /tmp/my_data/. my_container:/my_data
as referenced by How to copy multiple files into a docker data volume
But, how does one create a named volume using docker volume create --name my-volume
that already has files in it?
I have read that it's not a good idea to cp files into the {{.Mountpoint}}
.
I'm new to docker and all of it's nuancies, so apologies if my fundamental understanding of volumes is incorrect.
Docker automatically creates a directory for the volume on the host under the /var/lib/docker/volume/ path. You can now mount this volume on a container, ensuring data persistence and data sharing among multiple containers.
It is actually the path within the container where you would like to mount /path/from/host . For example, if you had a directory on the host, /home/edward/data , and you wanted the contents of that directory to be available in the container at /data , you would use -v /home/edward/data:/data .
Approach #1 - COPY
To copy the file from the host to the container
docker cp /path/of/the/file <Container_ID>:/path/of/he/container/folder
Issue with the above approch is, it will not persists the volume or file or dir, as you remove the container it will be lost. This is suggested only for temporary pupose.
Approach #2 - Volume Mounting
Mouting the volume from the host to container
Step1: Create the volume with the custom path
docker volume create --name my_test_volume --opt type=none --opt device=/home/jinna/Jinna_Balu/Test_volume --opt o=bind
Step2 : Mount to the container or swarm service
docker run -d \
--name devtest \
--mount source=my_test_volume,target=/app \
nginx:1.11.8-alpine
We can do both of the above steps with below .yaml files
version: '3'
services:
nginx:
image: nginx:1.11.8-alpine
ports:
- "8081:80"
volumes:
- my_test_volume:/usr/share/app
volumes:
my_test_volume:
driver: local
driver_opts:
o: bind
type: none
device: /home/jinna/Jinna_Balu/Test_volume
RUN the above yml with docker-compose
docker-compose up -d
NOTE: create the folder path before you do docker-compose.
Good practice to have files mouted to maintain the persistency.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With