Bash:sudo:找不到命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51734540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:59:57  来源:igfitidea点击:

Bash: sudo: command not found

bashdockerpermissionssudo

提问by adhg

I have an up and running containers and I wish to execute a database backup. Apparently, a simple command from the docker such as: sudo mkdir new_folderresult in: bash: sudo: command not found

我有一个正在运行的容器,我希望执行数据库备份。显然,来自 docker 的一个简单命令,例如:sudo mkdir new_folder导致:bash: sudo: command not found

What have I tried (on an intuitive level) I accessed one of the running container with docker exec -i -t 434a38fedd69/bin/bashand RUN

我尝试过什么(在直观层面上)我使用docker exec -i -t 434a38fedd69/bin/bashRUN访问了正在运行的容器之一

apt-get update 
apt-get install sudo

when exit back to docker and tried to perform sudo mkdir new_folderbut I got the same message bash: sudo: command not found

当退出回到 docker 并尝试执行sudo mkdir new_folder但我收到相同的消息时bash: sudo: command not found

Baresp@adhg MINGW64 /c/Program Files/Docker Toolbox/postgre
$ mkdir new_folder
mkdir: cannot create directory ‘new_folder': Permission denied

Baresp@adhg MINGW64 /c/Program Files/Docker Toolbox/postgre
$ sudo mkdir new_folder
bash: sudo: command not found

BTW, I'm not sure if this is relevant but the docker-compose file I was using is:

顺便说一句,我不确定这是否相关,但我使用的 docker-compose 文件是:

version: '2'

services:
postgres:
image: postgres
environment:
  POSTGRES_USER: postgres
  POSTGRES_PASSWORD: changeme
  PGDATA: /data/postgres
volumes:
   - /data/postgres:/data/postgres
ports:
  - "5432:5432"
networks:
  - postgres
restart: unless-stopped
container_name: xx_postgres

pgadmin:
links:
  - postgres:postgres
image: fenglc/pgadmin4
volumes:
   - /data/pgadmin:/root/.pgadmin
ports:
  - "5050:5050"
networks:
  - postgres
restart: unless-stopped
container_name: xx_pgadmin


networks:
postgres:
driver: bridge

回答by Mark Reed

First, nothing you do in a docker execis persistent outside of that particular running container (copy of the image), so if you want future containers run from that image to include sudo, those apt-getcommands need to go into the Dockerfile that builds the image. Which, since you're using docker-compose, would require you to first make a Dockerfile and specify its location in the YAML.

首先,您在 a 中所做的任何事情都不会在docker exec该特定运行的容器(映像的副本)之外是持久的,因此如果您希望将来从该映像运行的容器包含sudo,则这些apt-get命令需要进入构建映像的 Dockerfile。由于您使用的是docker-compose,因此需要您首先创建一个 Dockerfile 并在 YAML 中指定其位置。

Second, what do you mean "exit back to docker"? Nothing you do inside a container is going to have any effect on the system that Docker itself is running on, but it looks like you're running software install commands inside a Docker container and then expecting that to result in the newly-installed software being available outside the container on the Windows system that is running Docker.

其次,“退出回到docker”是什么意思?您在容器内所做的任何事情都不会对运行 Docker 本身的系统产生任何影响,但看起来您正在 Docker 容器内运行软件安装命令,然后期望这会导致新安装的软件被在运行 Docker 的 Windows 系统上的容器外部可用。

回答by Alwinius

To do a backup of the postgres database in the container, you first have to enter the container (similar to how you do it):

要做容器中postgres数据库的备份,首先要进入容器(和你做的方式类似):

docker exec -it postgres bash

(substitude postgres with the real container name you get from docker-compose ps) Now you are in the container as root. That means, you don't need sudo for anything. Next create your backup folder:

(substitude postgres 与您从中获得的真实容器名称docker-compose ps)现在您以 root 身份在容器。这意味着,你不需要 sudo 任何东西。接下来创建您的备份文件夹:

mkdir /tmp/backup

Now run the backup command, from a quick Google I found the following (you might know better):

现在运行备份命令,从快速谷歌我发现以下(你可能更清楚):

pg_dumpall > /tmp/backup/filename

Then exit the shell within the container by typing exit. From your host system run the following to copy the backup file out of the container:

然后通过键入 exit 退出容器内的 shell。从您的主机系统运行以下命令以将备份文件复制出容器:

docker cp postgres:/tmp/backup/filename .

(postgres is your container name again)

(postgres 又是你的容器名)