docker-compose v3在文件夹名称之前添加网络名称 [英] docker-compose v3 prepend folder name to network name

查看:136
本文介绍了docker-compose v3在文件夹名称之前添加网络名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行 docker-compose up -d 时,docker总是创建容器名称和网络名称,并以包含 docker-compose.yml file的文件夹名称为前缀./p>

我可以如下指定容器名称:

  nginx:container_name:nginx建造:语境: .dockerfile:.docker/docker-nginx.dockerfile 

但是我该如何指定网络名称,以使其不会在文件夹名称前添加网络名称?

谢谢.

解决方案

Docker在当前文件夹名称前添加使用docker compose文件创建的所有组件名称.

例如:如果包含docker-compose.yml文件的当前文件夹名称为test,则所有卷,网络和容器名称都将附加test.为了解决该问题,人们早先提出了在docker-compose命令中使用 -p 标志的想法,但是该解决方案并不是最可行的解决方案,因为仅在-p属性之后需要一个项目名称.然后,将项目名称附加到使用docker compose文件创建的所有组件中.

上述问题的解决方案使用的是 name 属性,如下所示.

 卷:数据:司机:本地名称:mongodata网络:内部网络:司机:桥名称:前端网络 

该卷可以在服务部分中称为

 服务:mongo数据库:数量:-数据:/data/db网络:- 内部网络 

上面的 name 属性将阻止docker-compose放置文件夹名称.

注意:对于容器名称,可以使用属性 container_name

 服务:mongo数据库:container_name:mongo 

When I run docker-compose up -d, docker always creates container name and network name prepended with the folder name that contains docker-compose.ymlfile.

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?

Thanks.

解决方案

Docker Prepends the current folder name with all the components name created using docker compose file.

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.

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

The above name attribute will prevent docker-compose to prepend folder name.

Note : For the container name one could use the property container_name

services:
  mongo-database:
    container_name: mongo

这篇关于docker-compose v3在文件夹名称之前添加网络名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆