Vlad
Vlad

Reputation: 63

Golang docker container doesn't start on Windows Home

I'm using Windows Home OS with WSL enabled in Docker.

After build message below is printed

docker build .
SECURITY WARNING: You are building a Docker image from Windows against a non-    Windows Docker host. All files and directories added to build context will have '-rwxr-    xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.

When I try to run image message below is printed

docker run 49fee6f5a6bb -d
docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"-d\": executable file not found in $PATH": unknown.

Dockerfile content is below

FROM golang:alpine
# Set necessary environmet variables needed for our image
ENV GO111MODULE=on \
    CGO_ENABLED=0 \
    GOOS=linux \
    GOARCH=amd64

# Move to working directory /build
WORKDIR /build

# Copy and download dependency using go mod
COPY go.mod .
COPY go.sum .
RUN go mod download

# Copy the code into the container
COPY . .

# Build the application
RUN go build -o main .

# Move to /dist directory as the place for resulting binary folder
WORKDIR /dist

# Copy binary from build to main folder
RUN cp /build/main .

# Export necessary port
EXPOSE 3000

# Command to run when starting the container
CMD ["/dist/main"]

Upvotes: 0

Views: 1010

Answers (1)

Norbert
Norbert

Reputation: 6084

The issue is the order of commands (most likely):

docker run -d yourcontainer should work

Some unrelated advice: This is a rewrite of your docker file (untested). As you notice it is shorter, and uses a build and run stage.

The build stage uses a larger container with golang in there, your code, etc. The run container (FROM alpine:latest)only has the resulting program making it nice and small (quick to load onto your container environment), and more secure (you can use a hardend linux for example with only your code).

FROM golang:alpine AS build

WORKDIR /build
ADD . /build

# Depending on the golang version GO111MODULE can be removed as env variable
RUN GOOS=linux GOARCH=amd64 GO111MODULE=on go build -o main 

FROM alpine:latest

# Export necessary port
EXPOSE 3000
# Add  application
WORKDIR /dist
COPY --from=build /build/main /dist/main

Upvotes: 2

Related Questions