user3700919
user3700919

Reputation: 365

Cannot execute script for a docker container?

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

Upvotes: 0

Views: 1744

Answers (1)

Tarun Lalwani
Tarun Lalwani

Reputation: 146490

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"

Upvotes: 1

Related Questions