简体   繁体   English

读取容器的特定 Docker env_file

[英]Reading a specific Docker env_file for a container

I have a project with a .env file at the root of a Maven project that contains project related information.我在包含项目相关信息的 Maven 项目的根目录下有一个带有.env文件的项目。 I want to have a separate env file called .creds.env in a module directory in the project so that I can add it to .gitignore .我想在项目的模块目录中有一个名为.creds.env的单独env文件,以便我可以将它添加到.gitignore The .creds.env file contains a user name and password in the following format: .creds.env文件包含以下格式的用户名和密码:

USER_NAME=user
USER_PASSWORD=password

In docker-compose.yml , I have the following to read the .creds.env file for the service:docker-compose.yml ,我有以下内容来读取服务的.creds.env文件:

  myservice:
    env_file:
      - ./module/.creds.env
    container_name: myservice
    build:
      context: ./
      dockerfile: ./module/Dockerfile
      args:
        USER_NAME: ${USER_NAME}
        USER_PASSWORD: ${USER_PASSWORD}

But the username and password are only being read from the root .env file and not from the module .creds.env但是用户名和密码只能从根.env文件中读取,而不是从模块.creds.env

The project structure is like below:项目结构如下:

|__ .env
|__ docker-compose.yml
|__ module
     |__ Dockerfile
     |__ .creds.env

I need help figuring out how to read the .creds.env file for the myservice container and continue to read the .env file for everything else.我需要帮助搞清楚如何阅读.creds.env该文件myservice容器,并继续读.env的一切文件。

You should remove USER_NAME and USER_PASSWORD in the root .env file.您应该删除根.env文件中的USER_NAMEUSER_PASSWORD

Sounds like you can assume that your environments will always include the .creds.env file.听起来您可以假设您的环境将始终包含.creds.env文件。

If you need a default value that you currently reference in .env , you may be able to use shell parameter substitution in your compose file:如果您需要当前在.env引用的默认值,您可以在撰写文件中使用 shell 参数替换:

  myservice:
    env_file:
      - ./module/.creds.env
    container_name: myservice
    build:
      context: ./
      dockerfile: ./module/Dockerfile
      args:
        USER_NAME: ${USER_NAME:-user}
        USER_PASSWORD: ${USER_PASSWORD:-mypass}

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

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