How do I prevent root access to my docker container

前端 未结 4 684
情书的邮戳
情书的邮戳 2021-01-15 03:48

I am working on hardening our docker images, which I already have a bit of a weak understanding of. With that being said, the current step I am on is preventing the user fr

4条回答
  •  北海茫月
    2021-01-15 04:39

    When the docker is normally run from one host, you can do some steps.
    Make sure it is not run from another host by looking for a secret in a directory mounted from the accepted host.

    Change the .bashrc of the users on the host, so that they will start running the docker as soon as they login. When your users needs to do other things on the host, give them an account without docker access and let them sudo to a special user with docker access (or use a startdocker script with a setuid flag).

    Start the docker with a script that you made and hardened, something like startserver.

    #!/bin/bash
    
    settings() {
       # Add mount dirs. The homedir in the docker will be different from the one on the host.
       mountdirs="-v /mirrored_home:/home -v /etc/dockercheck:/etc/dockercheck:ro"
       usroptions="--user $(id -u):$(id -g) -v /etc/passwd:/etc/passwd:ro"   
       usroptions="${usroptions} -v/etc/shadow:/etc/shadow:ro -v /etc/group:/etc/group:ro"
    }
    
    # call function that fills special variables
    settings
    
    image="my_image:latest"
    
    docker run -ti --rm ${usroptions} ${mountdirs} -w $HOME --entrypoint=/bin/bash "${image}"
    

    Adding a variable --env HOSTSERVER=${host} won't help hardening, on another server one can add --env HOSTSERVER=servername_that_will_be_checked.

    When the user logins to the host, the startserver will be called and the docker started. After the call to the startserver add exit to the .bash_rc.

提交回复
热议问题