Search code examples
dockerdocker-composesudosudoers

Run specific docker container without sudo and without docker group


I want to run a specific docker-compose file without entering the sudo password and without assigning that user who runs the command to the docker group for security reasons.

I thought about using the NOPASSWD inside sudoers file and run a bash script called "bash-dockercompose-up.sh" that simply runs docker-compose up -d.

However, it needs the sudo command before the docker-compose up -d to connect to docker host.

This is my /etc/sudoers file:

exampleuser        ALL=(root)       NOPASSWD:/usr/bin/bash-dockercompose-up.sh

Solution

  • Ok I was able to run it by using the python official sdk library.

    https://docs.docker.com/engine/api/sdk/

    I created a python script called "service-up.py"

    service-up.py

    import docker
    client = docker.from_env()
    container = client.containers.get('id or name here')
    container.start()
    

    Then compile it into a binary file in order to change it's uid permissions so a non root user can run it:

    pyinstaller service-up.py
    

    move into the dist folder where file is located and run:

    chown root:root service-up
    chmod 4755 service-up