简体   繁体   English

以非root用户运行docker inside docker

[英]Run docker inside docker as non root user

How can I run docker commands inside a docker container when the user is not root?当用户不是 root 用户时,如何在 docker 容器内运行 docker 命令?

The reason behind this (running as non root) is that the (first) container creates some files on a mounted volume.这背后的原因(以非根用户身份运行)是(第一个)容器在已安装的卷上创建了一些文件。 If the user in the container is root then these files' owner is also root.如果容器中的用户是 root,那么这些文件的所有者也是 root。 If I run the container with the same user as on the host system then these files have the correct user and group.如果我使用与主机系统相同的用户运行容器,那么这些文件具有正确的用户和组。

docker run --rm -it -u $(id -u):$(id -g) -v /var/run/docker.sock:/var/run/docker.sock ubuntu /bin/bash

// inside container:
// assume docker binary is available
docker pull alpine

This will not work when run as a non root user giving following error:当以非 root 用户身份运行时出现以下错误,这将不起作用:

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.24/images/create?fromImage=alpine&tag=latest: dial unix /var/run/docker.sock: connect: permission denied

Docker: Docker:

docker run -it -u $(id -u):$(id -g) --group-add $(getent group docker | cut -d ':' -f 3) --rm -v /var/run/docker.sock:/var/run/docker.sock docker docker --version

For Docker Compose set group_add under your service and set the env variable:对于 Docker Compose 在您的服务下设置group_add并设置环境变量:

export DOCKER_GROUP_ID=$(getent group docker | cut -d ':' -f 3);
services:
  myservice:
    image: docker
    group_add:
      - ${DOCKER_GROUP_ID}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 Docker 镜像中的用户切换为非 root 用户 - Switching users inside Docker image to a non-root user 以非 root 用户身份在 Docker 容器内运行 tcpdump - Running tcpdump inside a Docker container as non-root user 如何以非root用户身份运行Docker容器以及如何与他人共享Docker映像? - How to run docker container as non-root user and how to share the docker image to others? 在 Docker 映像中以非 root 用户身份运行 cronjob 的正确方法是什么? - What is the proper way to run a cronjob as a non-root user in a Docker image? 在 Cirrus/Kubernetes 环境中以特定的非 root 用户身份运行 Docker 容器 - Run Docker container in Cirrus/Kubernetes environment as specific non-root user 你能否以root身份在Docker容器内启动进程,而exec调用的默认用户是非root用户? - Can you start a process inside a Docker container as root, while having the default user of an exec call be non-root? 在Docker上以非root用户身份运行JAR时出现权限错误 - Permission error running a JAR as non-root user on Docker 让非root用户写入Docker中的linux主机 - Let non-root user write to linux host in Docker Docker - 在 ENTRYPOINT 中切换到非 root 用户是否安全? - Docker - is it safe to switch to non-root user in ENTRYPOINT? Docker 使用 dockerfile 将卷目录权限授予非 root 用户 - Docker mount volume directory permissions to non root user using dockerfile
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM