简体   繁体   中英

How to set/get the project name in docker-compose.yml

I'm trying to either set a docker-compose project name, or to be able to reference it within my config file. Is any of these possible?

The reason I'm asking is that I'm following this tutorial so I ended up with a docker-container.yml file that reads

web:
  build: .
  command: bundle exec rails s -p 3000 -b 0.0.0.0
  ports:
    - "3000:3000"
  links:
    - db
  volumes: 
    - ".:/myapp"
  volumes_from:
    - bundle

db:
  image: mongo
  ports:
    - "27017"

bundle:
  image: myapp_web  # <--- this is actually {$project_name}_web being hard-coded
  command: echo Hello
  volumes:
    - /bundle

Due to the image: myapp_web config, this will only work if the project name itself is myapp . This will be true if I'm working on a directory named myapp or if I use docker-compose -p myapp .

But as soon as someone changes the directory name, this will crash. I'd like to tell docker-compose to use a custom COMPOSE_PROJECT_NAME variable without having to pass the -p fancyname option.

Another way I could imagine would be to pass some kind of image: {$COMPOSE_PROJECT_NAME }_web in the yml file, but I'm really not sure this can be achieved.

Any idea?

在 Github 上有一个有关Docker compose 的相关问题,您可能想查看一下。

This is just an answer based on my initial comment.

When building multiple services off of the same image it is usually safe to just build it again. In this case, bundle can just build . . Since you are just building web again, it will be cached (thus building quickly) and have the same image ID as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM