欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Docker学习(五) Docker Compose Volumes

程序员文章站 2022-05-11 14:23:34
...

volumes的作用有点类似于VMware里面的共享目录,用于将物理主机里的目录映射到docker虚拟机里。

通常用法

version: '2'

networks:
  thinking:
  
services:
  test_1.thinking.com:
    image: ubuntu
    ports:
      - "3333:3333"
    container_name: test_1.thinking.com
    command: /bin/bash
    volumes:
      - $PWD/:/test_pwd
    tty: true
    networks:
      - thinking

  test_2.thinking.com:
    image: ubuntu
    ports:
      - "4444:4444"
    container_name: test_2.thinking.com
    command: /bin/bash
    tty: true
    networks:
      - thinking

其中

    volumes:

      - $PWD/:/test_pwd

表示将物理机的当前目录映射到docker虚拟机/test_pwd里面。

执行

$ docker-compose -f test_voloumes.yaml up -d
Creating network "desktop_thinking" with the default driver
Creating test_1.thinking.com ... done

Creating test_2.thinking.com ... done

$ docker exec -it test_1.thinking.com /bin/bash
[email protected]:/# ls
bin   dev  home  lib64  mnt  proc  run   srv  test_pwd  usr

boot  etc  lib   media  opt  root  sbin  sys  tmp       var

[email protected]:/# ls /test_pwd/

docker-compose-test.yml  docker-compose.yml  test  test_voloumes.yaml

可以看到映射成功了。

特殊用法

在研究fabric项目的时候还发现有另外一种用法

version: '2'

volumes:
  test_1.thinking.com:
  test_2.thinking.com:

networks:
  thinking:
  
services:
  test_1.thinking.com:
    image: ubuntu
    ports:
      - "3333:3333"
    container_name: test_1.thinking.com
    command: /bin/bash
    volumes:
      - test_1.thinking.com:/test
      - $PWD/:/test_pwd
    tty: true
    networks:
      - thinking

  test_2.thinking.com:
    image: ubuntu
    ports:
      - "4444:4444"
    container_name: test_2.thinking.com
    command: /bin/bash
    tty: true
    networks:
      - thinking

试一下

$ docker-compose -f test_voloumes.yaml up -d
Creating volume "desktop_test_1.thinking.com" with default driver
Creating volume "desktop_test_2.thinking.com" with default driver
Recreating test_1.thinking.com ... 

Recreating test_1.thinking.com ... done

$ docker volume ls
DRIVER              VOLUME NAME
local               desktop_test_1.thinking.com

local               desktop_test_2.thinking.com

$ docker volume inspect desktop_test_1.thinking.com
[
    {
        "CreatedAt": "2018-06-06T02:37:41-07:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/desktop_test_1.thinking.com/_data",
        "Name": "desktop_test_1.thinking.com",
        "Options": null,
        "Scope": "local"
    }

]

这看起来意思是volume test_1.thinking.com对应的物理机是/var/lib/docker/volumes/desktop_test_1.thinking.com/_data,映射到docker虚拟机里面是/test,检验一下

$ docker exec -it test_1.thinking.com /bin/bash
[email protected]:/# ls
bin   dev  home  lib64  mnt  proc  run   srv  test      tmp  var
boot  etc  lib   media  opt  root  sbin  sys  test_pwd  usr

[email protected]:/# mkdir -p /test/fuck

到物理机里面去看

$ ls /var/lib/docker/volumes/desktop_test_1.thinking.com/_data

ls: cannot access '/var/lib/docker/volumes/desktop_test_1.thinking.com/_data': Permission denied

$ sudo ls /var/lib/docker/volumes/desktop_test_1.thinking.com/_data

fuck

果然如此。