以后如何附加到运行的Docker容器? [英] How do I attach to a running Docker container later?

查看:369
本文介绍了以后如何附加到运行的Docker容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想简单地启动一个docker容器来执行一些最终启动JBoss的java代码。

I want to simply start a docker container that executes some java code which ends up starting JBoss.

这是非常正常的,除非我无法弄清楚如何再次附加到容器并返回到bash提示符。

This is working fine except I can't figure out how to attach to the container again and get back to the bash prompt.

这是我如何启动我的容器:

This is how I start my container:

docker run -i -t -p 80:80 -v /tmp/automatefiles:/automatefromhost jboss bash -c 'cd automatefromhost; chmod 777 *.*; ./runAutomate.sh;'

这是runAutomate.sh

This is the runAutomate.sh

/usr/bin/java -cp Automate.jar -Djava.net.preferIPv4Stack=true net.Automate > automateresults &
tail -f automateresults

现在我必须在尾端做尾巴来保持容器运行完成后,我的自动化代码。
这样做的最终结果是Jboss正在运行我的应用程序配置正确。

Now I have to do the tail at the end to keep the container running after its finished running my Automate code. The end result of this is that Jboss is running with my app configured correctly.

现在,当我尝试再次附加到容器时,我只需要一个空白屏幕没有提示...并且无法回到容器内的提示。所以没有办法在容器开始后与容器进行交互。

Now when I try to attach to the container again I just get a blank screen with no prompt...and can't get back to the prompt within the container. So no way to interact with the container after it has started.

有关如何启动容器,保持运行,然后再附加到容器的任何想法,在提示下,执行 ls,tail等等。

Any ideas on how I can start the container, keep it running and then attach to the container later and be back in the prompt to do things like ls, tail etc .

编辑
我最终这样做:

EDIT: I ended up doing this:

我复制了这种方法: https://stackoverflow.com/a/20932423/1519407
并添加到我的脚本

I copied this approach : https://stackoverflow.com/a/20932423/1519407 and added to my script

while ( true )
    do
    echo "Detach with Ctrl-p Ctrl-q. Dropping to shell"
    sleep 1
    /bin/bash
done

这似乎还是有点黑客,但它的工作原理...我认为它可能更好的下去安装的路径ssh到容器上或使用类似 http://phusion.github.io/baseimage-docker/

This still seems kind of hacky but it works...I think its probably better to go down the path of installing ssh onto the container or using something like http://phusion.github.io/baseimage-docker/

推荐答案

只需输入代码字。

docker attach container_name

ctrl p ctrl q

exit命令停止容器,其中ctrlp和ctrl q只是分离该容器并保持运行

exit command stops the container, where as ctrlp and ctrl q just detaches that container and keeps it running

更新:对于那些不已经知道了,从docker 1.3版本开始,我们可以使用exec命令附加到容器并退出它,而不用麻烦。

Update: For those who don't know already, from docker 1.3 or so, we can use exec command for attaching to a container and exiting it without hassle.

eg: docker exec -it container_name bash

您可以在需要时输入exit,它将退出容器,并保持运行。

You can just type exit when needed, it will exit the container and still keeps it running.

这篇关于以后如何附加到运行的Docker容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆