Skip to content

Commit

Permalink
[FIXED] Windows Docker Image
Browse files Browse the repository at this point in the history
The use of the `svc` API prevented the NATS Server to run as
a container on both nanoserver and windowsservercore Docker images.
An attempt was made to replace svc.Debug with normal server.Start()
if detected to be interactive, however, that did not work. The
fact of detecting if interactive or not already requires connecting
to the service controller apparently.
This change looks up for an environment variable (NATS_DOCKERIZED)
and if set to "1", will not make use of the `svc` package.
This environment variable will be set in the Docker image (in
nats-docker/windows/nanoserver/Dockerfile and windowsservercore/Dockerfile).

Resolves #543
  • Loading branch information
kozlovic committed Jul 19, 2017
1 parent 9a44c20 commit 9cddf0f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
11 changes: 11 additions & 0 deletions Dockerfile.win64
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM golang:1.7.6

MAINTAINER Ivan Kozlovic <[email protected]>

COPY . /go/src/github.com/nats-io/gnatsd
WORKDIR /go/src/github.com/nats-io/gnatsd

RUN CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags "-s -w" -o gnatsd.exe .

ENTRYPOINT ["go"]
CMD ["version"]
3 changes: 2 additions & 1 deletion logger/syslog_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ package logger

import (
"fmt"
"golang.org/x/sys/windows/svc/eventlog"
"os"
"strings"

"golang.org/x/sys/windows/svc/eventlog"
)

const (
Expand Down
3 changes: 2 additions & 1 deletion logger/syslog_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
package logger

import (
"golang.org/x/sys/windows/svc/eventlog"
"os/exec"
"strings"
"testing"

"golang.org/x/sys/windows/svc/eventlog"
)

// Skips testing if we do not have privledges to run this test.
Expand Down
2 changes: 1 addition & 1 deletion server/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const (

const (
// VERSION is the current version for the server.
VERSION = "1.0.0"
VERSION = "1.0.1"

// DEFAULT_PORT is the default port for client connections.
DEFAULT_PORT = 4222
Expand Down
16 changes: 16 additions & 0 deletions server/service_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package server

import (
"os"
"time"

"golang.org/x/sys/windows/svc"
Expand All @@ -22,6 +23,14 @@ type winServiceWrapper struct {
server *Server
}

var dockerized = false

func init() {
if v, exists := os.LookupEnv("NATS_DOCKERIZED"); exists && v == "1" {
dockerized = true
}
}

// Execute will be called by the package code at the start of
// the service, and the service will exit once Execute completes.
// Inside Execute you must read service change requests from r and
Expand Down Expand Up @@ -76,6 +85,10 @@ loop:

// Run starts the NATS server as a Windows service.
func Run(server *Server) error {
if dockerized {
server.Start()
return nil
}
run := svc.Run
isInteractive, err := svc.IsAnInteractiveSession()
if err != nil {
Expand All @@ -89,6 +102,9 @@ func Run(server *Server) error {

// isWindowsService indicates if NATS is running as a Windows service.
func isWindowsService() bool {
if dockerized {
return false
}
isInteractive, _ := svc.IsAnInteractiveSession()
return !isInteractive
}

0 comments on commit 9cddf0f

Please sign in to comment.