简体   繁体   English

Docker-ssh按容器名称

[英]Docker - ssh by container name

I want to easy ssh to docker container by container name. 我想通过容器名称轻松SSH到docker容器。 Now to ssh to container i need to call: 现在要SSH到容器,我需要调用:

docker ps

which returns: 返回:

CONTAINER ID        IMAGE
<container_id>        myContainer

and copy CONTAINER_ID to execute command: 并复制CONTAINER_ID以执行命令:

docker exec -ti <container_id> /bin/bash/

I have many containers and it will be much easier to ssh by IMAGE name. 我有很多容器,按IMAGE名称进行ssh将更加容易。 Is it possible without writting custom bash script? 是否可以不编写自定义bash脚本?

In general, as long as you don't install the SSH client inside every container no, it is not possible to "ssh" inside a container. 通常,只要不在每个容器中都没有安装SSH客户端,就不可能在容器中“ ssh”。

Please note that you can use container name instead of container id to exec a command (/bin/bash in your case) into a running container. 请注意,您可以使用container name而不是container id来对正在运行的容器执行命令(在您的情况下为/ bin / bash)。

For example, given a container like this: 例如,给定这样的容器:

$ docker ps
CONTAINER ID        IMAGE                                   COMMAND                  CREATED             STATUS              PORTS                              NAMES
5b3068b4e51c        registry_registry                       "/entrypoint.sh /e..."   4 months ago        Up 2 hours          0.0.0.0:5000->5000/tcp             registry_registry_1

Running the following 2 command leads to the same result: 运行以下2条命令会得到相同的结果:

docker exec -ti 5b3068b4e51c /bin/bash
docker exec -ti registry_registry_1 /bin/bash

Furthermore, again in general, you could have more that one container running for the same image, thus what you want to achive (enter a container by image name) isn't much safe. 此外,通常,对于同一图像,您可以有多个容器运行,因此要达到的目标(通过图像名称输入容器)并不十分安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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