简体   繁体   English

如何从容器使用root用户?

[英]How to use root user from a container?

I'm new to the docker and linux. 我是docker和linux的新手。
I'm using windows 10 and got a github example to create a container with Centos and nginx. 我正在使用Windows 10,并有一个github示例,用Centos和nginx创建一个容器。
I need to use the root user to change the nginx.config. 我需要使用root用户来更改nginx.config。
From Kitematic, I clicked on Exec to get a bash shell in the container and I tried sudo su – as blow: 在Kitematic中,我单击Exec以在容器中获得bash外壳,然后尝试了sudo su –受到打击:

  sh-4.2$ sudo su –  
  sh: sudo: command not found  

So, I tried to install sudo by below command: 因此,我尝试通过以下命令安装sudo:

sh-4.2$ yum install sudo -y  
Loaded plugins: fastestmirror, ovl  
ovl: Error while doing RPMdb copy-up:  
[Errno 13] Permission denied: '/var/lib/rpm/Installtid'  
You need to be root to perform this command.  

Then I ran su - , but I don't know the password! 然后我运行了su-,但是我不知道密码! How can I set the password? 如何设置密码?

sh-4.2$ su -  
Password: 

Then, from powershell on my windows I also tried: 然后,从Windows的Powershell中我也尝试了:

PS C:\Containers\nginx-container> docker exec -u 0 -it 9e8f5e7d5013 bash 

but it shows that the script is running and nothing happened and I canceled it by Ctrl+C after an hour. 但是它表明该脚本正在运行,什么也没发生,一个小时后我通过Ctrl + C取消了该脚本。

Some additional information: 一些其他信息:

Here is how I created the container: 这是我创建容器的方法:

PS C:\Containers\nginx-container> s2i build https://github.com/sclorg/nginx-container.git --context->dir=examples/1.12/test-app/ centos/nginx-112-centos7 nginx-sample-app

From bash shell in the container. 从bash外壳中的容器中取出。 I can get the os information as below: 我可以获得以下操作系统信息:

sh-4.2$ cat /etc/os-release  
NAME="CentOS Linux"  
VERSION="7 (Core)"  
ID="centos"  
ID_LIKE="rhel fedora"  
VERSION_ID="7"  
PRETTY_NAME="CentOS Linux 7 (Core)"  
ANSI_COLOR="0;31"  
CPE_NAME="cpe:/o:centos:centos:7"  
HOME_URL="https://www.centos.org/"  
BUG_REPORT_URL="https://bugs.centos.org/"  

CENTOS_MANTISBT_PROJECT="CentOS-7"  
CENTOS_MANTISBT_PROJECT_VERSION="7"  
REDHAT_SUPPORT_PRODUCT="centos"  
REDHAT_SUPPORT_PRODUCT_VERSION="7"  

I would really appreciate if you can help me to fix these issues. 如果您能帮助我解决这些问题,我将不胜感激。
Thanks! 谢谢!

Your approach is generally wrong. 您的方法通常是错误的。 You should prepare the file outside the container an then let the Docker itself to change it. 您应该在容器外部准备文件,然后让Docker本身对其进行更改。

There are several ways to achieve this. 有几种方法可以实现此目的。

  1. You can mount your file during startup: 您可以在启动期间挂载文件:

    docker run -v /your/host/path/to/config.cfg:/etc/nginx/config.cfg ...

  2. You can copy the file into the container during building the container (inside Dockerfile): 您可以在构建容器(在Dockerfile内部)期间将文件复制到容器中:

    FROM base-name COPY config.cfg /etc/nginx/

  3. You can apply a patch to the config script (once again, a Dockerfile): 您可以将补丁应用到配置脚本(再次是Dockerfile):

    FROM base-name ADD config.cfg.diff /etc/nginx/ RUN ["patch", "-N", "/etc/nginx/config.cfg", "--input=/etc/nginx/config.cfg.diff"]

For each method, there are lots of examples on StackOverflow. 对于每种方法,StackOverflow上都有很多示例。

You should read Docker's official tutorial on building and running custom images . 您应该阅读Docker的有关构建和运行自定义映像官方教程 I rarely do work in interactive shells in containers; 我很少在容器中的交互式shell中工作; instead, I set up a Dockerfile that builds an image that can run autonomously, and iterate on building and running it like any other piece of software. 相反,我设置了一个Dockerfile来构建可以自动运行的映像,并像其他任何软件一样迭代构建和运行该映像。 In this context su and sudo aren't very useful because the container rarely has a controlling terminal or a human operator to enter a password (and for that matter usually doesn't have a valid password for any user). 在这种情况下, susudo并不是很有用,因为该容器很少具有控制终端或人工操作员来输入密码(因此,对于任何用户通常都没有有效的密码)。

Instead, if I want to do work in a container as a non-root user, my Dockerfile needs to set up that user: 相反,如果我想以非root用户身份在容器中工作,则我的Dockerfile需要设置该用户:

FROM ubuntu:18.04
WORKDIR /app
COPY ...
RUN useradd -r -d /app myapp
USER myapp
CMD ["/app/myapp"]

The one exception I've seen is if you have a container that, for whatever reason, needs to do initial work as root and then drop privileges to do its real work. 我看到的一个例外是,如果您有一个容器,无论出于何种原因,该容器都需要以root用户身份进行初始工作,然后放弃特权才能进行其实际工作。 (In particular the official Consul image does this.) That uses a dedicated lighter-weight tool like gosu or su-exec . (特别是官方的Consul映像执行此操作。)它使用专用的轻量级工具,例如gosusu-exec A typical Dockerfile setup there might look like 那里的典型Dockerfile设置可能看起来像

# Dockerfile
FROM alpine:3.8
RUN addgroup myapp \
 && adduser -S -G myapp myapp
RUN apk add su-exec
WORKDIR /app
COPY . ./
ENTRYPOINT ["/app/docker-entrypoint.sh"]
CMD ["/app/myapp"]
#!/bin/sh
# docker-entrypoint.sh

# Initially launches as root
/app/do-initial-setup

# Switches to non-root user to run real app
su-exec myapp:myapp "$@"

Both docker run and docker exec take a -u argument to indicate the user to run as. docker rundocker exec使用-u参数指示用户以其身份运行。 If you launched a container as the wrong user, delete it and recreate it with the correct docker run -u option. 如果您以错误的用户身份启动了容器,请删除它,然后使用正确的docker run -u选项重新创建它。 (This isn't one I find myself wanting to change often, though.) (不过,这不是我发现自己想要经常更改的一种。)

I started the container on my local and turns out you don't need sudo you can do it with su that comes by default on the debian image 我在本地启动了容器,结果发现您不需要sudo您可以使用debian映像默认随附的su来进行操作

docker run -dit centos bash
docker exec -it 9e82ff936d28 sh
su

also you could try executing the following which defaults you to root: 您也可以尝试执行以下默认操作以root用户身份执行以下操作:

docker run -dit centos bash
docker exec -it 9e82ff936d28 bash

never less you could create the Nginx config outside the container and just have it copy using docker container copy {file_path} {container_id}:{path_inside_container} 您可以在容器外部创建Nginx配置,只需使用docker container copy {file_path} {container_id}:{path_inside_container}

Thanks everyone. 感谢大家。

I think it's better to setup a virtualbox with Centos and play with nginx. 我认为最好是用Centos设置一个virtualbox并与nginx一起玩。 Then when I'm ready and have a correct nginx.config, I can use Dockerfile to copy my config file. 然后,当我准备好并拥有正确的nginx.config时,可以使用Dockerfile复制我的配置文件。

VM is so slow and I was hoping that I can work in interactive shells in containers to learn and play instead of using a VM. VM太慢了,我希望可以在容器中的交互式shell中学习和玩游戏,而不是使用VM。 do you have any better idea than virtualbox? 您有什么比virtualbox更好的主意吗?

I tried 我试过了

  docker run -dit nginx-sample-app bash  
  docker exec -u root -it 9e8f5e7d5013 bash  

And it didn't do anything , it stays in the below status: here 而且它什么也没做,仍然保持以下状态: 这里

the same commands worked on debian image but not centos. 相同的命令适用于debian映像,但不适用于centos。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM