Coder Social home page Coder Social logo

autoportforward's Introduction

Auto-portforward (apf)

A handy tool to automatically set up proxies that expose the remote container's listening ports back to the local machine. Just like kubectl portforward or docker run -p LOCAL:REMOTE, but automatically discover and update the ports to be forwarded on the fly. apf can create listening ports in the container and forward them back as well.

I often find myself forgetting to add -p option when testing a docker image or missing to expose some other ports. Now I don't need to worry about that, I just run the following commands:

$ docker run -d --name redis redis

$ apf redis

*  ==> : Forwarding local listening ports to (==>) remote ports
*  <== : Forwarding to local ports from (<==) remote listening ports (use -r option)

Forwarding: [6379 ==> 6379]

apf will update the port list on the fly. So if you login to the container and start other server listening on different ports, it will dynamically update the local listeners.

For Kubernetes (hey, if you haven't heard of telepresence , please give it a try, it's way cooler than this project.):

$ kubectl run --image redis redis

$ apf -k default/redis
Forwarding: [6379 ==> 6379]

For Podman:

$ podman run --name redis docker.io/library/redis:latest

$ apf -p redis
Forwarding: [6379 ==> 6379]

Installation

First of all, apf requires a working docker / kubectl client setup, the client can connect to either local docker daemon / k8s cluster or remote.

You can either download the binary from the release artifacts or build it yourself.

# MacOS (Intel)
curl -L -O https://github.com/ruoshan/autoportforward/releases/latest/download/apf-mac
chmod +x apf-mac
mv apf-mac /usr/local/bin/apf

# MacOS (ARM)
curl -L -O https://github.com/ruoshan/autoportforward/releases/latest/download/apf-mac-arm64
chmod +x apf-mac-arm64
mv apf-mac-arm64 /usr/local/bin/apf

# Linux
curl -L -O https://github.com/ruoshan/autoportforward/releases/latest/download/apf-linux-x64
chmod +x apf-linux-x64
mv apf-linux-x64 /usr/local/bin/apf

To manually build it, clone the repo and run the build.sh script.

Usage

Expose all the listening ports in the container back to the local machine

# Docker
apf {container ID / name}

# Kubernetes
apf -k {namespace}/{pod name}

# Podman
apf -p {podman container ID / name}

Also expose local ports (8080,9090) to the container

# Docker
apf -r 8080,9090  {container ID / name}

# Kubernetes
apf -r 8080,9090 -k {namespace}/{pod name}

# Podman
apf -r 8080,9090 -p {podman container ID / name}

Limitations

  • Currently, apf only supports containers of the same CPU arch of your host machine. For other arch, you can do a custom build by tweaking the build.sh script.
  • For Kubernetes, the container must have tar installed.
  • If the container is run with readonly rootfs, apf won't work. (apf needs to copy a guest agent into the container)

Tips

  1. apf does not come with shell completion, but here is what I do to make it more handy:
# `brew install fzf`
alias ap='docker ps | grep -v "^CONTAINER ID" | fzf | awk "{print \$1}" | xargs -n 1 apf'

autoportforward's People

Contributors

l4zy0n3 avatar milanvives avatar ruoshan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

autoportforward's Issues

Cant run apf on n Apple Silicon M1

Hello
I have mac on Apple Silicon M1. When I try to run apf I get en error

apf -r 9003 -k namespace/pod

  • ==> : Forwarding local listening ports to (==>) remote ports
  • <== : Forwarding to local ports from (<==) remote listening ports (use -r option)

panic: Failed to establish manager stream: EOF

goroutine 1 [running]:
main.main()
/home/runner/work/autoportforward/autoportforward/cmd/apf/apf.go:123 +0xe8c

Any ideas on how to fix this?

Not working with Docker per example

Running this per the documentation does not seem to work.

$ apf redis          

*  ==> : Forwarding local listening ports to (==>) remote ports
*  <== : Forwarding to local ports from (<==) remote listening ports (use -r option)

No ports are opened.

I have tried builds from commit 38558a1 and b8059ea. I've tried the -d option, but the log file is empty

Docker version 20.10.12, build e91ed5707e
Linux archlinux 5.15.10-arch1-1 #1 SMP PREEMPT Fri, 17 Dec 2021 11:17:37 +0000 x86_64 GNU/Linux

Let me know if you need any other information

Change default host port for webserver containers from 10080 to something under 10K

For some reason most browsers seem to block webservers on ports higher than 10K.
Apf now by default maps all server containers (80) to host port 10080. I tried Nginx and httpd.

Maybe change the default port to something like 9080 or 8080 to avoid this problem.

Docker ➤ docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
c88969704c48   nginx     "/docker-entrypoint.…"   15 minutes ago   Up 15 minutes   80/tcp    nginx
Docker ➤ apf nginx

*  ==> : Forwarding local listening ports to (==>) remote ports
*  <== : Forwarding to local ports from (<==) remote listening ports (use -r option)

Forwarding: [10080 ==> 80]

Get request from Netcat. Website root (Nginx default page) is loaded.

Docker ➤ nc localhost 10080
GET / HTTP/1.2
Host: localhost

HTTP/1.1 200 OK
Server: nginx/1.21.4
Date: Thu, 10 Feb 2022 09:28:03 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 02 Nov 2021 14:49:22 GMT
Connection: keep-alive
ETag: "61814ff2-267"
Accept-Ranges: bytes

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
Docker ➤

Browser
Screenshot 2022-02-10 at 10 30 59

apf exits after startup

if container use some common port which already used in the host node, such as ssh(22), http(80)
the apf-agent may panic
image

Invalid memory address or nil pointer dereference on M1

~ ➤ docker run -d --name redis redis
ae44475c8835158e4c1d36406de38048b3b797e49f5f86a80e23f3e330b89c9f
~ ➤ apf redis

*  ==> : Forwarding local listening ports to (==>) remote ports
*  <== : Forwarding to local ports from (<==) remote listening ports (use -r option)

Forwarding: []panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x18 pc=0x102cdff34]

goroutine 8 [running]:
io.ReadAtLeast({0x0, 0x0}, {0x140000ae00a, 0x3, 0x3}, 0x3)
	/opt/hostedtoolcache/go/1.17.5/x64/src/io/io.go:328 +0x74
io.ReadFull(...)
	/opt/hostedtoolcache/go/1.17.5/x64/src/io/io.go:347
github.com/ruoshan/autoportforward/manager.(*Manager).receivingLoop(0x14000066000)
	/home/runner/work/autoportforward/autoportforward/manager/manager.go:86 +0xb8
created by github.com/ruoshan/autoportforward/manager.(*Manager).Run
	/home/runner/work/autoportforward/autoportforward/manager/manager.go:74 +0x54
~ ➤

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.