0

I am trying to deploy my Golang application with the help of docker compose.

In CentOS server the hierarchy of folders:

docker_app
 - src
   - app
     - docker-compose.yml
     - main.go

This Golang application use several third-party libraries:

  • Gorilla Mux;

  • Gorilla Handlers;

  • pq;

  • godotenv;

  • GORM;

  • goracle.

docker-compose.yml:

version: '2'

services:
  app:
    image: golang:1.11-alpine
    volumes:
      - .:/go/src/app
    working_dir: /go/src/app
    command: go run main.go

When I try to run docker-compose up project it raise error:

Attaching to app_app_1
app_1  | main.go:4:2: cannot find package "github.com/gorilla/handlers" in any of:
app_1  |      /usr/local/go/src/github.com/gorilla/handlers (from $GOROOT)
app_1  |      /go/src/github.com/gorilla/handlers (from $GOPATH)

As you can see I need to set up third-party libraries. How to make it correctly? Also how to set up the name of the future docker image and contaiener with the help of docker compose?

Is it possible to create go.mod file on Windows 10?

When I run $Env:GOOS = "linux"; $Env:GOARCH = "amd64"; go build command in Powershell it raise error:

enter image description here

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Nurzhan Nogerbek
  • 4,806
  • 16
  • 87
  • 193

1 Answers1

2

Is it possible to create go.mod file on Windows 10?

Yes, as long as you have a Go 1.11/1.12

But to use it with a docker-compose, you can follow "Go Docker dev environment with Go Modules and live code reloading" from Miłosz Smółka

He uses one Dockerfile for compilation:

FROM golang:1.11.2-stretch
RUN go get github.com/cespare/reflex
COPY reflex.conf /
ENTRYPOINT ["reflex", "-c", "/reflex.conf"]

Then a docker-compose for execution, mounting the compiled executable, *and the Go module cache:

version: '3'
services:
  publisher:
    build: .
    volumes:
      - ./publisher:/app
      - $GOPATH/pkg/mod/cache:/go/pkg/mod/cache
    working_dir: /app
    env_file:
      - .env
    ports:
      - 5000:5000

Regarding the cross-compilation issue with go-goracle/goracle, issue 59 details:

goracle needs CGO to compile in the Oracle OCI libs.
So either a cross-compiling C toolchain is required (and special env vars set accordingly), or a native env (that's easier, IMHO).

Meaning: don't try to cross-compile it from Windows, do it in a Dockerfile using directly the right OS (through a Linux VM, which is the case with Windows 10 HyperV environment).

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Same idea in https://www.callicoder.com/docker-compose-multi-container-orchestration-golang/ – VonC Mar 11 '19 at 05:43
  • Hello! Thank you for links. Let me try to explain what I am doing right now. I created this application in Windows 10. When I try to create `go.mod` file in `Windows 10` I see such error: `go: modules disabled inside GOPATH/src by GO111MODULE=auto; see 'go help modules'`. It seems like I need to make this command in CentOS server, right?! – Nurzhan Nogerbek Mar 11 '19 at 05:52
  • 1
    @NurzhanNogerbek Yes, or you can compile directly in Windows 10, but with cross-compilation (https://stackoverflow.com/a/50912419/6309). And you can check out https://www.reddit.com/r/golang/comments/alrnuk/receiving_the_following_error_go_modules_disabled/ – VonC Mar 11 '19 at 05:54
  • I am confused. Am I to understand that in `Dockerfile` I need to install all third-party libraries? For example `RUN go get github.com/gorilla/mux` instead of `RUN go get github.com/cespare/reflex`? As I know `go get` command need git, right? Do I need to install `git` in `Dockerfile`? – Nurzhan Nogerbek Mar 11 '19 at 06:45
  • @NurzhanNogerbek If you are building a project with `go.mod` in it (after a `go mod init`), you don't need to go get anything: a go build will go get the dependencies for you. – VonC Mar 11 '19 at 07:11
  • @NurzhanNogerbek The Dockerfile in the answer is just an example where the programmer had to get the source of his/her project first, then build it. In your case, it is not needed, since your sources are already local. – VonC Mar 11 '19 at 07:12
  • I also tried to compile the binary file with the help of next command: `$Env:GOOS = "linux"; $Env:GOARCH = "amd64"; go build`. It raise error. Can you check my post again please? – Nurzhan Nogerbek Mar 11 '19 at 07:13
  • @NurzhanNogerbek Yes, I saw your edit and I am researching it. But you are starting to cram a lot of questions in this page ;) – VonC Mar 11 '19 at 07:15
  • In production server I don't have `golang`. For thats why I can't use `go.mod` file. I think the best way is to create binary file in my local windows machine. Send it to production server and configurate Dockerfile. What do you think about that? – Nurzhan Nogerbek Mar 11 '19 at 07:20
  • @NurzhanNogerbek Yes, in your local Windows machine, you have Docker, so you can make an image with the right tools in it to compile your program directly in Linux container. From Windows. – VonC Mar 11 '19 at 07:27