简体   繁体   中英

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.

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

$ 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?

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.

A container can be run detached , the command then prints the new container ID to stdout.

cid=$(docker run --detach <image>)
docker exec -ti $cid bash

If you still want to see the container output, use docker logs $cid

Give a name to the container using --name
docker run -d --name containername

And then use the name docker exec -it containername bash

Don't forget to run it with -d key or to run commands in the different terminals

当容器终止时,只需执行此操作即可获取容器 ID:

$ docker ps -a

You can get container id inside a container using a python console running inside the particular container. Basically container id is nothing but a socket hostname. You can obtain the same by following.

import socket; print(socket.gethostname())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM