简体   繁体   中英

Variable substitution in docker-compose .yml file

I was trying to use variable substitution for a volume declaration. This led to the fallowing error:

ERROR: for php  Cannot create container for service php: create .: volume name is too short, names should be at least two alphanumeric characters

Docker's Compose file:

services:        
  php:
      image: php5-apache-composer
      volumes:
        - ${DIR}:/var/www/html

Is variable substitution supported for the volume declaration? I can use environment variables for any other declaration except this one.

You need to use .env file in folder where docker-compose.yaml is in order to declaring default environment variables for docker-compose.yaml file.

Just create .env file with the following content:

DIR=/var/www/html

Of course you can do that dinamically on each build like:

echo "DIR=/var/www/html" > .env && docker-compose up

I've run export DIR="/var/www/html" and sudo docker-compose run php -e DIR="/var/www/html"

The sudo creates a different environment for the command it's running, the DIR variable doesn't exist there. You can check that with a simple env command:

$ export USER_VAR=test
$ sudo env | grep USER_VAR
$

To get this to work, you'll need to either:

  1. Create a .env and let docker-compose source the variable from that.
  2. Run a shell with sudo -s and run both commands as root from there.
  3. Run a shell as the sudo command, eg: sudo /bin/sh -c "DIR=/var/www/html docker-compose run php -e DIR=/var/www/html"

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