Automate container creation from python

by Luis Ramon Ramirez Rodriguez   Last Updated September 12, 2019 02:26 AM

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:


My issue is with the file:, 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,, which have the following code:

import os
import subprocess




 sudo python


sudo su

For all three cases I get:

PermissionError: [Errno 13] Permission denied: ''

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:


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.

Tags : python docker

Related Questions

Docker Engine API connect to an existing network

Updated June 26, 2018 09:26 AM