How to create docker volume device/host path

前端 未结 3 1364
庸人自扰
庸人自扰 2020-12-31 23:12

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

相关标签:
3条回答
  • 2020-12-31 23:31

    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:
    
    0 讨论(0)
  • 2020-12-31 23:32

    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.

    0 讨论(0)
  • 2020-12-31 23:35

    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
    $
    
    0 讨论(0)
提交回复
热议问题