Roberto Lo Giacco
Roberto Lo Giacco

Reputation: 740

Where are Go templates allowed in docker-compose files?

The title is self explanatory: I wasn't able to find a document describing where Go templates are allowed in compose files.

In other words I wish to know which of the following is supported:

version: "3.7"


services:
  whoami:
    image: "containous/whoami"
    environment:
      - VALUE='{{.Name}}'
      - NAME_{{.Name}}='NAME'
    deploy:
      labels:
        - value={{.Name}}
        - name_{{.Name}}=NAME
    networks:
      - {{.Name}}

Upvotes: 5

Views: 4318

Answers (4)

rokpoto.com
rokpoto.com

Reputation: 10778

I don't think what you need is possible, at least according to compose docs. Please note that docker-compose is written in python and it uses docker APIs.

If your need is defining some variables inside docker-compose.yaml file, then you can do that using ${SOME_VAR} syntax.

Var values may be stored in .env file which you can reference in your compose commands using docker compose --env-file [path_to_env_file] syntax

See sample repository using the above on GitHub

Upvotes: 0

Daniele Cruciani
Daniele Cruciani

Reputation: 623

I am curious about "where is documentation?" on this, but yes, it is almost working. docker-compose is another application that parse a yaml file to feed docker client interface to send command to docker daemon, or at least this is what I understood, because it looks like the golang templates pass through and are replaced.

Here is an example of use https://github.com/lovelysystems/rabbitmq-swarm-cluster/blob/master/docker-compose.yml

More on this: https://github.com/moby/moby/issues/37377

I think open a new case is a good idea, but who maintain docker.com docs?

Upvotes: 0

Roberto Lo Giacco
Roberto Lo Giacco

Reputation: 740

I kept searching and apparently the information is actually provided in the official Docker documentation, even if it doesn't explicitly refer to the docker compose file format.

In particular:

You can use templates for some flags of service create, using the syntax provided by the Go’s text/template package.

The supported flags are the following :

--hostname --mount --env

As --labels is not listed we should assume text/template replacement is not allowed in there.

Upvotes: 4

Nicholas Corin
Nicholas Corin

Reputation: 2404

Go templates are a part of the Go standard library. docker-compose is a separate application as part of docker.

1. Declare your template.

You can declare your template as a const within your Go code, or have it in a file.

const composeTemplate = `version: "3.7"

services:
  whoami:
    image: "containous/whoami"
    environment:
      - VALUE='{{.Name}}'
      - NAME_{{.Name}}='NAME'
    deploy:
      labels:
        - value={{.Name}}
        - name_{{.Name}}=NAME
    networks:
      - {{.Name}}
`

2. Config for your template.

Create a configuration type that will be populated and used to execute the template.

type Config struct {
    Name string
}

3. Execute your template with config.

conf := &Config{Name: "MyService"}

tpl, err := template.New("myservice").Parse(composeTemplate)
if err != nil {
    // Handle error.
}

var output bytes.Buffer
err = tpl.Execute(&output, conf)
if err != nil {
    // Handle error.
}

4. Write your output to a file.

err = ioutil.WriteFile("myservice-compose.yml", output.Bytes(), 0644)
if err != nil {
    // Handle error.
}

(Go Playground)

Good luck!

Upvotes: 1

Related Questions