我使用命令创建了一个容器
docker run ubuntu /bin/bash -c "echo 'cool content' > /tmp/cool-file"
Run Code Online (Sandbox Code Playgroud)
现在我看到容器已退出
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9e5017aef3f9 ubuntu "/bin/bash -c 'echo '" 38 seconds ago Exited (0) 36 seconds ago elegant_euler
Run Code Online (Sandbox Code Playgroud)
问题:如何使用其容器ID重新启动并进入此容器的交互模式?
我不能使用,docker run -it <image_name>因为这需要图像名称而不是容器ID.我尝试使用docker attach,但我认为这只适用于运行容器.我还不想提交这个容器,如何使用它的container-id重新启动并进入这个容器的交互模式?
编辑:我可以使用docker start {container-id}然后运行其他容器docker attach {container-id}.我想知道是否有一些特殊的东西,我创建容器的方式会导致这种行为.我刚开始使用码头工具,所以如果我错过了一些基本位,请指引我正确的方向.
gil*_*ile 13
容器在完成其命令时退出.所以容器开始了
docker run ubuntu /bin/bash -c "echo 'cool content' > /tmp/cool-file"
Run Code Online (Sandbox Code Playgroud)
命令echo完成后将立即退出.在这种情况下,重启该容器没有意义.
如果您以分离模式运行新容器,您将能够将其保持活动状态并在第二次附加它.
因此,在您的情况下,您应该以分离模式运行一个新容器运行一个命令/bin/bash,然后您可以运行回声并附加它
docker run -d -ti ubuntu /bin/bash
docker exec -ti <containerId> /bin/bash -c "echo 'cool content' > /tmp/cool-file"
Run Code Online (Sandbox Code Playgroud)
容器将保持活动状态,因此您可以在其上执行更多命令,例如docker exec -ti/bin/bash -c"cat/tmp/cool-file"
或运行一个新的/ bin/bash来"附加"你的容器,并作为命令提示符在其中工作
docker exec -ti <containerId> /bin/bash
root@<containerId>:/# cat /tmp/cool-file
cool content
Run Code Online (Sandbox Code Playgroud)
您可以成功停止/启动/重新启动此容器
docker stop <containerId> && docker start <containerId>
Run Code Online (Sandbox Code Playgroud)
要么
docker restart <containerId>
Run Code Online (Sandbox Code Playgroud)
提醒一下,当您重新启动容器时,它会再次执行其原始命令.因此,如果您能够重新启动用例的容器(但不是),它将再次运行/bin/bash -c "cat /tmp/cool-file"
重新启动使用命令/ bin/bash运行的容器,它将在重新启动时再次运行相同的命令.
重启现有容器时,通常无法将命令更改为RUN; 要做到这一点你可以尝试一些技巧,如如何使用不同的命令启动已停止的docker容器.
我试了一下:
docker restart <container_id>
docker exec -it <container_id> bash
Run Code Online (Sandbox Code Playgroud)
可以完美地重新启动并进入交互式终端。
| 归档时间: |
|
| 查看次数: |
24584 次 |
| 最近记录: |