How to write docker private registry reverse proxy via golang?

Viewed 49

I'm trying to create a reverse proxy in golang for my private registry, however the following snippet has unexpected behavior:

package main

import (
    "fmt"
    "log"
    "net/http"
    "net/http/httputil"
    "net/url"
)

func main() {
    http.HandleFunc("/", ServeHTTP)
    http.ListenAndServe("0.0.0.0:9090", nil)
}

func ServeHTTP(w http.ResponseWriter, r *http.Request) {
    fmt.Printf(r.URL.Path + "\n")
    remote, err := url.Parse("http://localhost:10000")
    if err != nil {
        log.Fatal(err)
    }
    proxy := httputil.NewSingleHostReverseProxy(remote)
    proxy.ServeHTTP(w, r)
}

I used docker run -d -p 10000:5000 -v docker_image:/var/lib/registry\ registry to run a private docker registry container.

After running the code, I used curl 127.0.0.1:9090/v2/_catalog and I got the correct answer, but when I used docker push 127.0.0.1:9090/hello-world:latest (after running docker tag), the docker client threw Get "http://127.0.0.1:9090/v2/": dial tcp 127.0.0.1:9090: connect: connection refused.

I checked the logs and it seems that my code does not even get the http request.

What could I do to achieve the desired result?

0 Answers
Related