Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing forwarded port from Docker container #14

Open
maxispeicher opened this issue Sep 17, 2021 · 12 comments
Open

Accessing forwarded port from Docker container #14

maxispeicher opened this issue Sep 17, 2021 · 12 comments

Comments

@maxispeicher
Copy link

Hello,

I am using the PortForwardingSession to access some services in our AWS account. When using it locally everything works fine and I have access via localhost:<port>. However, inside a docker container I'm not able to access the same port. At the same time I am able to access a different port with a local web server without any issues. So the problem only seems to exist in combination of SSM and Docker.
When using nmap from inside the container it also shows the port used for the forwarding session as closed.

docker --version -> 20.10.8
session-manager-plugin --version -> 1.2.245.0

running on Ubuntu 20.04.3 LTS

Do you know any reason why the SSM port behaves differently in comparison to e.g. a webserver.

Many Thanks!

@YujiaozhAws
Copy link
Contributor

Thanks for reaching out! Could you please provide below information to investigate this issue further?

  1. Is only one local port not accessible or are you not able to successfully use Session Manager inside the container?
  2. Provide a few lines of debug log of Session Manager plugin.
  3. Is there any port mapping for the container?

Reference:
https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-install-plugin.html#install-plugin-configure-logs

@sanjeevik89
Copy link

I am seeing the same problem that @maxispeicher raised in this issue.

  1. Port Forwarding Session Manager is working inside container.
  2. Attached the logs for reference.
  3. port mapping is set for 8080.
    sessionmanager.txt

@maxispeicher
Copy link
Author

Sorry for the late reply, but I kind of managed to "fix" it. However, the solution is not optimal. After changing the line 127.0.0.1 localhost to 0.0.0.0 localhost in /etc/hosts the forwarding also works inside the docker container.

@krystian-panek-vmltech
Copy link

also struggling with that problem but I am afraid to correct that /etc/hosts because I have some other software on that particular container on which I am using SSM

how about just making configurable also that localhost here? localHost param?

if listener, err = getNewListener("tcp", "localhost:"+portNumber); err != nil {

to be able to put there 0.0.0.0 ?:)

@dougmoscrop
Copy link

Hopefully the PR that fixes this will be merged soon, but in the meantime, if you're (rightfully) avoiding modifying /etc/hosts you could also use socat. If you have an ECS Task listening on port 8000, and want to connect to that from your host machine on port 8000, via a Docker-run SSM port forwarding session, it looks something like this:

docker (compose) run -p 8000:8000 [...] socat TCP-LISTEN:8000,fork,reuseaddr TCP:127.0.0.1:8081 & aws ssm start-session [...] --document-name AWS-StartPortForwardingSession --parameters '{"portNumber":["8000"],"localPortNumber":["8001"]}'

where [...] is your image that has the AWS CLI, SSM Plugin, and socat installed, AWS region/profile/target, etc.

@krystian-panek-vmltech
Copy link

krystian-panek-vmltech commented May 23, 2023

yes I have used socat as workaround ;) @dougmoscrop

@anas-zurkiyeh-sp
Copy link

Any insight on when this will be fixed?

@josealdaco
Copy link

Any updates on the fix? :)

@fneco
Copy link

fneco commented Jun 21, 2024

I don't know if this is related, but this works for me.

docker run -it --rm  \
  -e "PGPASSWORD=${DB_PASSWORD}" \
  postgres:16.3 psql -h host.docker.internal -p 5432 -U foo -d bar

The key is to specify host.docker.internal as the host.

@MikeJansen
Copy link

I have begun work on a PR for this. If anyone that has worked on this plugin can give me a jump start on understanding the networking aspect of the plugin, that would help.

Without delving too much into the big picture, I made a first pass attempt to get it working simply by overriding the hostname for the listener. See change.

The listener listens on the specified interface, port. I'll add more info later with details of what I'm encountering. I'm hoping to make initial contact with a maintainer at this point.

@audrey-roe
Copy link

audrey-roe commented Jul 17, 2024

If you've tried the solutions mentioned above without success, here's another approach that worked for me:

  1. Check if you have PostgreSQL running locally on your machine.
  2. If so, try stopping it. In my case, I had PostgreSQL 14 installed via Homebrew.
  3. To stop the local PostgreSQL service, I ran:
    brew services stop postgresql
  4. After stopping the local service, I retried connecting to the Docker PostgreSQL instance, and it worked.

Note: Changing 127.0.0.1 localhost to 0.0.0.0 localhost in /etc/hosts also worked for me, but it's not recommended as it can potentially cause other issues.

For more information on this approach, see this StackOverflow post.

Hopefully, this helps someone facing a similar issue!

@thiagomarafeli
Copy link

I'm running through the same problem here! It's painful to use a non-oficial solution just to get around that...

Any updates on this?? This would be a major and important feature

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests