简体   繁体   English

运行 ubuntu 16.04 bash shell 在 Z05B6053C41A2130AFD6BFCA3 中

[英]Run ubuntu 16.04 bash shell in a docker image

I have this Dockerfile:我有这个 Dockerfile:

FROM ubuntu:16.04

I run我跑

docker build -t mine .

It builds.它建立。 Then I run然后我跑

docker exec -it mine /bin/bash

and it says它说

Error: No such container: mine

I'm sure I'm missing something simple, but I've Googled for docker image name, I've run docker images and it shows the "CONTAINER ID" "mine" (with tag "latest"), I've looked at stackoverflow.我确定我遗漏了一些简单的东西,但我已经用 Google 搜索了 docker 图像名称,我运行docker images ,它显示了“CONTAINER ID”“我的”(带有“最新”标签),我看过在堆栈溢出。 This is so basic it's hard to find the answer.这是非常基本的,很难找到答案。

I'm running docker 19.03.2, build 6a30dfc, on OS X.我在 OS X 上运行 docker 19.03.2,构建 6a30dfc。

EDIT : If I run docker run mine it runs and immediately exits (of course, since that's what the Dockerfile says).编辑:如果我运行docker run mine的,它会运行并立即退出(当然,因为这就是 Dockerfile 所说的)。 Then there's a container (not running).然后是一个容器(未运行)。 Then if I exec that container by id, it says, "Container..hex.. is not running," which makes sense.然后,如果我通过 id 执行该容器,它会显示“Container..hex.. 未运行”,这是有道理的。

I guess I want the simplest way to run a bash shell in Ubuntu 16.04.我想我想要在 Ubuntu 16.04 中运行 bash shell 的最简单方法。

Advice?建议?

Docker exec command is for executing a command inside of a running container. Docker exec命令用于在正在运行的容器内执行命令。 You simply need to run your container using docker run -it mine /bin/bash .您只需使用docker run -it mine /bin/bash run您的容器。

If your ultimate goal is to run Ubuntu's bash on itself, you can skip the build phase and just do docker run -it ubuntu:16.04 /bin/bash .如果您的最终目标是自行运行 Ubuntu 的 bash,您可以跳过build阶段,只需执行docker run -it ubuntu:16.04 /bin/bash

Notice the -i and -t flags.注意-i-t标志。 The first one indicates that your containerized program (ie bash) may receive some user input.第一个表示您的容器化程序(即 bash)可能会收到一些用户输入。 Hence, docker will keep its stdin open.因此,docker 将保持其标准输入打开。 The second flag is to use a Linux pseudoterminal (PTY) as a controlling terminal of the command (ie bash).第二个标志是使用 Linux 伪终端(PTY)作为命令(即 bash)的控制终端。 And the combination of these flags allows you to have a normal interactive shell experience.并且这些标志的组合可以让你拥有正常的交互式 shell 体验。

The reason behinde this docker run mine command is, It will run the container but it will be terminated because it will not allocate pseudo-tty这个docker run mine命令背后的原因是,它将运行容器但它将被终止,因为它不会分配伪 tty

-t              : Allocate a pseudo-tty
-i              : Keep STDIN open even if not attached

For interactive processes (like a shell), you must use -i -t together in order to allocate a tty for the container process.对于交互式进程(如 shell),您必须一起使用 -i -t 才能为容器进程分配 tty。 -i -t is often written -it as you'll see in later examples. -i -t 通常写成 -it,正如您将在后面的示例中看到的那样。 Specifying -t is forbidden when the client is receiving its standard input from a pipe, as in:当客户端从 pipe 接收其标准输入时,禁止指定 -t,如下所示:

echo test | docker run -i busybox cat

so in your case you should allocate pseudo-tty for the ubuntu container.因此,在您的情况下,您应该为 ubuntu 容器分配伪 tty。

docker run --name my_ubuntu_container -it ubuntu bash

Then you can run然后你可以运行

--tty , -t      Allocate a pseudo-TTY
docker exec -it my_ubuntu_container bash

that because you need to run it first before using exec那是因为您需要在使用exec之前先运行它

docker run -it mine bash

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

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