简体   繁体   English

Docker-compose:了解链接环境变量

[英]Docker-compose : understanding linking environment variables

I'm now using docker-compose for all of my projects.我现在正在为我的所有项目使用docker-compose Very convenient.很方便。 Much more comfortable than manual linking through several docker commands.比通过多个docker命令手动链接要舒服得多。

There is something that is not clear to me yet though: the logic behind the linking environment variables.不过,我还不清楚一些事情:链接环境变量背后的逻辑。

Eg.例如。 with this docker-compose.yml :使用这个docker-compose.yml

mongodb:
  image: mongo
  command: "--smallfiles --logpath=/dev/null"
web:
  build: .
  command: npm start
  volumes:
    - .:/myapp
  ports:
    - "3001:3000"
  links:
    - mongodb
  environment:
    PORT: 3000
    NODE_ENV: 'development'

In the node app, I need to retrieve the mongodb url.在节点应用程序中,我需要检索 mongodb url。 And if I console.log(process.env) , I get so many things that it feels very random (just kept the docker-compose -related ones):如果我使用console.log(process.env) ,我会得到很多感觉非常随机的东西(只保留与docker-compose相关的东西):

MONGODB_PORT_27017_TCP: 'tcp://172.17.0.2:27017',
MYAPP_MONGODB_1_PORT_27017_TCP_PORT: '27017',
MYAPP_MONGODB_1_PORT_27017_TCP_PROTO: 'tcp',
MONGODB_ENV_MONGO_VERSION: '3.2.6',
MONGODB_1_ENV_GOSU_VERSION: '1.7',
'MYAPP_MONGODB_1_ENV_affinity:container': '=d5c9ebd7766dc954c412accec5ae334bfbe836c0ad0f430929c28d4cda1bcc0e',
MYAPP_MONGODB_1_ENV_GPG_KEYS: 'DFFA3DCF326E302C4787673A01C4E7FAAAB2461C \t42F3E95A2C4F08279C4960ADD68FA50FEA312927',
MYAPP_MONGODB_1_PORT_27017_TCP: 'tcp://172.17.0.2:27017',
MONGODB_1_PORT: 'tcp://172.17.0.2:27017',
MYAPP_MONGODB_1_ENV_MONGO_VERSION: '3.2.6',
MONGODB_1_ENV_MONGO_MAJOR: '3.2',
MONGODB_ENV_GOSU_VERSION: '1.7',
MONGODB_1_PORT_27017_TCP_ADDR: '172.17.0.2',
MONGODB_1_NAME: '/myapp_web_1/mongodb_1',
MONGODB_1_PORT_27017_TCP_PORT: '27017',
MONGODB_1_PORT_27017_TCP_PROTO: 'tcp',
'MONGODB_1_ENV_affinity:container': '=d5c9ebd7766dc954c412accec5ae334bfbe836c0ad0f430929c28d4cda1bcc0e',
MONGODB_PORT: 'tcp://172.17.0.2:27017',
MONGODB_1_ENV_GPG_KEYS: 'DFFA3DCF326E302C4787673A01C4E7FAAAB2461C \t42F3E95A2C4F08279C4960ADD68FA50FEA312927',
MYAPP_MONGODB_1_ENV_GOSU_VERSION: '1.7',
MONGODB_ENV_MONGO_MAJOR: '3.2',
MONGODB_PORT_27017_TCP_ADDR: '172.17.0.2',
MONGODB_NAME: '/myapp_web_1/mongodb',
MONGODB_1_PORT_27017_TCP: 'tcp://172.17.0.2:27017',
MONGODB_PORT_27017_TCP_PORT: '27017',
MONGODB_1_ENV_MONGO_VERSION: '3.2.6',
MONGODB_PORT_27017_TCP_PROTO: 'tcp',
MYAPP_MONGODB_1_PORT: 'tcp://172.17.0.2:27017',
'MONGODB_ENV_affinity:container': '=d5c9ebd7766dc954c412accec5ae334bfbe836c0ad0f430929c28d4cda1bcc0e',
MYAPP_MONGODB_1_ENV_MONGO_MAJOR: '3.2',
MONGODB_ENV_GPG_KEYS: 'DFFA3DCF326E302C4787673A01C4E7FAAAB2461C \t42F3E95A2C4F08279C4960ADD68FA50FEA312927',
MYAPP_MONGODB_1_PORT_27017_TCP_ADDR: '172.17.0.2',
MYAPP_MONGODB_1_NAME: '/myapp_web_1/novatube_mongodb_1',

Don't know what to pick, and why so many entries?不知道选什么,为什么这么多条目? Is it better to use the general ones, or the MYAPP -prefixed one?使用通用的还是MYAPP前缀的更好? Where does the MYAPP name comes from? MYAPP名字从何而来? Folder name?文件夹名称?

Could someone clarify this?有人可以澄清这一点吗?

Wouldn't it be easier to let the user define the ones he needs in the docker-compose.yml file with a custom mapping?让用户使用自定义映射在docker-compose.yml文件中定义他需要的那些不是更容易吗? Like:喜欢:

  links:
    - mongodb:
      - MONGOIP: IP
      - MONGOPORT : PORT

What I'm saying might not have sense though.虽然我说的可能没有意义。 :-) :-)

Environment variables are a legacy way of defining links between containers.环境变量是定义容器之间链接的传统方式。 If you are using a newer version of compose, you don't need the links declaration at all.如果您使用的是较新版本的 compose,则根本不需要links声明。 Trying to connect to mongodb from your app container will work fine by just using the name of the service ( mongodb ) as a hostname, without any links defined in the compose file (instead using docker's builtin DNS resolution, check /etc/hosts, nothing in there either!)尝试从您的应用程序容器连接到mongodb只需使用服务名称 ( mongodb ) 作为主机名即可正常工作,而无需在撰写文件中定义任何链接(而是使用 docker 的内置 DNS 解析,检查 /etc/hosts,什么都没有)在那里!)

In answer to your question about why the prefix with MYAPP , you're right.在回答您关于为什么前缀为MYAPP ,您是对的。 Compose prefixes the service name with the name of the folder (or 'project', in compose nomenclature). Compose 使用文件夹名称(或“项目”,在组合命名法中)作为服务名称的前缀。 It does the same thing when creating custom networks and volumes.在创建自定义网络和卷时,它会做同样的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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