gpt4 book ai didi

How can I use a volume in context of build directive of docker-compose?(如何在docker-compose的构建指令上下文中使用卷?)

转载 作者:bug小助手 更新时间:2023-10-25 14:49:16 26 4
gpt4 key购买 nike



I define a volume in docker-compose as follows

我在docker中定义了一个卷-编写如下


volumes:
root_volume:
driver: local,
driver_opts:
device:"/home/user/test",
type:none

I need to do something like this

我需要做这样的事情


  nginx:
build:
context: root_volume
dockerfile: nginx.Dockerfile
container_name: test_nginx
restart: unless-stopped
tty: true
ports:
- "8015:80"
networks:
- test_network



But it doesn't work.

但这并不管用。


更多回答

What are you hoping that volume declaration will do? Volumes aren't considered or used at all as part of the image build, and they also aren't used when running a container if you don't include a separate volumes: block as part of the service definition. It's possible a best-practice approach would be to delete this volume definition entirely.

你希望这个卷声明能起到什么作用?卷根本不会作为映像构建的一部分进行考虑或使用,如果不将单独的卷:块作为服务定义的一部分包括在内,则在运行容器时也不会使用它们。最佳实践方法可能是完全删除该卷定义。

How to mount host volumes into docker containers in Dockerfile during build may answer your larger question (TL;DR: you can't, except in the context of mounting read-only content into individual RUN instructions).

如何在构建期间将主机卷挂载到Dockerfile中的停靠容器中可能会回答您更大的问题(TL;DR:您不能,除非在将只读内容挂载到单个运行指令的上下文中)。

@DavidMaze As you know I have merged many docker-compose files. The relative path in build loads the first path defined in .env. I was going to define the absolute path in volume and use that volume in different directives of docker-compose file, including build directive.

@DavidMaze如你所知,我已经合并了许多由docker组成的文件。Build中的相对路径加载.env中定义的第一个路径。我打算在卷中定义绝对路径,并在docker合成文件的不同指令中使用该卷,包括构建指令。

优秀答案推荐

The issue is that volumes defined at the top level can't be directly used as build contexts. Instead, define the volume as normal at the top level.
Then in the service, mount that volume at a specific path in the container using the volumes option.
The build context should be . (the current directory) and reference the Dockerfile as normal.

问题是在顶级定义的卷不能直接用作构建上下文。相反,应在顶层将音量定义为正常音量。然后在服务中,使用卷选项将该卷装入容器中的特定路径。构建上下文应该是。(当前目录),并照常引用Dockerfile。


Example:

示例:


volumes:
root_volume:
driver: local
driver_opts:
type: none
device: /home/user/test
o: bind

services:

nginx:
build:
context: ./
dockerfile: nginx.Dockerfile
volumes:
- root_volume:/path/in/container
container_name: test_nginx
restart: unless-stopped
tty: true
ports:
- "8015:80"
networks:
- test_network

This will mount the host directory into the container, making it available at the specified path during the build.

这会将主机目录挂载到容器中,使其在构建期间在指定路径处可用。


更多回答

Volumes are never available during the build. The actual effect of this will cause the contents of the named volume (itself defined as a host directory here) to hide the image's contents, resulting in a container that might not have the filesystem contents you expect.

卷在构建期间永远不可用。这样做的实际效果将导致命名卷(其本身在这里定义为主机目录)的内容隐藏映像的内容,从而导致容器可能没有您期望的文件系统内容。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com