30
web:
  image: nginx
  volumes:
    - "./app:/src/app"
  ports:
    - "3030:3000"
    - "35729:35729"

I would like to have a bash script to replace the nginx for an argument with bash script.

./script apache

Will replace nginx for apache

1
  • 1
    As it seems you want to override value in a docker-compose file, you might want to look at my docker compose wrapper program that allows default that can be overridden by environment variables (e..g to make the difference between a testing and production system). Because it supports YAML 1.2, it also relieves you of the need to quote those "3030:3000" port mappings that could be interpreted by compose's default YAML 1.1 parser as sexagesimals
    – Anthon
    May 18, 2016 at 6:37

4 Answers 4

20

You can use this: sed -r 's/^(\s*)(image\s*:\s*nginx\s*$)/\1image: apache/' file

Sample run:

$ cat file
web:
  image: nginx
  volumes:
    - "./app:/src/app"
  ports:
    - "3030:3000"
    - "35729:35729"
$ sed -r 's/^(\s*)(image\s*:\s*nginx\s*$)/\1image: apache/' file
web:
  image: apache
  volumes:
    - "./app:/src/app"
  ports:
    - "3030:3000"
    - "35729:35729"

To persist the changes into the file you can use in-place option like this:

$ sed -ri 's/^(\s*)(image\s*:\s*nginx\s*$)/\1image: apache/' file

If you want it inside a script you can just put the sed command inside a script and execute it with $1 in sustitution.

$ vim script.sh 
$ cat script.sh 
sed -ri 's/^(\s*)(image\s*:\s*nginx\s*$)/\1image: '"$1"'/' file
$ chmod 755 script.sh 
$ cat file 
web:
  image: nginx
  volumes:
    - "./app:/src/app"
  ports:
    - "3030:3000"
    - "35729:35729"
$ ./script.sh apache
$ cat file 
web:
  image: apache
  volumes:
    - "./app:/src/app"
  ports:
    - "3030:3000"
    - "35729:35729"
$
10

You could create your script.sh as follows:

#!/bin/bash
# $1 image value you want to replace
# $2 is the file you want to edit
sed -i "" "/^\([[:space:]]*image: \).*/s//\1$1/" $2

And then run: ./script.sh apache filename.yaml

3
  • Does empty quotes "" are necessary? What they do? Apr 30, 2019 at 9:23
  • 2
    Correct answer: sed -i "/^\([[:space:]]*image: \).*/s//\1$1/" $2 , Remove the empty quotes otherwise it will complain about the no file or directory error ! Jun 24, 2019 at 11:47
  • to make it work both in Linux and OSX, add .bak next to -i e.g. sed if.bak .... Then of course you can delete .bak file. Sep 23, 2019 at 11:00
7

since this is apparently a docker-compose file, you might want to try

 image: ${webserver_image}

and then set:

 webserver_image=[nginx | apache]

before launching. i believe this should give you a nice interpolation.

6

script:

#!/bin/bash
sed -i.bak "s/\bnginx\b/$1/g" file
# \b matches for word boundary
# -i changes the file in-place
# -i.bak produces a backup with .bak extension

Now you can do ./script apache to replace nginx with apache.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.