I want to have a variable in a docker-compose.yml
for my project that represents a port and instead of putting 8080
everywhere in the file, I'd rather use it as a variable.
Is it possible to set variable, say in the beginning of the file, then reuse it in other places of the file?
You can use an external .env file and include it in the docker-compose file.
$ cat .env
port=8080
$ cat docker-compose.yml
version: '3'
services:
web:
port: "${port}"
docker-compose.yml and .env should be in the same directory.
Starting from version 3.4, you can re-use configuration parts using extension fields. Valid extension fields have two requirements:
Here's an example:
version: '3.4'
x-logging:
&default-logging
options:
max-size: '12m'
max-file: '5'
driver: json-file
services:
web:
image: myapp/web:latest
logging: *default-logging
db:
image: mysql:latest
logging: *default-logging
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With