请教各位达人一个关于docker的问题哈。我想在docker run的时候,用-d参数运行/bin/bash,然后让这个bash一直跑着,我要用的时候就docker attach上去。但是我一跑docker run -d .... /bin/bash,这个容器就运行结束停下来了。这是为毛啊,有啥解决方案没。。。
PHPz2017-04-21 10:57:35
I also encountered this problem myself. But in a different situation, I hope that when docker runs, the container can always run in the background and allow me to log in remotely to operate.
I copied the article: SSH remote login to a container
----Content begins-----
After starting a container, we may need to log in using ssh to perform some operations. To achieve this goal, there are 2 points that need to be ensured:
注意:以下示例是在ubuntu/13.10中完成的
First make sure the image has openssh-server
service
# 官方镜像一般没有安装ssh服务端,so,需要安装一个
apt-get install openssh-server
# 设置一个初始密码
passwd 123
...
Then, submit the image:
docker commit CONTAINER_ID NAME/VERSION
Finally, run the image and let the generated container run in the background:
# 第一个-d表示让容器在后台运行
# 末尾的-D表示启动ssh的daemon模式,不然容器启动后立刻就变为停止状态了
docker run -d NAME/VERSION /etc/init.d/ssh start -D
Now, you can log into the container via ssh.
# 查询容器IP
docker inspect CONTAINER_ID | grep IP
# 登入
ssh root@IP
# 输入密码 123完成登陆
# 为保证安全,请即刻用passwd修改密码
ringa_lee2017-04-21 10:57:35
docker run
指定的命令如果不是那些一直挂起的命令(比如运行top
,不断echo
),就是会自动退出的。-d
命令是设置detach为true,根据官方的文档,意思是让这个命令在后台运行,但并不是一直运行(我们在一个正常的Linux Terminal中运行/bin/bash
,运行完了也就完了,不会一直挂着等待响应的,所以确实没办法用daemon方式来跑/bin/bash
).
There are indeed some inconsistencies between the early and current official documents here. Now it is detach. The early documents say that -d is specified to run the container in daemon mode. There may be some misunderstandings.
In addition, if you need to run bash in the container, just run docker run -i -t CONTAINER_NAME /bin/bash
directly. If you think there are more parameters than docker attach, you can set an alias to solve the problem:
alias dockerbash='docker run -i -t CONTAINER_ID /bin/bash'
After setting the alias, run dockerbash
directly to enter the bash of the container.
PHPz2017-04-21 10:57:35
It can be achieved using supervisor. And you can start multiple services at the same time.
First install the software package with yum -y install supervisor and modify the configuration file /etc/supervisord.conf
Add the services you want to start, such as sshd.
For specifics, you can refer to this article: http://openstack.blog.163.com/blog/static/236387267201491734019283/
天蓬老师2017-04-21 10:57:35
docker run --attach=stdin -d image bash, the -d parameter turns off stdin by default.
怪我咯2017-04-21 10:57:35
It is recommended to add a sentence to the Dockerfile file for building the image:
CMD tail -f
If there are other commands
CMD other commands && tail -f
I hope it will be helpful to you