简体   繁体   中英

Docker use variables in conf file?

If I'm using Docker with nginx for hosting a web app, how can I use either

  1. Variables in my docker-compose.yml file
  2. Environment variables such as HOSTNAME=example.com .

So that when I build the container, it will insert the value into my nginx.conf file that I copy over when I build the container.

You can use environment variables is your compose file. According to official docs

Your configuration options can contain environment variables. Compose uses the variable values from the shell environment in which docker-compose is run. For example, suppose the shell contains POSTGRES_VERSION=9.3 and you supply this configuration:

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

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.

See the docs for more information. You will find various other approaches to supply environment variables in the link like passing them through env_file etc.

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