Rob
Rob

Reputation: 1328

Dynamically replace current time inside docker-compose command

version: '3.7'

services:
  pgdump:
    image: postgres:alpine
    command: pg_dump -f "backup-`date -u -Iseconds`.pg_restore" $DATABASE_URL

This produces a file named

backup-`date -u -Iseconds`.pg_restore

instead of the desired

backup-2021-04-14T16:42:54+00:00.pg_restore.

I also tried:

command: pg_dump -f backup-`date -u -Iseconds`.pg_restore $DATABASE_URL

command: pg_dump -f "backup-${date -u -Iseconds}.pg_restore" $DATABASE_URL

command: pg_dump -f backup-${date -u -Iseconds}.pg_restore $DATABASE_URL

All of them yield different errors.

Upvotes: 2

Views: 1314

Answers (3)

MaxA
MaxA

Reputation: 78

You can create the filename and store it as a variable with shell command before doing the pg_dump:

version: '3.7'

services:
  pgdump:
    image: postgres:alpine
    entrypoint: ["/bin/sh","-c"]
    command: >
      "FILENAME=backup-`date -u -Iseconds`.pg_restore
      && pg_dump -f $$FILENAME $$DATABASE_URL"

Successfully tested against Docker image for postgres 13.6.

Upvotes: 0

Rob
Rob

Reputation: 1328

As of April 2021 command substitution is not supported by docker-compose according to this GitHub issue.

As a workaround in my use case, one could either use native docker run commands, where substitution works or use an .env file.

Upvotes: 3

om-ha
om-ha

Reputation: 3602

Current command

The date command itself is incorrect. Try running it on its own

date -u -Iseconds
echo `date -u -Iseconds`

From your command, I presume you want date in UTC seconds since epoch? Epoch by itself is UTC. So you just need seconds since Epoch. No need for -u parameter.

Solution

Here's the correct command in two forms:

A.

    command: pg_dump -f "backup-`date +'%s'`.pg_restore" $DATABASE_URL

B.

    command: pg_dump -f "backup-$(date +'%s').pg_restore" $DATABASE_URL

Explanation

There are multiple things to watch out for in the command you provided:

  1. Notice the double quotes around the file name? This means you cannot nest another double-quote within the original outer pair without escaping the inner ones with \. Another alternative option is to use as many single-quote pairs you want within a pair of double-quotes. See this answer and this excerpt about 2.2.2 Single-Quotes and 2.2.3 Double-Quotes.
  2. For string interpolation, you can use either $() or `` notation. But NOT within single-quotes as I said.
  3. As a dry-run test, create a file directly with said notations:
vi "backup-`date +'%s'`.txt"
vi "backup-$(date +'%s').txt"
  1. As for date format. Both GNU/date BSD/date accept %s to represent seconds since Epoch. Find "%s" in ss64 or man7 or cyberciti.
  2. Docker-related, watch out what command does. Source:

command overrides the the default command declared by the container image (i.e. by Dockerfile's CMD).

Upvotes: 2

Related Questions