docker from指令的含义_volume_from指令-docker撰写

With the below docker-compose.yml file:

test:

build: ../../

dockerfile: docker/dev/Dockerfile

volumes_from:

- cachev

cachev:

build: ../../

dockerfile: docker/dev/Dockerfile

volumes:

- /build

entrypoint: "true"

cachev service in above file launches volume container that creates anonymous volume in /var/lib/docker/ folder in docker host and creates mount point /cache within volume container(xx_cachev).

Does volumes_from instruction under test service create /build mount point in xx_test container? that points to /build mount point of xx_cachev container?

解决方案

From volumes_from docs:

Mount all of the volumes from another service or container...

So the short answer is yes:

volumes_from mounts /build volume defined by cachev service inside test service.

Long answer:

To answer your question let's run the test service:

docker compose up test

Before answering your question, let's make sure the description is clear:

cachev service in above file launches volume container...

It's just regular container which exits immediately because of entrypoint: "true".

docker ps -a should show:

ac68a33abe59 cache "true" 16 hours ago Exited (0) 4 minutes ago cache_1

But before it exits it creates volumes specified in volumes:. So we can call it volume container if its volumes are used by other service, for caching for instance.

that creates anonymous volume in /var/lib/docker/ folder in docker host

Agree. - /build is anonymous volume. Can be verified by viewing all container mounts:

docker inspect [cachev_container_id] --format '{{json .Mounts}}' | jq

should show something like:

{

"Type": "volume",

"Name": "1ec7ff7c72bfb5a3259ed54be5b156ea694be6c8d932bcb3fa6e657cbcaea378",

"Source": "/var/lib/docker/volumes/1ec7ff7c72bfb5a3259ed54be5b156ea694be6c8d932bcb3fa6e657cbcaea378/_data",

"Destination": "/build",

"Driver": "local",

"Mode": "",

"RW": true,

"Propagation": ""

}

jq is great utility for working with jsons in bash. Install it for the above command to work.

and creates mount point /cache within volume container(xx_cachev).

Don't see any evidence of mounts in cachev: service spec you provided.

If you add mapping - /tmp/cache:/cache to its volumes section and run docker compose up test again and inspect the exited container you should see:

{

"Type": "bind",

"Source": "/tmp/cache",

"Destination": "/cache",

"Mode": "rw",

"RW": true,

"Propagation": "rprivate"

}

Please, note that docker inspect [cachev_service_id] --format '{{json .Mounts}}' | jq will show all container mounts including those specified in docker/dev/Dockerfile using VOLUME instruction.

To answer to your question we need to inspect test service container:

docker inspect [test_container_id] --format '{{json .Mounts}}' | jq:

would show all the volumes specified in docker/dev/Dockerfile if any and all the volumes of cachev thanks to volumes_from instruction.

You can see that both test and cache containers have:

{

"Type": "volume",

"Name": "1ec7ff7c72bfb5a3259ed54be5b156ea694be6c8d932bcb3fa6e657cbcaea378",

"Source": "/var/lib/docker/volumes/1ec7ff7c72bfb5a3259ed54be5b156ea694be6c8d932bcb3fa6e657cbcaea378/_data",

"Destination": "/build",

"Driver": "local",

"Mode": "",

"RW": true,

"Propagation": ""

}

in their mounts and this volume survives subsequent runs of docker compose up test

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
docker-compose 搭建的lamp+redis 代码 附上docker-compose # 标准配置文件应该包含 version、services、networks 三大部分, # 其中最关键的就是 services 和 networks 两个部分,下面先来看 services 的书写规则 # 指定版本号 version: '2' services: # 在 services 标签下的第二级标签是 console,这个名字是用户自己自定义,它就是服务名称。 console: # 为自定义的容器指定一个名称,而不是使用默认的名称 container_name: console # 容器内置名称 hostname: console # 指明路径 build: # context 指定绝对路径或者相对路径 context: ./images/console # dockerfile 指定 Dockerfile 的文件名称 dockerfile: Dockerfile # volumes_from 从其它容器或者服务挂载数据卷, # 可选的参数是 :ro 或 :rw,前者表示容器只读,后者表示容器对数据卷是可读可写的(默认情况为可读可写的)。 volumes_from: - php # 挂载一个目录或者一个已存在的数据卷容器,可以直接使用 HOST:CONTAINER 这样的格式 # ,或者使用 HOST:CONTAINER:ro 这样的格式,后者对于容器来说,数据卷是只读的,这样可以有效保护宿主机的文件系统 volumes: # 使用绝对路径挂载数据卷 - /root/.ssh/:/root/.ssh/ # 类似于使用 docker run 的效果 我也不知道 不写因为console并不是直接启动导致镜像不会产生 tty: true # web,这个名字是用户自己自定义,它就是服务名称。 web: # 为自定义的容器指定一个名称,而不是使用默认的名称 container_name: web # 容器内置名称 hostname: web # 指明路径 build: # context 指定绝对路径或者相对路径 context: ./images/nginx # dockerfile 指定 Dockerfile 的文件名称 dockerfile: Dockerfile # 映射端口 ports: - '80:80' # 此选项解决了启动顺序的问题 这个的意思是必须在php启动以后才能启动 # 注意的是,默认情况下使用 docker-compose up web 这样的方式启动 web 服务时, # 也会启动 php 服务,因为在配置文件中定义了依赖关系 depends_on: - php # volumes_from 从其它容器或者服务挂载数据卷, volumes_from: - php volumes: # 已经存在的命名的数据卷 - nginx-log:/var/log/nginx # 以 Compose 配置文件为中心的相对路径作为数据卷挂载到容器 - ./images/nginx/sites-enabled:/etc/nginx/sites-enabled - ./images/nginx/cert:/etc/nginx/cert # 加入指定网络 networks: default: # 同一网络上的其他容器可以使用服务器名称或别名来连接到其他服务的容器 aliases: - web.sunchanghao.top - mid.sunchanghao.top - sevice.sunchanghao.top - admin.sunchanghao.top # php,这个名字是用户自己自定义,它就是服务名称。 php: # 为自定义的容器指定一个名称,而不是使用默认的名称 container_name: php # 容器内置名称 hostname: php # 服务除了可以基于指定的镜像,还可以基于一份 Dockerfile, # 在使用 up 启动之时执行构建任务,这个构建标签就是 build,它可以指定 Dockerfile # 所在文件夹的路径。Compose 将会利用它自动构建这个镜像,然后使用这个镜像启动服务容器 build: # context 选项可以是 Dockerfile 的文件路径,也可以是到链接到 git 仓库的 url # 当提供的值是相对路径时,它被解析为相对于撰写文件的路径,此目录也是发送到 Docker 守护进程的 context context: ./images/php # 使用此 dockerfile 文件来构建,必须指定构建路径 dockerfile: Dockerfile # 挂载一个目录或者一个已存在的数据卷容器, volumes: # 以 Compose 配置文件为中心的相对路径作为数据卷挂载到容器。 - ./app:/mnt/app # db,这个名字是用户自己自定义,它就是服务名称。 db: # 为自定义的容器指定一个名称,而不是使用默认的名称 container_name: db # 容器内置名称 hostname: db # 从指定的镜像中启动容器,可以是存储仓库、标签以及镜像 ID image: mysql:5.7 environment: MYSQL_USER: 'sch' MYSQL_PASS: '1111' MYSQL_ROOT_PASSWORD: 'root' volumes: - db:/var/lib/mysql ports: - '3306:3306' redis: # 为自定义的容器指定一个名称,而不是使用默认的名称 container_name: redis # 容器内置名称 hostname: redis # image 则是指定服务的镜像名称或镜像 ID。如果镜像在本地不存在,Compose 将会尝试拉取这个镜像。 image: redis:3.2.7 # 设置端口号 ports: - '6379:6379' # 挂载一个目录或者一个已存在的数据卷容器 volumes: # 已经存在的命名的数据卷。 - redis:/data # node volumes: nginx-log: # 设置volume的驱动,默认是local. driver: local db: driver: local redis: driver: local

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值