Docker 如何使用容器id重新启动容器?
我使用命令创建了一个容器Docker 如何使用容器id重新启动容器?,docker,Docker,我使用命令创建了一个容器 docker run ubuntu /bin/bash -c "echo 'cool content' > /tmp/cool-file" 现在我看到容器已退出 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9e5017aef3f9
docker run ubuntu /bin/bash -c "echo 'cool content' > /tmp/cool-file"
现在我看到容器已退出CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9e5017aef3f9 ubuntu "/bin/bash -c 'echo '" 38 seconds ago Exited (0) 36 seconds ago elegant_euler
问题:如何使用容器id重新启动并进入该容器的交互模式?我不能使用
docker run-it
,因为这需要图像名而不是容器id。
我尝试使用docker attach,但我认为这只适用于运行容器。
我还不想提交此容器,因此,如何使用其容器id重新启动并进入此容器的交互模式?编辑:我可以使用
docker start{container id}
进入其他容器,然后运行docker attach{container id}
。我想知道我创建容器的方式是否有导致这种行为的特殊之处。我刚从docker开始,如果我缺少一些基本的东西,请告诉我正确的方向。检查
我试过自己:
docker restart <container_id>
docker exec -it <container_id> bash
docker重启
docker exec-it bash
它可以完美地重新启动并进入交互式终端 容器在完成其命令时退出。因此,容器从
docker run ubuntu /bin/bash -c "echo 'cool content' > /tmp/cool-file"
将在命令echo
完成后立即退出。在这种情况下,重新启动该容器是没有意义的
如果您在分离模式下运行一个新容器,您将能够使它保持活动状态,并在第二次连接它
因此,在您的情况下,您应该在分离模式下从映像运行一个新容器,运行类似于/bin/bash
的命令,然后您可以运行echo并附加它
docker run -d -ti ubuntu /bin/bash
docker exec -ti <containerId> /bin/bash -c "echo 'cool content' > /tmp/cool-file"
docker run-d-ti ubuntu/bin/bash
docker exec-ti/bin/bash-c“echo'cool content'>/tmp/cool file”
容器将保持活动状态,因此您可以在其上执行更多命令,例如。
docker exec-ti/bin/bash-c“cat/tmp/cool文件”
或者运行一个新的/bin/bash来“附加”您的容器,并作为命令提示符在其中工作
docker exec -ti <containerId> /bin/bash
root@<containerId>:/# cat /tmp/cool-file
cool content
docker exec-ti/bin/bash
root@:/#cat/tmp/cool文件
酷内容
您可以成功地停止/启动/重新启动此容器
docker stop <containerId> && docker start <containerId>
docker停止和docker启动
或
docker重启
提醒您,当您重新启动容器时,它会再次执行其原始命令。因此,如果您能够重新启动用例的容器(但您没有),它将再次运行/bin/bash-c“cat/tmp/cool file”
重新启动使用命令/bin/bash运行的容器时,它将在重新启动时再次运行相同的命令
在重新启动现有容器时,通常无法更改要运行的命令;要做到这一点,你可以尝试一些技巧建议在 不是最好的解决方案,但我正在使用它来管理docker映像。请查看此文档-对于短时间运行的进程,您可能无法复制此文档。在上面的dupe中,处理容器是否在可以对其使用
docker exec
之前退出。此外,docker start命令没有-t标志。此外,为了更精确,我还对我的问题进行了编辑。
docker stop <containerId> && docker start <containerId>
docker restart <containerId>