0

I am trying to generate the random password using bash script and then pass it to the docker-compose file to replace the value of WORDPRESS_DB_PASSWORD in docker-compose file. It is generating the random password but I am not able to substitute the value in docker-compose.

Bash script

#!bin/bash 
password= tr -dc 'a-zA-Z0-9' < /dev/urandom | head -c10
echo $password
INPUT=dockercompose.yaml
sed  -i -e 's/\(^WORDPRESS_DB_PASSWORD=\).*/\1$password/' $INPUT

dockercompose.yaml

version: '3.1'

services:

  wordpress:
    image: wordpress
    restart: always
    ports:
      - 8080:80
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: exampleuser
      WORDPRESS_DB_PASSWORD: examplepass
      WORDPRESS_DB_NAME: exampledb
    volumes:
      - wordpress:/var/www/html

  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: exampledb
      MYSQL_USER: exampleuser
      MYSQL_PASSWORD: examplepass
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
    volumes:
      - db:/var/lib/mysql

volumes:
  wordpress:
  db:
coder
  • 61
  • 5
  • 1
    Probably use a dedicated YAML too like `yq`. This is almost certainly a duplicate; please search before asking. – tripleee Dec 17 '21 at 11:59
  • I am trying to do it using sed or awk command – coder Dec 17 '21 at 12:06
  • Why not use the standardized approach with an `.env` file? – KamilCuk Dec 17 '21 at 12:07
  • The immediate error is that you are using incorrect syntax for assigning `password`. Your quoting is also wrong in several places. Probably try http://shellcheck.net/ before asking for human assistance. – tripleee Dec 17 '21 at 12:10

0 Answers0