2015年6月20日 星期六

[ 常見問題 ] How to get bash\ssh inside runned container (run -d)?

Source From Here
Question
I want to ssh or bash into running docker container. Please, see example:
$ sudo docker run -d webserver
webserver is clean image from ubuntu:14.04
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
665b4a1e17b6 webserver:latest /bin/bash ... ... 22/tcp, 80/tcp loving_heisenberg

Now I want to get something like this (go into running container):
$ sudo docker run -t -i webserver # or maybe 665b4a1e17b6 instead
$ root@42f1e37bd0e5:/# // However when I run the line above I get new CONTAINER ID

Previously I used Vagrant so I want to get behavior similar to vagrant ssh. Please, could anyone help me?

How-To
The answer is docker's attach command. So for my example above the solution will:
$ sudo docker attach 665b4a1e17b6 #by ID
or
$ sudo docker attach loving_heisenberg #by Name
$ root@665b4a1e17b6:/#

UPDATE: (docker >= 1.3) Thanks to WiR3D user who suggested another way to get container's shell. If we use attach we can use only one instance of shell. So if we want open new terminal with new instance of container's shell, we just need run the following:
$ sudo docker exec -i -t 665b4a1e17b6 bash #by ID
or
sudo docker exec -i -t loving_heisenberg bash #by Name
$ root@665b4a1e17b6:/#


沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...