[英]How to get docker container ID within Docker?
I am trying to follow the instructions here in order to run a Jupyter Notebook that's not part of a prebuilt Docker container.我正在尝试按照此处的说明运行一个 Jupyter Notebook,它不是预构建的 Docker 容器的一部分。
In order to get the contain id, I did this in Docker Quickstart Terminal when the notebook server is terminated with Ctrl + C (no response when the notebook server is running), I got为了获得包含 ID,当笔记本服务器用 Ctrl + C 终止(笔记本服务器运行时没有响应)时,我在 Docker 快速入门终端中执行了此操作,我得到了
$ docker ps -lq
0a8f14a15b4f
Then I did this然后我做了这个
$ docker exec -it 0a8f14a15b4f bash
Error response from daemon: Container 0a8f14a15b4f357352f3c40b6d449e1d9150a0ce79
5fd81c09c00e978ea86163 is not running
So, what is going on?那么发生了什么? How am I supposed to get the container id so that I can do something like below?我应该如何获取容器 ID 以便我可以执行以下操作?
docker exec -it [container-id] bash
Thanks!谢谢!
docker exec
will only work on a running container and ctrl - c will generally stop/kill a container. docker exec
仅适用于正在运行的容器,而ctrl - c通常会停止/终止容器。
A container can be run detached , the command then prints the new container ID to stdout.可以分离运行容器,然后该命令将新的容器 ID 打印到标准输出。
cid=$(docker run --detach <image>)
docker exec -ti $cid bash
If you still want to see the container output, use docker logs $cid
如果您仍想查看容器输出,请使用docker logs $cid
当容器终止时,只需执行此操作即可获取容器 ID:
$ docker ps -a
You can get container id inside a container using a python console running inside the particular container.您可以使用在特定容器内运行的 python 控制台获取容器内的容器 ID。 Basically container id is nothing but a socket hostname.基本上容器 id 只不过是一个套接字主机名。 You can obtain the same by following.您可以通过以下方式获得相同的内容。
import socket; print(socket.gethostname())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.