docker-compose v3 将文件夹名称添加到网络名称 [英] docker-compose v3 prepend folder name to network name
问题描述
当我运行 docker-compose up -d
时,docker 总是创建容器名称和网络名称,前面带有包含 docker-compose.yml
文件的文件夹名称.
When I run docker-compose up -d
, docker always creates container name and network name prepended with the folder name that contains docker-compose.yml
file.
我可以指定容器名称如下:
I can specify the container name as follows:
nginx:
container_name: nginx
build:
context: .
dockerfile: .docker/docker-nginx.dockerfile
但是我如何指定网络名称以使其不预先添加文件夹名称?
But how can I specify the network name so that it doesn't prepend folder name to it?
谢谢.
推荐答案
Docker 将使用 docker compose 文件创建的所有组件名称放在当前文件夹名称前面.
Docker Prepends the current folder name with all the components name created using docker compose file.
例如:如果包含 docker-compose.yml 文件的当前文件夹名称是 test,则所有卷、网络和容器名称都将附加到 test .为了解决这个问题,人们早先提出了在 docker-compose 命令中使用 -p 标志的想法,但该解决方案并不是最可行的解决方案,因为在 -p 属性之后需要一个项目名称.然后将项目名称附加到使用 docker compose 文件创建的所有组件中.
Eg : If the current folder name containing the docker-compose.yml file is test, all the volumes,network and container names will get test appended to it. In order to solve the problem people earlier proposed the idea of using -p flag with docker-compose command but the solution is not the most feasible one as a project name is required just after the -p attribute. The project name then gets appended to all the components created using docker compose file.
上述问题的解决方案是使用 name 属性,如下所示.
The Solution to the above problem is using the name property as in below.
volumes:
data:
driver: local
name: mongodata
networks:
internal-network:
driver: bridge
name: frontend-network
这个卷可以在服务部分称为
This volume can be referred in the service section as
services:
mongo-database:
volumes:
- data:/data/db
networks:
- internal-network
上面的 name 属性将阻止 docker-compose 预先添加文件夹名称.
The above name attribute will prevent docker-compose to prepend folder name.
注意:对于容器名称,可以使用属性container_name
Note : For the container name one could use the property container_name
services:
mongo-database:
container_name: mongo
这篇关于docker-compose v3 将文件夹名称添加到网络名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!