0

Im not sure why but I cant seem to run this script inside of the docker container from the host system

on the host im executing this code in a shell script

#!/bin/bash

Docker_wordpress_status_check () {
mysql_docker_status=$(docker container ls | grep -E 'docker_image_name|dockerwordpressmaster_wp-fpm_1')
if [[ "$mysql_docker_status" ]]; then
echo "checking to see if wordpress exists"
wget https://s3/wordpress_staging_to_production_image_fixer.sh
chmod +x wordpress_staging_to_production_image_fixer.sh
docker cp wordpress_staging_to_production_image_fixer.sh dockerwordpressmaster_wp-fpm_1:/var/www/html/wordpress_staging_to_production_image_fixer.sh
docker exec -it dockerwordpressmaster_wp-fpm_1 sh -c "./wordpress_staging_to_production_image_fixer.sh"
fi
}

Docker_wordpress_status_check

This script works fine for the most part and I can even see the file in the correct directory this being

/var/www/html/

and I can clearly see that the file exists inside of the container

docker exec -it dockerwordpressmaster_wp-fpm_1 sh
/var/www/html # ls -l | grep wordpress_staging_to_production_image_fixer.sh
-rwxr-xr-x    1 500      500            898 Dec 26 17:31 
wordpress_staging_to_production_image_fixer.sh

however when I try to execute the script from within the container

docker exec dockerwordpressmaster_wp-fpm_1 sh ./var/www/html/wordpress_staging_to_production_image_fixer.sh

sh: can't open './var/www/html/wordpress_staging_to_production_image_fixer.sh'

docker exec dockerwordpressmaster_wp-fpm_1 "sh" -c "/var/www/html/wordpress_staging_to_production_image_fixer.sh"

sh: /var/www/html/wordpress_staging_to_production_image_fixer.sh: not found

docker exec dockerwordpressmaster_wp-fpm_1 sh -c wordpress_staging_to_production_image_fixer.sh

sh: wordpress_staging_to_production_image_fixer.sh: not found

docker exec dockerwordpressmaster_wp-fpm_1 bash ./var/www/html/wordpress_staging_to_production_image_fixer.sh

bash: ./var/www/html/wordpress_staging_to_production_image_fixer.sh: No such file or directory

I'm not quite sure what I'm doing wrong as for the file to not execute I realize that owner may be incorrect and hence I may not be able to run the script however

docker exec dockerwordpressmaster_wp-fpm_1 sh chown root:root /var/www/html/wordpress_staging_to_production_image_fixer.sh

sh: can't open 'chown'

Any help in solving this issue will be greatly appreciated

user3700919
  • 325
  • 2
  • 4
  • 19
  • 1
    sh -c 'chown ...' ? – bartimar Dec 26 '17 at 19:16
  • 4
    Try `sh /var/www/html/wordpress_staging_to_production_image_fixer.sh` – Tarun Lalwani Dec 26 '17 at 19:18
  • docker exec dockerwordpressmaster_wp-fpm_1 sh chown 'root:root /var/www/html/wordpress_staging_to_production_image_fixer.sh' seems to have worked combined with sh /var/www/html/wordpress_staging_to_production_image_fixer.sh seem to be getting the output that im looking for, Thank you all – user3700919 Dec 26 '17 at 19:20

1 Answers1

1

The below statement may not have worked for you for two reason

docker exec dockerwordpressmaster_wp-fpm_1 "sh" -c "/var/www/html/wordpress_staging_to_production_image_fixer.sh"

One is Access issues and another is the shebang #!/bin/sh missing at the start of shell script. Your error was "sh: /var/www/html/wordpress_staging_to_production_image_fixer.sh: not found". Which definitely indicates access issue. So you need to chown to root:root or login using the user with id 500.

docker exec sh -c "pwd && chown root:root wordpress_staging_to_production_image_fixer.sh‌​ && sh wordpress_staging_to_production_image_fixer.sh"

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
  • 1
    Maybe more explicitly point out the difference between `sh /path/to/file` and `sh -c 'command; command'` as well as the difference between absolute and relative paths. – tripleee Dec 27 '17 at 10:35
  • @tripleee what are the differences? – x29a Jan 25 '18 at 18:42
  • The OP seems not to know the difference between `/var` and `./var` if that's what you mean. They are the same when you are in the root directory, but are you? Running `sh -c` allows you to pass in an arbitrarily complex sequence of commands, whilst without `-c` you can only supply the name of an existing script. But conversely, `sh -c existingscript` requires the `existingscript` file to be executable, whereas `sh existingscript` only requires read permissions to the script file. – tripleee Jan 26 '18 at 03:38
  • (And of course, `sh chown` is just whimsical if you hope to execute `/bin/chown` which isn't a `sh` script at all, most places. The OP apparently also needs to better [understand the difference between `sh` and `bash`](https://stackoverflow.com/questions/5725296/difference-between-sh-and-bash).) – tripleee Jan 26 '18 at 03:46