How to use environment variables in docker compose

后端 未结 13 750
孤街浪徒
孤街浪徒 2020-11-28 01:02

I would like to be able to use env variables inside docker-compose.yml, with values passed in at the time of docker-compose up. This is the example.

I am

13条回答
  •  暖寄归人
    2020-11-28 01:14

    The DOCKER solution:

    It looks like docker-compose 1.5+ has enabled variables substitution: https://github.com/docker/compose/releases

    The latest Docker Compose allows you to access environment variables from your compose file. So you can source your environment variables, then run Compose like so:

    set -a
    source .my-env
    docker-compose up -d
    

    Then you can reference the variables in docker-compose.yml using ${VARIABLE}, like so:

    db:
      image: "postgres:${POSTGRES_VERSION}"
    

    And here is more info from the docs, taken here: https://docs.docker.com/compose/compose-file/#variable-substitution

    When you run docker-compose up with this configuration, Compose looks for the POSTGRES_VERSION environment variable in the shell and substitutes its value in. For this example, Compose resolves the image to postgres:9.3 before running the configuration.

    If an environment variable is not set, Compose substitutes with an empty string. In the example above, if POSTGRES_VERSION is not set, the value for the image option is postgres:.

    Both $VARIABLE and ${VARIABLE} syntax are supported. Extended shell-style features, such as ${VARIABLE-default} and ${VARIABLE/foo/bar}, are not supported.

    If you need to put a literal dollar sign in a configuration value, use a double dollar sign ($$).

    And I believe this feature was added in this pull request: https://github.com/docker/compose/pull/1765

    The BASH solution:

    I notice folks have issues with Docker's environment variables support. Instead of dealing with environment variables in Docker, let's go back to basics, like bash! Here is a more flexible method using a bash script and a .env file.

    An example .env file:

    EXAMPLE_URL=http://example.com
    # Note that the variable below is commented out and will not be used:
    # EXAMPLE_URL=http://example2.com 
    SECRET_KEY=ABDFWEDFSADFWWEFSFSDFM
    
    # You can even define the compose file in an env variable like so:
    COMPOSE_CONFIG=my-compose-file.yml
    # You can define other compose files, and just comment them out
    # when not needed:
    # COMPOSE_CONFIG=another-compose-file.yml
    

    then run this bash script in the same directory, which should deploy everything properly:

    #!/bin/bash
    
    docker rm -f `docker ps -aq -f name=myproject_*`
    set -a
    source .env
    cat ${COMPOSE_CONFIG} | envsubst | docker-compose -f - -p "myproject" up -d
    

    Just reference your env variables in your compose file with the usual bash syntax (ie ${SECRET_KEY} to insert the SECRET_KEY from the .env file).

    Note the COMPOSE_CONFIG is defined in my .env file and used in my bash script, but you can easily just replace {$COMPOSE_CONFIG} with the my-compose-file.yml in the bash script.

    Also note that I labeled this deployment by naming all of my containers with the "myproject" prefix. You can use any name you want, but it helps identify your containers so you can easily reference them later. Assuming that your containers are stateless, as they should be, this script will quickly remove and redeploy your containers according to your .env file params and your compose YAML file.

    Update Since this answer seems pretty popular, I wrote a blog post that describes my Docker deployment workflow in more depth: http://lukeswart.net/2016/03/lets-deploy-part-1/ This might be helpful when you add more complexity to a deployment configuration, like nginx configs, LetsEncrypt certs, and linked containers.

提交回复
热议问题