Unable to ssh to Docker instance using `docker attach <name>`

Viewed 27

I am following this blog on how to connect to a docker instance: https://phoenixnap.com/kb/how-to-ssh-into-docker-container. It mentions using docker attach <name>

Trying this on my ec2 instance gives us:

$ docker ps
CONTAINER ID        IMAGE                             COMMAND                  CREATED              STATUS                  PORTS                     NAMES
849844c1e3a5        6501862...us-east-618356524   "docker-entrypoint.s…"   About a minute ago   Up About a minute       0.0.0.0:32788->8401/tcp   ecs-prod-clia-lab-5-Applicationprodclia-lab-8c88d2e0bc83cfb1230

Now let's try to `docker attach <instance-name>

$ docker attach ecs-prod-clia-lab-5-Applicationprodclia-lab-8c88d2e0bc83cfb1230
Error: No such container: ecs-prod-clia-lab-5-Applicationprodclia-lab-8c88d2e0bc83cfb1230

So that actually does not work? What is the correct way to do this?

1 Answers

To get a shell in a running container, do this:

$ docker exec -it <container-id> /bin/sh

The attach sub-command gives you access to a running containers stdout. That's not what you want here

However, if your conainer is meant to provide SSH as a service, you'll need to run it in such a way that it's exposed on the host, on some available port (like 2222).

The you'd simply "SSH in" like this:

$ ssh 127.0.0.1 -p 2222
Related