Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
696 views
in Technique[技术] by (71.8m points)

docker-compose v3 prepend folder name to network name

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...