天天看點

如何輸入已經以新的TTY運作的Docker容器

本文翻譯自:How to enter in a Docker container already running with a new TTY

I have a container that is running the Apache service in the foreground.

我有一個在前台運作Apache服務的容器。

I would like to be able to access the container from another shell in order to "poke around" inside it and examine the files.

我希望能夠從另一個外殼通路該容器,以便在其中“探查”并檢查檔案。

At the moment, if I attach to the container, I am left looking at the Apache daemon and cannot run any commands.

目前,如果我連接配接到容器,則隻能檢視Apache守護程式,并且無法運作任何指令。

Is it possible to attach another tty to a running container?

是否可以将另一個tty附加到正在運作的容器?

Possibly, I can take advantage of the fact that Docker is actually just wrapping around LXC containers?

可能,我可以利用Docker實際上隻是包裹LXC容器這一事實嗎?

I have tried

sudo lxc-console -n [container-id] -t [1-4]

but it appears that only one tty is made available and that is the one running the apache daemon.

我已經嘗試過

sudo lxc-console -n [container-id] -t [1-4]

但是看來隻有一個tty可用,而那是運作apache守護程式的那個。

Perhaps there is a way to enable multiple lxc consoles during the build?

也許有一種在建構過程中啟用多個lxc控制台的方法?

I would rather not configure and build the container with an openssh service if possible.

如果可能的話,我甯願不使用openssh服務配置和建構容器。

#1樓

參考:https://stackoom.com/question/1PpSf/如何輸入已經以新的TTY運作的Docker容器

#2樓

Update

更新資料

As of docker 0.9, for the steps below to now work, one now has to update the

/etc/default/docker

file with the

'-e lxc'

to the docker daemon startup option before restarting the daemon (I did this by rebooting the host).

從docker 0.9開始,為了使以下步驟正常運作,現在必須在重新開機守護程式之前将帶有

'-e lxc'

/etc/default/docker

檔案更新為docker守護程式啟動選項(我通過重新啟動主辦)。
如何輸入已經以新的TTY運作的Docker容器

This is all because...

這是因為...
...it [docker 0.9] contains a new "engine driver" abstraction to make possible the use of other API than LXC to start containers. ... [docker 0.9]包含新的“引擎驅動程式”抽象,以使可以使用LXC以外的其他API來啟動容器。 It also provide a new engine driver based on a new API library (libcontainer) which is able to handle Control Groups without using LXC tools. 它還提供了一個基于新API庫(libcontainer)的新引擎驅動程式,該驅動程式無需使用LXC工具即可處理控制組。 The main issue is that if you are relying on lxc-attach to perform actions on your container, like starting a shell inside the container, which is insanely useful for developpment environment... 主要問題是,如果您依靠lxc-attach在容器上執行操作,例如在容器内啟動外殼,這對于開發環境非常有用...

source

資源

Please note that this will prevent the new host only networking optional feature of docker 0.11 from "working" and you will only see the loopback interface.

請注意,這将阻止docker 0.11的新的僅主機聯網可選功能 “正常運作”,并且您隻會看到回送接口。

bug report

錯誤報告

It turns out that the solution to a different question was also the solution to this one:

事實證明, 另一個問題的解決方案也是這個問題的解決方案:
...you can use docker

ps -notrunc

to get the full lxc container ID and then use

lxc-attach -n <container_id>

run bash in that container as root. ...您可以使用

ps -notrunc

擷取完整的lxc容器ID,然後使用

lxc-attach -n <container_id>

在該容器中以root身份運作bash。

Update: You will soon need to use

ps --no-trunc

instead of

ps -notrunc

which is being deprecated.

更新:您很快将需要使用

ps --no-trunc

而不是已棄用的

ps -notrunc

如何輸入已經以新的TTY運作的Docker容器

Find the full container ID

查找完整的容器ID
如何輸入已經以新的TTY運作的Docker容器

Enter the lxc attach command.

輸入lxc attach指令。
如何輸入已經以新的TTY運作的Docker容器

Top shows my apache process running that docker started.

頂部顯示了運作Docker啟動的我的Apache程序。

#3樓

The "nsinit" way is:

“ nsinit”的方式是:

install nsinit

安裝nsinit
git clone [email protected]:dotcloud/docker.git
cd docker
make shell
           

from inside the container:

從容器内部:
go install github.com/dotcloud/docker/pkg/libcontainer/nsinit/nsinit
           

from outside:

從外面:
docker cp id_docker_container:/go/bin/nsinit /root/
           

use it

用它
cd /var/lib/docker/execdriver/native/<container_id>/
nsinit exec bash
           

#4樓

You should use Jérôme Petazzoni's tool called 'nsenter' to enter a container without using SSH.

您應該使用JérômePetazzoni的名為“ nsenter”的工具來輸入容器,而無需使用SSH。

See: https://github.com/jpetazzo/nsenter

參見: https : //github.com/jpetazzo/nsenter

Install with simply running:

docker run -v /usr/local/bin:/target jpetazzo/nsenter

隻需運作即可安裝:

docker run -v /usr/local/bin:/target jpetazzo/nsenter

Then use the command

docker-enter <container-id>

to enter the container.

然後使用指令

docker-enter <container-id>

進入容器。

#5樓

What about running tmux/GNU Screen within the container?

在容器中運作tmux / GNU Screen怎麼辦?

Seems the smoother way to access as many vty as you want with a simple:

似乎更簡單的方法是通過一個簡單的方法來通路任意數量的vty:
$ docker attach {container id}
           

#6樓

nsenter

does that.

nsenter

做到了。

However I also needed to enter a container in a simple way and nsenter didn't suffice for my needs.

但是,我還需要以一種簡單的方式輸入一個容器,nsenter不足以滿足我的需求。

It was buggy in some occasions (black screen plus -wd flag not working).

在某些情況下,這是錯誤的(黑屏加上-wd标志不起作用)。

Furthermore I wanted to login as a specific user and in a specific directory.

此外,我想以特定使用者身份和特定目錄登入。

I ended up making my own tool to enter containers.

我最終制作了自己的工具來輸入容器。

You can find it at: https://github.com/Pithikos/docker-enter

您可以在以下位置找到它: https : //github.com/Pithikos/docker-enter

Its usage is as easy as

它的用法就像
./docker-enter [-u <user>] [-d <directory>] <container ID>