Reputation: 5236
I have such pretty simple Dockerfile:
FROM alpine
ADD ./service /
RUN chmod +x /service
EXPOSE 8000
ENTRYPOINT ["./service"]
With this Dockerfile I successfully created image but I have error when try to create container.
standard_init_linux.go:178: exec user process caused "exec format error"
This error can happens when something wrong with binary file or with OS/ARCH of docker container.
service
is a binary file of Golang application. In my case Golang application use CGO packages. I compile it in my local machine which is Windows 10. Here is my instruction:
Install tdm-gcc. A compiler suite for 32- and 64-bit Windows based on the GNU toolchain.
In POWERSHELL
execute commands:
set GOOS=linux
set GOARCH=amd64
go build -v -o questionnaire -ldflags="-extld=$CC"
go env
command return me next:
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\NNogerbek\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\NNogerbek\go
set GOPROXY=
set GORACE=
set GOROOT=C:\Go
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\NNOGER~1\AppData\Local\Temp\go-build371579938=/tmp/go-build -gno-record-gcc-switches
Where I make mistake and how to fix this problem?
I also tried to compile binary inside docker with such Dockerfile:
FROM golang:1.11
WORKDIR /go/src/application
ADD . .
RUN go get ./...
RUN go build -o /service .
ENV PORT=8000
CMD ["/service"]
I don't know why but I have error. Somehow Docker can't import project packages:
package application/database: unrecognized import path "application/database" (import path does not begin with hostname)
package application/routes: unrecognized import path "application/routes" (import path does not begin with hostname)
package application/utils: unrecognized import path "application/utils" (import path does not begin with hostname)
package application/models: unrecognized import path "application/models" (import path does not begin with hostname)
package application/controllers: unrecognized import path "application/controllers" (import path does not begin with hostname)
By the way project structure:
- application
- database
- routes
- utils
- models
- controllers
main.go
Dockerfile
Upvotes: 0
Views: 1290
Reputation: 5236
Finally I understand that compile linux/amd64
binary file of Goland
application which use CGO
packages in Windows OS is not good idea. Binary file which I tried to start in Docker container always raised error standard_init_linux.go:178: exec user process caused "exec format error"
. Error mean that something wrong with OS/Arch
or you use corrupted file.
Finally I decided to compile binary file inside Docker container. My Dockerfile
looks like this:
FROM golang:1.11
WORKDIR /go/src/application
ADD . .
RUN go get [your_dependencies]
RUN go build -o service
ENV PORT=8000
CMD ["./service"]
I hope this post will be useful for you!
Upvotes: 0
Reputation: 757
What is SRC_DIR
value and moreover does ./service
has executable permissions.
Try to run chmod +x ./service
on host machine or in Dockerfile
.
Upvotes: 1