How to pass environment variable to docker-compose up

后端 未结 3 1923
我寻月下人不归
我寻月下人不归 2020-12-03 06:11

I am trying to run a container. I already have the image uploaded to private Docker registry. I want to write a compose file to download and deploy the image. But I want to

相关标签:
3条回答
  • 2020-12-03 07:06

    You can create a .env file on the directory where you execute the docker-compose up command (and your docker-compose.yml file is located) with the following content:

    KB_DB_TAG_VERSION=kb-1.3.20-v1.0.0
    

    Your docker-compose.yml file should look like the following (added { and }):

    version: '3'
    services:
       db:
         user: "1000:50"
         volumes:
           - /data/mysql:/var/lib/mysql
         container_name: k-db
         environment:
           - MYSQL_ALLOW_EMPTY_PASSWORD=yes
         image: XX:${KB_DB_TAG_VERSION}
         image: k-db
         ports:
           - "3307:3306"
    

    After making the above changes , check whether the changes are reflected or not using the command docker-compose config. The variable will be replaced by the variable value. Please refer to the page here to understand more about variable replacement.

    0 讨论(0)
  • 2020-12-03 07:12

    You have two options:

    1. Create the .env file as already suggested in another answer.
    2. Prepend KEY=VALUE pair(s) to your docker-compose command, e.g:

      KB_DB_TAG_VERSION=kb-1.3.20-v1.0.0 docker-compose up
      

      Exporting it earlier in a script should also work, e.g.:

      export KB_DB_TAG_VERSION=kb-1.3.20-v1.0.0
      docker-compose up
      
    0 讨论(0)
  • 2020-12-03 07:14

    In your docker-compose.yml file add

    env_file:
      - .env_file
    

    to your db service where .env_file is your .env file (change its name accordingly).

    version: '3'
    services:
       db:
        #build: k-db
        user: "1000:50"
        volumes:
          - /data/mysql:/var/lib/mysql
        container_name: k-db
        env_file:
          - .env_file
        environment:
          - MYSQL_ALLOW_EMPTY_PASSWORD=yes
        image:  XX:$KB_DB_TAG_VERSION
        image: k-db
        ports:
          - "3307:3306"
    
    0 讨论(0)
提交回复
热议问题