docker - 如何在 docker 容器中成为 root

标签 docker

我在本地机器上运行这个命令
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/

相关文章:

docker - 为什么我有这么多REPOSITORY name = <none>的Docker镜像?

apache - Docker 错误 : Cannot create container for service web: invalid mode

ruby-on-rails - Rails 5没有Cron的常规任务

docker - 如何通过Docker运送python应用程序

docker - 如何检查应用程序是否已在容器内启动

bash - 循环遍历docker bash命令的输出

docker - hostconfig.json 在哪里(Docker 桌面 + WSL2 环境)

Docker.IO 文件系统一致性

python - 使用 venv 运行 docker 容器时找不到 gunicorn

Django,Sqlalchemy : Cannot drop table ganalytics_article because other objects depend on it