16

I want to suffix a file name with the current date in my docker-compose.yml file. The end goal being a file such as my_log_123456789.log, where 123456789 is the current unix date.

I tried using an .env file, but that did not evaluate as expected. My attempt was:

# .env
NOW="$(date +%s)"
# docker-compose.yml
version: '3.8'
services:
  xxx:
      container_name: xxx
      image: xxx/xxx
      volumes:
        - /home/ubuntu/xxx_"$(NOW)".log:/home/ubuntu/xxx.log
      ...

I tried a few variations of this and had the following issues:

  • ERROR: Invalid interpolation format for "volumes" option in service "xxx": "/home/ubuntu/xxx_$(NOW).log:/home/ubuntu/xxx.log"
  • A directory was created that was named 'xxx_"$(date +%s)".log'

How can I append the current date to a log file in a docker-compose.yml file?

7
  • You can pass environment variables to docker-compose run -e VAR=VAL. May 31, 2020 at 19:47
  • No, you can't..
    – KamilCuk
    May 31, 2020 at 19:48
  • 1
    Can you export the variable from your environment before running the Compose file? i.e. take NOW out of .env
    – DazWilkin
    May 31, 2020 at 19:49
  • Running NOW="$(date +%s)" sudo docker-compose up -d produces a directory xxx_NOW.log/
    – ReactHelp
    May 31, 2020 at 19:51
  • 2
    @KamilCuk, yes, yes you can. docs.docker.com/compose/environment-variables May 31, 2020 at 19:52

2 Answers 2

8

What you can do is to generate .env just before calling docker-compose:

#!/usr/bin/env bash

cat << EOF > .env
NOW=$(date +%s)
EOF

docker-compose up
6

No, docker doesn't have a "dynamic" variable - the text from --env-file is read by docker as it is, literally, leading and trailing ' or " are removed, without any interpretation and expansions done by shell.

Create another script that will export the variable value to docker-compose or will edit the content of your variable in your .env file before calling docker-compose.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.