linux 中 控制台 怎样和 docker 容器交互
发布网友
发布时间:2022-05-24 19:58
我来回答
共1个回答
热心网友
时间:2023-10-29 10:15
n previous articles [1,2,3] Lukas Pustina has given an introction to system-level virtualization with Docker. In this article I discuss 4 options how to connect to a running Docker container and interact with it. All code examples are available on GitHub so you can try yourself.
nsenter
The nsenter tool is part of the util-linux package since version 2.23. It provides access to the namespace of another process. nsenter requires root privileges to work properly. Unfortunately, util-linux is still at version 2.20 in Ubuntu 14.04. To install the latest version (2.24) proceed as follows:
cd /tmp
curl https://www.kernel.org/pub/linux/utils/util-linux/v2.24/util-linux-2.24.tar.gz | tar -zxf-cd util-linux-2.24./configure --without-ncursesmake nsentercp nsenter /usr/local/bin
In order to connect to a container, you have to find out the PID of the first process in the container.
docker inspect --format "{{ .State.Pid }}" <container-id>
With that PID you can connect to the container:
nsenter --target $PID --mount --uts --ipc --net --pid