我在本地机器上运行这个命令
docker run -d --name SonarQube -p 9000:9000 -p 9092:9092 sonarqube
这从 dockerhub 获取分支的图像,然后创建图像的容器。现在我想对文件进行一些更改,但容器中没有编辑器。我尝试使用 apt-get 安装 vi,但它说我需要成为执行命令的root用户,当我写sudo时它说找不到命令。如何在容器中安装编辑器?
我运行这个命令来安装 vim
sudo apt-get 安装 vim
这是我得到的错误
bash:sudo:找不到命令
最佳答案
尝试将用户传递给 docker run 命令。
docker run -it --user root --name SonarQube -p 9000:9000 -p 9092:9092 sonarqube
但是您可能会看到与此方法相同的问题,因为这将使用
root
启动容器,而不是 sonarqube用户。所以我会推荐使用下面的方法。
FROM sonarqube
USER root
RUN apt-get update \
&& apt-get install -y vim
USER sonarqube
ENTRYPOINT ["./bin/run.sh"]
用户
root (id = 0) is the default user within a container. The image developer can create additional users. Those users are accessible by name. When passing a numeric ID, the user does not have to exist in the container.
The developer can set a default user to run the first process with the Dockerfile USER instruction. When starting a container, the operator can override the USER instruction by passing the -u option.
-u="", --user="": Sets the username or UID used and optionally the groupname or GID for the specified command.
The followings examples are all valid:
--user=[ user | user:group | uid | uid:gid | user:gid | uid:group ]
reference-run
关于docker - 如何在 docker 容器中成为 root,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58234639/