[FIXED] Windows Docker Image

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
This commit is contained in:
Ivan Kozlovic
2017-07-17 13:25:56 -06:00
parent 9a44c20890
commit 9cddf0fcdf
5 changed files with 32 additions and 3 deletions

11
Dockerfile.win64 Normal file
View File

@@ -0,0 +1,11 @@
FROM golang:1.7.6
MAINTAINER Ivan Kozlovic <ivan.kozlovic@apcera.com>
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"]

View File

@@ -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 (

View File

@@ -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.

View File

@@ -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

View File

@@ -3,6 +3,7 @@
package server
import (
"os"
"time"
"golang.org/x/sys/windows/svc"
@@ -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
@@ -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 {
@@ -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
}