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"
andsudo 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:
.env
and let docker-compose
source the variable from that. sudo -s
and run both commands as root from there. 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.