dbones
dbones

Reputation: 4504

setting a variable from a file

Is there a way to set a variable from a file in the docker compose?

The following does not work in compose (however it does if you attach to the container):

version: '2'
services:
  foo:
    image: busybox
    command:
      - /bin/sh
      - -c
      - |
          echo "hello" > id #this file will be supplied by a volume-from
          ls
          cat id
          TEST=$(cat id)   #This line does not work
          echo TEST

The error which is produced is:

ERROR: Invalid interpolation format for "command" option in service "foo":

Upvotes: 0

Views: 132

Answers (1)

Adiii
Adiii

Reputation: 60036

you have to change a bit your config file.

version: '2'
services:
  foo:
    image: busybox
    command:
      - /bin/sh
      - -c
      - |
          echo "hello" > id 
          ls
          cat id
          TEST=$$(cat id)
          echo TEST

I checked against Docker Version Docker version 18.06.0-ce and Docker compose version docker-compose version 1.20.0-rc2,

https://github.com/docker/docker.github.io/blob/master/compose/compose-file/index.md#variable-substitution

You can use a $$ (double-dollar sign) when your configuration needs a literal dollar sign. This also prevents Compose from interpolating a value, so a $$ allows you to refer to environment variables that you don’t want processed by Compose.

Upvotes: 1

Related Questions