Is it possible to define local variable in docker-compose.yml?
Something like this for example:
version: '3'
services:
app:
build:
context: .
dockerfile: Dockerfile
image: $APP_NAME
container_name: $APP_NAME
Can I define the value of $APP_NAME
on this same docker-compose.yml
file?
In Docker Compose, IT admins can use environment variables to generalize configurations for different situations, deployment environments and security contexts without editing the main project file(s) manually. Variables can either be passed as command-line arguments -- suitable for only a few parameters -- or via a .
Use -e or --env value to set environment variables (default []). If you want to use multiple environments from the command line then before every environment variable use the -e flag. Note: Make sure put the container name after the environment variable, not before that.
When we launch our Docker container, we can pass environment variables as key-value pairs directly into the command line using the parameter –env (or its short form -e). As can be seen, the Docker container correctly interprets the variable VARIABLE1.
Starting from docker-compose file version 3.4, you can define a top level x-
prefixed section in the compose file. It will be parsed as valid yml but ignored by compose when creating services, network, volumes, etc...
version: '3.4'
x-var: &APP_NAME
myimage
services:
app:
build:
context: .
dockerfile: Dockerfile
image: *APP_NAME
container_name: *APP_NAME
You can even use this feature to define "object", like:
x-common: &common
image: ${REGISTRY_URL}img:latest
services:
master:
<<: *common
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