Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CGO_ENABLED=0 breaks go-sqlite3, Error : Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work #52

Closed
xmlking opened this issue Jul 8, 2019 · 7 comments

Comments

@xmlking
Copy link
Contributor

xmlking commented Jul 8, 2019

also tried defaultBaseImage: gcr.io/distroless/base without any luck

Test : repo to reproduce https://github.com/xmlking/micro-starter-kit

export KO_DOCKER_REPO=gcr.io/ngx-starter-kit
ko resolve -P -f deploy/ > release.yaml

# run
docker run -it --rm  --env CONFIG_FILE=/var/run/ko/config.yaml  --env APP_ENV=production  -p 8080:8080   gcr.io/ngx-starter-kit/github.com/xmlking/micro-starter-kit/srv/account:latest

Error
panic: could not build 'user-repository' because the build function panicked: could not build 'database': Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work. This is a stub

@xmlking xmlking changed the title CGO_ENABLED=0 breaks go-sqlite3 CGO_ENABLED=0 breaks go-sqlite3, Error : Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work Jul 8, 2019
@jonjohnsonjr
Copy link
Collaborator

We could tackle this in a couple ways...


We could check for CGO_ENABLED in the environment, and only set CGO_ENABLED=0 if it's unset.

This would allow you to build a specific image with:

$ export CGO_ENABLED=1
$ ko resolve -P -f deploy/ > release.yaml

That would be a simple change here. Unfortunately, this would be global for your invocation of ko, so you couldn't enable cgo for an individual image :(


Or, we could expand the config file to allow build flags and environment variables to be configured in on a per-package basis.

This is similar to how we allow overriding the base image: https://github.com/google/ko#overriding-the-base-for-particular-imports

Something like this:

baseImageOverrides:
  github.com/my-org/my-repo/path/to/binary: docker.io/another/base:latest
buildOverrides:
  github.com/my-org/my-repo/path/to/binary:
    env:
    - name: CGO_ENABLED
      value: 1
    flags:
    - name: ldflags
      value: "-X main.Version=foo"

I don't love either of these solutions, but I also don't think it's reasonable for us to just not work with packages that require cgo... also open to other solutions I'm not seeing :)

@xmlking
Copy link
Contributor Author

xmlking commented Jul 8, 2019

i think second option is more appropriate way to support fine graded controle.
eventually we may need to support more customizations for image building. .ko.yaml is right place to store them.

@xmlking
Copy link
Contributor Author

xmlking commented Jul 8, 2019

switching the order, let user provide any custom environment variables or overwrite. will this work ?

cmd.Env = append(os.Environ(), "CGO_ENABLED=0", "GOOS=linux")

to

cmd.Env = append([]string{"CGO_ENABLED=0", "GOOS=linux"}, os.Environ()...)

@jonjohnsonjr
Copy link
Collaborator

That seems reasonable to me

@xmlking
Copy link
Contributor Author

xmlking commented Jul 8, 2019

thanks. can i send a PR?

@xmlking
Copy link
Contributor Author

xmlking commented Jul 8, 2019

eventually we can further support customization for kodata

baseImageOverrides:
  github.com/my-org/my-repo/path/to/binary: docker.io/another/base:latest
buildOverrides:
  github.com/my-org/my-repo/path/to/binary:
    env:
    - name: CGO_ENABLED
      value: 1
    flags:
    - name: ldflags
      value: "-X main.Version=foo"
    data:
    - source: "config/template"
      destination: "template"
    - source: "config/seed"
      destination: "data/seed"

destination is relative to WorkingDir
source is relative to PrtojectRoot

jonjohnsonjr pushed a commit that referenced this issue Jul 9, 2019
Changing the order of environment variables so that user supplied value for `CGO_ENABLED` take priority 
#52
@jonjohnsonjr
Copy link
Collaborator

Fixed by #56

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants