João Pedro Schmitt
João Pedro Schmitt

Reputation: 1498

How to escape dollar sign on Docker-compose bash script command

I have the following container configured as part of a docker-compose script. I need to remove the prefix URL /rooturl and continue with the remaining part of the request. But I'm not having success because I can't escape the dollar sign ($) when the content is written to the nginx.conf file (the dollar sign disappears).

app-reverse-proxy:
    image: nginx:1.23
    ports:
      - "9090:80"
    command: |
      bash -c 'bash -s << EOF
        cat > /etc/nginx/nginx.conf << EON
          daemon off;
          user www-data;
          events {
            worker_connections 1024;
          }
          http {
            gzip on;
            gzip_disable "msie6";
            include /etc/nginx/mime.types;
            server {
              listen 80;
              root /var/www/html;
              index index.html;
              location ^~ /rooturl/ {
                proxy_pass http://other-app:8080;
                rewrite /rooturl(.*) /$$1 break; 
              }
            }
          }
      EON
      nginx
      EOF'
    depends_on:
      - other-app
    networks:
      - docker-net

I already tried to escape it using \$$1 and "\$$1", no success so far, any ideas on how I can do that?

Upvotes: 1

Views: 502

Answers (3)

acenturyandabit
acenturyandabit

Reputation: 1418

  1. Replace $ with % everywhere it appears in your heredoc within your docker-compose.
  2. Add sed 's/%/\x24/g' -i filename to use hex codes to replace % with $ (note the single quotes in sed are important!

Upvotes: 0

David Maze
David Maze

Reputation: 159393

You should create the file directly on your host.

# nginx.conf
daemon off;
user www-data;
http {
  ...
}

Since this is just a file, you don't need to escape anything.

Once you've done that, you can inject it into the container using a bind mount. The only thing your command: does is to try to create the file, and then run nginx as normal; since you're not creating the file, you can use the default command from the image.

app-reverse-proxy:
    image: nginx:1.23
    ports:
      - "9090:80"
    volumes: # <-- add
      - ./nginx.conf:/etc/nginx/nginx.conf
    # no command:
    depends_on:
      - other-app
    networks:
      - docker-net

Upvotes: 2

bluepuma77
bluepuma77

Reputation: 496

We use 'EOF' to generate a file which is not changed. Try that.

cat << 'EOF' > file echo "I love my $$$" EOF

Upvotes: 1

Related Questions