2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2020

08/24/2015: Go Program to Read Docker Image List From Unix Socket (/var/run/docker.sock)

It took me a bit of time to get this simple program working so I'm sharing for other people new to Go.



package main

import (
    "fmt"
    "io"
    "net"
)

func reader(r io.Reader) {
    buf := make([]byte, 1024)
    for {
        n, err := r.Read(buf[:])
        if err != nil {
            return
        }
        println(string(buf[0:n]))
    }
}

func main() {
    c, err := net.Dial("unix", "/var/run/docker.sock")
    if err != nil {
        panic(err)
    }
    defer c.Close()

    fmt.Fprintf(c, "GET /images/json HTTP/1.0\r\n\r\n")

    reader(c)
}

04/24/2015: Running the NodeJS Example Inside Docker Container

Yesterday, I showed how to run NodeJS inside a Docker container. Today, I updated my Github project (https://github.com/medined/docker-nodejs) so that the Example server works correctly.

The trick is for the NodeJS code inside the container to find the container's IP address and listen on that address instead of localhost or 127.0.0.1. This is not difficult.



 require('dns').lookup(require('os').hostname(), function (err, add, fam) {
  var http = require('http');
  http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
  }).listen(1337, add);
  console.log('Server running at http://' + add + ':1337/');
})

If you're using my Docker image, then you'd just run the following to start the server. Use ^C to stop the server.



node example.js


Now you can browse from the host computer using the following URL. Note that the 'docker run' command exposes port 1337.



http://localhost:1337/