From 74e8502f3f142d999c495be1b7683dfe17701ac6 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 9 Oct 2016 18:58:32 +0200 Subject: [PATCH 1/5] - Fix .gitignore - Note about bug with Docker driver - Implement experimental pid ulimit of 150. --- .gitignore | 2 ++ services/docker.go | 8 ++++++++ services/session.go | 1 + 3 files changed, 11 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e253afb --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +play-with-docker + diff --git a/services/docker.go b/services/docker.go index 9734c4e..c30408c 100644 --- a/services/docker.go +++ b/services/docker.go @@ -29,10 +29,14 @@ func GetContainerInfo(id string) (types.ContainerJSON, error) { } func CreateNetwork(name string) error { + // TODO: This line appears to give an error when running on localhost:3000 + // when driver is specified a name must be given. opts := types.NetworkCreate{Attachable: true, Driver: "overlay"} _, err := c.NetworkCreate(context.Background(), name, opts) if err != nil { + log.Printf("Starting session err [%s]\n", err) + return err } @@ -72,7 +76,11 @@ func AttachExecConnection(execId string, ctx context.Context) (*types.HijackedRe func CreateInstance(net string) (*ptypes.Instance, error) { + var maximumPidLimit int64 + maximumPidLimit = 150 // Set a ulimit value to prevent misuse h := &container.HostConfig{NetworkMode: container.NetworkMode(net), Privileged: true} + h.Resources.PidsLimit = maximumPidLimit + conf := &container.Config{Image: "docker:dind"} container, err := c.ContainerCreate(context.Background(), conf, h, nil, "") diff --git a/services/session.go b/services/session.go index 4c8ced6..6f82613 100644 --- a/services/session.go +++ b/services/session.go @@ -18,6 +18,7 @@ func NewSession() (*types.Session, error) { s := &types.Session{} s.Id = uuid.NewV4().String() s.Instances = map[string]*types.Instance{} + log.Printf("NewSession id=[%s]\n", s.Id) //TODO: Store in something like redis sessions[s.Id] = s From bccfdb2ca52868239665f6cf4951eaad2881e83d Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 9 Oct 2016 19:12:35 +0200 Subject: [PATCH 2/5] Outline basic instructions --- README.md | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8124ae6..a2d5d89 100644 --- a/README.md +++ b/README.md @@ -1 +1,23 @@ -# play-with-docker \ No newline at end of file +# play-with-docker + +Play With Docker gives you the experience of having a free Alpine Linux Virtual Machine in the cloud +where you can build and run Docker containers and even create clusters with Docker features like Swarm Mode. + +A live version is available at: http://play-with-docker.com/ + +## Installation + +1) Install go 1.7.1 with `brew` on Mac or through a package manager. + +2) `go get` + +3) `go build` + +4) Run the binary produced as `play-with-docker` + +5) Point to http://localhost:3000/ and click "New Instance" + +Notes: + +There is a hard-coded limit to 5 Docker playgrounds per session. After 1 hour sessions are deleted. + From f91af56a09ad2631eaccfe6058de009a51638e71 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 9 Oct 2016 19:16:09 +0200 Subject: [PATCH 3/5] Ammend wording --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index a2d5d89..3c0aea5 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,8 @@ Play With Docker gives you the experience of having a free Alpine Linux Virtual Machine in the cloud where you can build and run Docker containers and even create clusters with Docker features like Swarm Mode. +Under the hood DIND or Docker-in-Docker is used to give the effect of multiple VMs/PCs. + A live version is available at: http://play-with-docker.com/ ## Installation From 11749c49024029d61cba5d74dd5ed36c449f8a8a Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 9 Oct 2016 19:37:53 +0200 Subject: [PATCH 4/5] Provide DIND override through DIND_IMAGE env variable. Use pinned version of DIND by default instead of latest to prevent surprises. --- services/docker.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/services/docker.go b/services/docker.go index c30408c..21ea4f8 100644 --- a/services/docker.go +++ b/services/docker.go @@ -2,6 +2,7 @@ package services import ( "log" + "os" "strings" ptypes "github.com/franela/play-with-docker/types" @@ -81,7 +82,12 @@ func CreateInstance(net string) (*ptypes.Instance, error) { h := &container.HostConfig{NetworkMode: container.NetworkMode(net), Privileged: true} h.Resources.PidsLimit = maximumPidLimit - conf := &container.Config{Image: "docker:dind"} + dindImage := os.Getenv("DIND_IMAGE") + if len(dindImage) == 0 { + dindImage = "docker:1.12.2-rc2-dind" + } + + conf := &container.Config{Image: dindImage} container, err := c.ContainerCreate(context.Background(), conf, h, nil, "") if err != nil { From 7df6bacee59cf2f11bceee646e385d6c5a0d0b36 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 9 Oct 2016 19:49:34 +0100 Subject: [PATCH 5/5] Allow dind image to be overriden. - improve readme - mention pre-pulling dind image. - read env variable once in init() method --- README.md | 6 +++++- services/docker.go | 8 +------- services/instance.go | 26 ++++++++++++++++++++++---- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 3c0aea5..59ab094 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,8 @@ A live version is available at: http://play-with-docker.com/ ## Installation +Start the Docker daemon on your machine and run `docker pull docker:1.12.2-rc2-dind`. + 1) Install go 1.7.1 with `brew` on Mac or through a package manager. 2) `go get` @@ -21,5 +23,7 @@ A live version is available at: http://play-with-docker.com/ Notes: -There is a hard-coded limit to 5 Docker playgrounds per session. After 1 hour sessions are deleted. +* There is a hard-coded limit to 5 Docker playgrounds per session. After 1 hour sessions are deleted. +* If you want to override the DIND version or image then set the environmental variable i.e. + `DIND_IMAGE=docker:dind` diff --git a/services/docker.go b/services/docker.go index 21ea4f8..49306d9 100644 --- a/services/docker.go +++ b/services/docker.go @@ -2,7 +2,6 @@ package services import ( "log" - "os" "strings" ptypes "github.com/franela/play-with-docker/types" @@ -75,18 +74,13 @@ func AttachExecConnection(execId string, ctx context.Context) (*types.HijackedRe return &conn, nil } -func CreateInstance(net string) (*ptypes.Instance, error) { +func CreateInstance(net string, dindImage string) (*ptypes.Instance, error) { var maximumPidLimit int64 maximumPidLimit = 150 // Set a ulimit value to prevent misuse h := &container.HostConfig{NetworkMode: container.NetworkMode(net), Privileged: true} h.Resources.PidsLimit = maximumPidLimit - dindImage := os.Getenv("DIND_IMAGE") - if len(dindImage) == 0 { - dindImage = "docker:1.12.2-rc2-dind" - } - conf := &container.Config{Image: dindImage} container, err := c.ContainerCreate(context.Background(), conf, h, nil, "") diff --git a/services/instance.go b/services/instance.go index 949f27d..e95b8e2 100644 --- a/services/instance.go +++ b/services/instance.go @@ -1,19 +1,37 @@ package services -import "github.com/franela/play-with-docker/types" +import ( + "log" + "os" + + "github.com/franela/play-with-docker/types" +) var instances map[string]map[string]*types.Instance +var dindImage string +var defaultDindImageName string + func init() { instances = make(map[string]map[string]*types.Instance) + dindImage = getDindImageName() +} + +func getDindImageName() string { + dindImage := os.Getenv("DIND_IMAGE") + defaultDindImageName = "docker:1.12.2-rc2-dind" + if len(dindImage) == 0 { + dindImage = defaultDindImageName + } + return dindImage } func NewInstance(session *types.Session) (*types.Instance, error) { - //TODO: Validate that a session can only have 10 instances + //TODO: Validate that a session can only have 5 instances //TODO: Create in redis - - instance, err := CreateInstance(session.Id) + log.Printf("NewInstance - using image: [%s]\n", dindImage) + instance, err := CreateInstance(session.Id, dindImage) if err != nil { return nil, err