Can we use template in Docker compose file YML?
For example, I want deploy service with replicated and I want set name for container like: -servicename-_-replicId-
Can we use template in Docker compose file YML?
For example, I want deploy service with replicated and I want set name for container like: -servicename-_-replicId-
Short answer: yes, and it's called interpolation or variable substitution in their context: https://docs.docker.com/compose/compose-file/#variable-substitution
A bit more details: You can interpolate variable values from environment variables, but can also provide defaults in case the environment doesn't contain the necessary variable.
An example taken from the official docs looks like this:
db:
image: "postgres:${POSTGRES_VERSION}"
Now regarding your actual use case to name a container: the container name stems from a variable key and not from a variable value. So you'll have to use the container_name
property to explicitly override the generated container name. See the example above: db
would be the generated container name, but db
isn't a property value. So to make your use case work, you should try this:
db:
container_name: "app_${CONTAINER_NAME_SUFFIX}"
There is a new project related to docker-compose templating problematics called octo-compose.
In this projects there is included: