domandinho
domandinho

Reputation: 1320

Docker - ssh by container name

I want to easy ssh to docker container by container name. Now to ssh to container i need to call:

docker ps

which returns:

CONTAINER ID        IMAGE
<container_id>        myContainer

and copy CONTAINER_ID to execute command:

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

I have many containers and it will be much easier to ssh by IMAGE name. Is it possible without writting custom bash script?

Upvotes: 1

Views: 943

Answers (1)

whites11
whites11

Reputation: 13260

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.

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.

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:

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.

Upvotes: 2

Related Questions