Home > OS >  Need to write a Regex in docker-compose environment variable
Need to write a Regex in docker-compose environment variable

Time:12-15

I'm trying to write this environment variable:

ExpectedRegex=^[\\"rn ]*A\$$$$

(Meaning: allow [, ", r, n, (space)] for any number of times, then require "A$". Matching the start and end of the string)

docker-compose.yml snap:

environment:
 - ExpectedRegex=^[\\"rn ]*A\$$$$

But docker-compose returns me this regex:

REGEX=^[\\\"rn ]*A\\$$

The problem lies in the last dollar which docker-compose adds another \ before it.

Inside the container I'm using .Net Core to match the regex. When debug-printing the string received I see the double backslash

How can I write the regex in the docker-compose yaml file so that it doesn't get double backslashed?

CodePudding user response:

I assume, regex is in environment variables, yes? You can use single quotes to escape it. Something like this:


version: '3.7'

services:

  something:
    image: something:latest
    environment:
      REGEX: '^[\\"rn ]*A\$$$$' 
      // more parameters below

also i think last 4 $ signs are wrong here, seems like only first not escaped is took into account, i think you need to escape all of them, but the last one:

      REGEX: '^[\\"rn ]*A\$\$\$$' 

Last one to match end of string, i mean.

CodePudding user response:

@vodolaz095 Answer is a good one because he fix the REGEX, but I found that docker-compose always escapes string on the yaml file.

The only safe way is to use an external environment file with env_file: option.

# docker-compose.yml
version: '3.7'
services:
  test:
    image: alpine
    environment:
      TEST_YML: '$$'
    env_file: .env
    command: env
# .env
TEST_FILE=$$
$ docker-compose up
Creating network "tmprc0c7hqcve_default" with the default driver
Creating tmprc0c7hqcve_test_1 ... done
Attaching to tmprc0c7hqcve_test_1
test_1  | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:    /sbin:/bin
test_1  | HOSTNAME=240627fd4c1d
test_1  | TEST_FILE=$$
test_1  | TEST_YML=$
test_1  | HOME=/root
tmprc0c7hqcve_test_1 exited with code 0
  • Related