0

I'm using python to create templates of docker projects, it is working fine until the very last step when I want to build the container to create the image. The project structure I have so far is similar to this:

project
  |--flask_server.py
  |--main_script.py
  |--image_creator.sh
  |--Dockerfile
  |--requirements.txt

My issue is with the file: image_creator.sh, which basically has:

docker build -t my_project:latest .

That can't be executed without sudo. This is what I have tried so far in the python script, create_contaner.py, which have the following code:

import os
import subprocess
...
...
subprocess.call(['image_creator.sh'])

1)

python create_contaner.py 

2)

 sudo python create_contaner.py

3)

sudo su
python create_contaner.py 

For all three cases I get:

PermissionError: [Errno 13] Permission denied: 'image_creator.sh'

For the last one

I have also tried adding my current user to the group that can run docker without using sudo as explained here

sudo groupadd docker
sudo gpasswd -a $USER docker

After doing it from the terminal I can execute:

docker build -t my_project:latest .

Which works with sudo, but if from that user I open python and try:

python create_contaner.py

I still get the same error. Someone has pointing me to use docker-compose instead of docker build, that that does not seem to get rid of the issue of the permissions.

Luis Ramon Ramirez Rodriguez
  • 9,591
  • 27
  • 102
  • 181

1 Answers1

1

Set permission to your bash script before call the script in python

 chmod +x image_creator.sh

If you want to automate, you can set permission to file from python

import os

command = os.popen('chmod +x image_creator.sh')
print(command.read())
print(command.close())

or you check better answer to run bash in python here

Adiii
  • 54,482
  • 7
  • 145
  • 148