0

I need to compile a Golang application for Linux and I can't cross-compile under Mac, because of another library. So I decided to compile within a Docker container. This is my first time to use Docker.

This is my current directory structure:

.
├── Dockerfile
├── Gopkg.lock
├── Gopkg.toml
├── Vagrantfile
├── bootstrap.sh
├── src
│   ├── cmd
│   │   ├── build.bat
│   │   ├── build.sh
│   │   ├── config.json
│   │   ├── readme.md
│   │   └── server.go
│   ├── consumers.go
│   ├── endpoints
│   │   ├── json.go
│   │   ├── rate.go
│   │   ├── test_payment.go
│   │   └── wallet.go
│   ├── middleware
│   │   └── acl.go
│   ├── models.go
│   ├── network
│   │   └── network.go
│   ├── qr
│   │   └── qr.go
│   ├── router
│   │   └── router.go
│   ├── service
│   │   └── walletService.go
│   ├── services.go
│   ├── setup.sql
│   ├── store
│   │   └── wallet.go
│   ├── stores.go
│   └── wallet
│       ├── coin.go
│       └── ethereum.go

Dockerfile:

FROM golang:latest 
WORKDIR /src/cmd
RUN ls
RUN go get github.com/go-sql-driver/mysql
RUN go build -o main ./src/cmd/server.go 
CMD ["./main"]

I try to build the Docker image with:

docker build -t outyet .

This is the error it returns:

Sending build context to Docker daemon  5.505MB
Step 1/6 : FROM golang:latest
 ---> d0e7a411e3da
Step 2/6 : WORKDIR /src/cmd
 ---> Using cache
 ---> 0c4c2b99e294
Step 3/6 : run ls
 ---> Using cache
 ---> 23d3e491a2e1
Step 4/6 : RUN go get github.com/go-sql-driver/mysql
 ---> Running in f34447e51f6c
Removing intermediate container f34447e51f6c
 ---> 5731ab22ee43
Step 5/6 : RUN go build -o main server.go
 ---> Running in ecc48fcf5488
stat server.go: no such file or directory
The command '/bin/sh -c go build -o main server.go' returned a non-zero code: 1

How i can build my Golang application with docker?

1
  • If you want to build the source code within Docker, you first have to copy the source code into your Docker image, which you're not doing. Commented Aug 17, 2018 at 12:10

2 Answers 2

0

The error you're seeing is:

stat server.go: no such file or directory
The command '/bin/sh -c go build -o main server.go' returned a non-zero code: 1

... and in other words, it's telling you that the Go compiler can't find server.go which you're trying to build. Update your Dockerfile to copy your local files into the Docker image:

FROM golang:latest 
COPY . /go/src/workdir
WORKDIR /go/src/workdir
RUN go build -o main ./src/cmd/server.go 
CMD ["/go/src/workdir/main"]

In your directory I spotted Gopkg.toml which is a dependency manifest used by dep. dep will use a directory called vendor to include all dependencies for your Go project. Before you build the Docker image, you need to ensure all dependencies are present with dep ensure:

$ dep ensure
$ docker build -t outyet .
Sign up to request clarification or add additional context in comments.

Comments

0

You need to add your sources in your build container. Add this line in your Dockerfile (after FROM golang:latest for example) :

ADD src/ /src/cmd

Then, you could access files inside the container (/src/cmd) : RUN ls should now return something.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.