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.
你希望这个卷声明能起到什么作用?卷根本不会作为映像构建的一部分进行考虑或使用,如果不将单独的卷:块作为服务定义的一部分包括在内,则在运行容器时也不会使用它们。最佳实践方法可能是完全删除该卷定义。
@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.
卷在构建期间永远不可用。这样做的实际效果将导致命名卷(其本身在这里定义为主机目录)的内容隐藏映像的内容,从而导致容器可能没有您期望的文件系统内容。
我是一名优秀的程序员,十分优秀!