1

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-

Lorna Mitchell
  • 1,819
  • 13
  • 22
Ivan Bukharin
  • 321
  • 2
  • 4
  • 22

2 Answers2

6

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}"
gesellix
  • 3,024
  • 28
  • 31
  • Yep, we can use variables from environment, it is useful. And exist way to use template syntax by Go programming language (https://golang.org/pkg/text/template/). Some information: (https://docs.docker.com/engine/reference/commandline/service_create/#create-services-using-templates) (https://stackoverflow.com/questions/17157721/how-to-get-a-docker-containers-ip-address-from-the-host?rq=1) – Ivan Bukharin Aug 26 '17 at 21:51
  • Ah, sorry, you didn't mention that you wanted to use Golang template syntax. Then my answer is: no, that's not possible as far as I understand the linked docs. – gesellix Aug 26 '17 at 21:54
  • Where we can see more information about this template format, hierarchical ( .Service.ID, .NetworkSettings.Networks, ...) or how find more information? – Ivan Bukharin Aug 26 '17 at 21:55
  • "no, that's not possible ...". @gesellix It is bad. How I can more information about docker + Golang template syntax ? – Ivan Bukharin Aug 26 '17 at 21:59
  • 1
    You can find the complete structure when you see the JSON output of the `docker inspect ...` or `docker service inspect ...` commands. These JSON structures are exactly those which the Golang templating will use. – gesellix Aug 26 '17 at 22:01
  • Variable substitution in Compose files has nothing to do with the `docker ... inspect ...` commands/result interpolation. – gesellix Aug 26 '17 at 22:02
  • I also don't think inspect results are the basis for golang templates in compose files. See my question: https://stackoverflow.com/questions/54251442/which-docker-template-values-are-available-in-docker-stack-deploy-compose – codesmith Jan 18 '19 at 10:10
-1

There is a new project related to docker-compose templating problematics called octo-compose.

In this projects there is included:

  • Templating by defining custom variables,
  • templating by using built-in variables like instance id, ports from range, ...,
  • running host preparation bash scripts,
  • recursive octo-composes inclusion from another repo or directory,
  • support for Docker Swarm deployments.