31

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.

Jinna Balu
  • 6,747
  • 38
  • 47
loonyuni
  • 1,373
  • 3
  • 16
  • 24

3 Answers3

61

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.

Jinna Balu
  • 6,747
  • 38
  • 47
  • 1
    This is a great answer. Thanks for providing me with multiple ways and explaining the reasoning of the correct way, as well as introducing me to docker-compose! – loonyuni Apr 23 '18 at 16:06
  • 1
    Could you add where those paths (jinna) are and how they relate to the location of docker-compose? When you run docker-compose then does it look at the path of `device:` on the same machine? – problemofficer - n.f. Monica Dec 10 '19 at 12:47
  • It looks for path within the machine. As we are not using the docker volume driver. According to the above example you have to havethe path `/home/jinna/Jinna_Balu/Test_volume` of the directory created before you run the container. – Jinna Balu Dec 11 '19 at 04:26
  • 1
    Hi Jinna, can you link to where the documentation for this is? I've been searching the Docker documentation for how to do this in Docker Compose in the top-level volumes key, and I haven't been able to find anything. I see plenty of examples in the documentation for how to set up volume mounts in a service definition, but all top-level volumes key example are just `volumes:someNamedVolume:` – Ungeheuer Jun 02 '21 at 05:12
  • Can you share your requirement. There are lot of docs available with volumes but you may not be finding the right documentation. – Jinna Balu Jun 02 '21 at 16:38
  • In option 2 above, does the mount replace the contents of the target? If the target is /usr/data and it has folder1, folder2 will those directories remain intact as long as the source does not include the same folder names? – zakariah1 Sep 20 '21 at 13:26
  • 1
    @zakariah1 host volume data will be persisted, whatever you keep in the host volume will be available in container. – Jinna Balu Sep 29 '21 at 16:19
  • 1
    Bless you! +1 for providing extra information about multiple approaches. – ilter Apr 13 '22 at 23:30
  • 1
    @Ungeheuer These are documented under [Bind mounts](https://docs.docker.com/storage/bind-mounts/). They are very performant, but prevent Docker from fully managing the directory’s contents. The pros and cons are listed at the top of the doc, the Docker Compose syntax at the end. – Funk Jun 18 '23 at 18:25
0

create a volume.

$ docker volume ls | grep my-volume
$ docker run -it --rm -d --name tmp -v my-volume:/mnt alpine
$ docker volume ls | grep my-volume
local               my-volume
$ docker exec tmp ls -l /mnt
total 0
$ 

Copy a file to a container.

$ docker cp . tmp:/mnt
$ docker exec tmp ls -l /mnt
total 4
-rw-r--r--    1 501      50              93 Apr 20 22:53 Dockerfile
$ ls
Dockerfile
$ 

clean up.

$ docker rm -f tmp
tmp
$ 

check.

$ docker run --rm -v my-volume:/mnt alpine ls /mnt
Dockerfile
$
Saito Kazuo
  • 137
  • 3
-1

To bind volumes between container and host machine. The most easy way is to use volume mounting. With which we can setup your container before starting it, (its images, ports, ..etc)

To build the container from an image: docker-compose build To start the container : docker-compose up

Volumes key in the docker-compose file is used to set the binding. which means when we update the content of volumes in the container it will persist even after stopping the container.

We can bind volumes from a local dir or from a named docker volume see the exmple below

We can use COPY or ADD in your Dockerfile to make the content of your local diroctory available in the docker container.

read docs here

version: "3.2"
services:
  web:
    image: nginx:alpine
    ports:
      - "80:80"
    volumes:
      - type: volume
        source: mydata
        target: /data
        volume:
          nocopy: true
      - type: bind
        source: ./static
        target: /opt/app/static

    networks:
      webnet:

    volumes:
Jinna Balu
  • 6,747
  • 38
  • 47
Cheikh HAIBALA
  • 125
  • 1
  • 11