简体   繁体   中英

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.

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.

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 (ie by Dockerfile's CMD).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM