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.

3
  • 2
    did you set chmod +x image_creator.sh? Commented Sep 12, 2019 at 2:28
  • Rigth, that seems to work if I do it manually. Can I automate:'chmod +x image_creator.sh ' from python? otherwise I get the same issue but with a different file Commented Sep 12, 2019 at 2:55
  • @Adiii got it, you can add that as the answer Commented Sep 12, 2019 at 3:04

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.