Skip to content

Instantly share code, notes, and snippets.

@smagch
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save smagch/2393330ae8a9d8b03ae0 to your computer and use it in GitHub Desktop.

Select an option

Save smagch/2393330ae8a9d8b03ae0 to your computer and use it in GitHub Desktop.
Link Docker container example between golang1.3.3-onbuild and postgres:9.3.5 with Docker v1.2.0
.git
README.md

Link Docker container example

This example illustrates how to link Docker containers with following images. Docker version is 1.2.0.

  • golang1.3.3-onbuild
  • postgres:9.3.5

How to start

If you are on Mac OSX, follows the next Boot2Docker section.

Boot2Docker

As described on here, you should configure your network before you start a boot2docker VM.

$ boot2docker init
$ ./for-boot2docker.sh
$ boot2docker up

Run Postgres

You can pull official postgres and run it simply.

$ docker run --name some-postgres -d postgres:9.3.5

You'll be able to see postgres with docker ps.

Run the sample Go app

At first, you need to build the app to an image.

$ docker build --tag="test:1.0" .

Since you started a postgres container, you can link that with --link flag.

$ docker run -p 49153:8080 --name some-app --link some-postgres:postgres -d test:1.0

The docker ps will show something similar to below.

$ docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                     NAMES
f87470a6c176        test:1.0            "go-wrapper run"       22 minutes ago      Up 22 minutes       0.0.0.0:49153->8080/tcp   some-app
32cc507dce51        postgres:9.3.5      "/docker-entrypoint.   26 minutes ago      Up 26 minutes       5432/tcp                  some-app/postgres,some-postgres

You can see the app with curl.

$ curl http://localhost:49153/

Or if you are using boot2docker with Mac OSX, You need to know to IP address that boot2docker VM is on.

$ boot2docker ip # it'll show ip address
$ # assume the IP address is 192.168.59.103
$ curl http://192.168.59.103:49153/
FROM golang:1.3.3-onbuild
EXPOSE 8080
package main
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
"log"
"net/http"
"os"
)
var (
db *sql.DB
id = 1
)
func handler(w http.ResponseWriter, r *http.Request) {
var count int
err := db.QueryRow(`
SELECT num
FROM count
WHERE id = $1
`, id).Scan(&count)
if err != nil {
log.Fatal(err)
}
count += 1
fmt.Fprintf(w, "Count is %d", count)
_, err = db.Exec(`
UPDATE count
SET (num) = ($2)
WHERE id = $1
`, id, count)
if err != nil {
log.Fatal(err)
}
}
func main() {
addr := os.Getenv("POSTGRES_PORT_5432_TCP_ADDR")
port := os.Getenv("POSTGRES_PORT_5432_TCP_PORT")
log.Println(addr, port)
var err error
db, err = sql.Open("postgres", fmt.Sprintf("port=%s host=%s user=postgres sslmode=disable", port, addr))
if err != nil {
log.Fatal(err)
}
_, err = db.Exec(`
DROP TABLE IF EXISTS count;
CREATE TABLE count (
id integer PRIMARY KEY,
num integer NOT NULL
);
INSERT INTO count (id, num) VALUES (1, 0)
`)
if err != nil {
log.Fatal(err)
}
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
for i in {49153..49154}; do
VBoxManage modifyvm "boot2docker-vm" --natpf1 "tcp-port$i,tcp,,$i,,$i";
VBoxManage modifyvm "boot2docker-vm" --natpf1 "udp-port$i,udp,,$i,,$i";
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment