Allow a user to bind to a unix socket

pull/2/head
Michael Crosby 2013-12-30 10:40:08 -08:00
parent efd58156f7
commit 75d5bd1217
3 changed files with 77 additions and 27 deletions

View File

@ -1,25 +1,16 @@
# Dockerfile for DockerUI
FROM ubuntu:12.04 FROM ubuntu:12.04
MAINTAINER Michael Crosby http://crosbymichael.com
RUN apt-get update RUN apt-get update
RUN apt-get upgrade -y RUN apt-get upgrade -y
RUN apt-get install -y curl ;\ RUN apt-get install -y curl
curl -s https://go.googlecode.com/files/go1.1.2.linux-amd64.tar.gz | tar -v -C /opt -xz ;\ RUN curl -s https://go.googlecode.com/files/go1.2.linux-amd64.tar.gz | tar -v -C /opt -xz ;\
cp -a /opt/go/* /usr/local/ ;\ cp -a /opt/go/* /usr/local/ ;\
rm -rf /opt/go ;\ rm -rf /opt/go;
#RUN
ENV GOROOT /usr/local/ ENV GOROOT /usr/local/
ADD . /app/ ADD . /app/
WORKDIR /app/ WORKDIR /app/
RUN go build dockerui.go RUN go build dockerui.go
EXPOSE 9000 EXPOSE 9000
ENTRYPOINT ["./dockerui"] ENTRYPOINT ["./dockerui"]

View File

@ -37,6 +37,13 @@ DockerUI is a web interface to interact with the Remote API. The goal is to pro
5. Everything should be good to go, if you experience any issues please report them on this repository. 5. Everything should be good to go, if you experience any issues please report them on this repository.
### Connect via a unix socket
If you want to connect to docker via the unix socket you can pass the socket path to the `-e` variable. If you are running dockerui in a container you can bind mount the unix socket into the container.
```bash
docker run -d -p 9000:9000 -v /var/run/docker.sock:/docker.sock dockerui -e /docker.sock
```
### Stack ### Stack
* Angular.js * Angular.js
* Flatstrap ( Flat Twitter Bootstrap ) * Flatstrap ( Flat Twitter Bootstrap )

View File

@ -2,10 +2,13 @@ package main
import ( import (
"flag" "flag"
"io"
"log" "log"
"net"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
"net/url" "net/url"
"strings"
) )
var ( var (
@ -14,20 +17,69 @@ var (
assets = flag.String("a", ".", "Path to the assets") assets = flag.String("a", ".", "Path to the assets")
) )
func createHandler(dir string, dockerEndpoint string) http.Handler { type UnixHandler struct {
mux := http.NewServeMux() path string
}
fileHandler := http.FileServer(http.Dir(dir)) func (h *UnixHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
u, err := url.Parse(dockerEndpoint) conn, err := net.Dial("unix", h.path)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Println(err)
return
}
c := httputil.NewClientConn(conn, nil)
defer c.Close()
res, err := c.Do(r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Println(err)
return
}
defer res.Body.Close()
copyHeader(w.Header(), res.Header)
if _, err := io.Copy(w, res.Body); err != nil {
log.Println(err)
}
}
func copyHeader(dst, src http.Header) {
for k, vv := range src {
for _, v := range vv {
dst.Add(k, v)
}
}
}
func createTcpHandler(e string) http.Handler {
u, err := url.Parse(e)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
return nil
} }
reverseProxy := httputil.NewSingleHostReverseProxy(u) return httputil.NewSingleHostReverseProxy(u)
}
mux.Handle("/dockerapi/", http.StripPrefix("/dockerapi", reverseProxy)) func createUnixHandler(e string) http.Handler {
return &UnixHandler{e}
}
func createHandler(dir string, e string) http.Handler {
var (
mux = http.NewServeMux()
fileHandler = http.FileServer(http.Dir(dir))
h http.Handler
)
if strings.Contains(e, "http") {
h = createTcpHandler(e)
} else {
h = createUnixHandler(e)
}
mux.Handle("/dockerapi/", http.StripPrefix("/dockerapi", h))
mux.Handle("/", fileHandler) mux.Handle("/", fileHandler)
return mux return mux
} }