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

Time:09-01

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?

CodePudding user response:

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

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

CodePudding user response:

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
  • Related