Dockerfile golang build - docker

I am building my Go application. I am getting error like below
# cd .; git clone https://github.com/julienschmidt/httprouter /go/src/github.com/julienschmidt/httprouter Cloning into
'/go/src/github.com/julienschmidt/httprouter'... fatal: unable to
access 'https://github.com/julienschmidt/httprouter/': Could not
resolve host: github.com package github.com/julienschmidt/httprouter:
exit status 128
The command '/bin/sh -c go get ./' returned a non-zero code: 1
docker file.
# install dependencies first
RUN go get ./
RUN go build
CMD if [ ${APP_ENV} = production ]; \
then \
main; \
else \
go get github.com/pilu/fresh && \
fresh; \
fi
EXPOSE 8080
build command:
docker image build -t test:1.0 .
Thanks. Any help please?

Check DNS server
Check internet connection
You can clone the repository with a script in a folder, and then copy the folder to the docker and build the project. Example
FROM golang:1.9 as builder
RUN mkdir -p /go/src/folder
WORKDIR /go/src/folder
COPY src/ .
RUN go get -d
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -o app .
FROM alpine:latest
WORKDIR /
COPY --from=builder /go/src/folder/app .
CMD ["/app"]

Related

Building Go apps with private modules in Docker

I'm trying to build a go project in a docker container that relies on private submodules.
I was hoping that --mount=type=ssh would pass my ssh credentials to the container and it'd work. Currently I can build locally with just make the GOPRIVATE variable set and the git config update.
Here is my relevant Dockerfile currently
# syntax = docker/dockerfile:experimental
FROM golang:1.14.3-alpine AS build
RUN apk add --no-cache git \
openssh-client \
ca-certificates
WORKDIR /src
ENV GIT_TERMINAL_PROMPT=1
ENV GOPRIVATE="gitlab.com/company_foo"
RUN git config --global url."ssh://git#gitlab.com".insteadOf "https://gitlab.com"
# Authorize SSH Host
# Skip Host verification for git
RUN mkdir -p /root/.ssh && \
chmod 0700 /root/.ssh && \
ssh-keyscan gitlab.com > /root/.ssh/known_hosts &&\
chmod 644 /root/.ssh/known_hosts && touch /root/.ssh/config \
&& echo "StrictHostKeyChecking no" > /root/.ssh/config
COPY go.mod go.sum .
RUN --mount=type=ssh mkdir -p /var/ssh && \
GIT_SSH_COMMAND="ssh -o \"ControlMaster auto\" -o \"ControlPersist 300\" -o \"ControlPath /var/ssh/%r#%h:%p\"" \
go mod download
COPY . .
RUN --mount=type=cache,target=/root/.cache/go-build go build -o api-server ./cmd/api-server
RUN --mount=type=cache,target=/root/.cache/go-build go build -o migrations ./cmd/migrations
I've also tried adding a CI_JOB_TOKEN with
RUN echo -e "machine gitlab.com\nlogin gitlab-ci-token\npassword ${CI_JOB_TOKEN}" > ~/.netrc
but this also didn't work. Perhaps I did it wrong.
All of this results in the failure:
revision v0.0.3: unknown revision v0.0.3
relating to one of our private repos.
Any advice would be appreciate.
I'm absolutely at a lost.
This workes for me.
FROM golang:1.14
ARG USERNAME=user1
ARG PASSWORD=secret
WORKDIR /app
ADD . .
ENV GOPRIVATE=private.git.local/*
RUN echo "machine private.git.local login $USERNAME password $PASSWORD" > ~/.netrc
RUN go build -o testGo main.go
CMD ["/app/testGo"]
pass your gitlab_token to docker file from gitlab_ci.yaml and do the following steps
RUN git config --global url."https://oauth2:$GITLAB_TOKEN#gitlab.com/".insteadOf "https://git#gitlab.com/"
add your repo as GO_PRIVATE
ENV GOPRIVATE=gitlab.com/*
copy .netrc file to docker root
COPY confidential/.netrc /root/.netrc
.netrc file will have the following structure
machine gitlab.com
login gitlab_user
password p#$$word

Im having problem building docker for golang api

I'm new to docker.
I'm trying to implement RESTfull api in go using echo server. My code works fine when I run main.go but i cant run it using docker.
this is my echo server:
r := router.Router()
r.Logger.Fatal(r.Start("localhost:8080"))
and this is my Dockerfile:
FROM golang:latest AS build
ENV GO111MODULE=on \
CGO_ENABLED=1
#Maintainer info
LABEL maintainer="Saman Hoseini"
WORKDIR /app
COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .
RUN go build -o main .
#this step is for CGO libraries
RUN ldd main | tr -s '[:blank:]' '\n' | grep '^/' | \
xargs -I % sh -c 'mkdir -p $(dirname ./%); cp % ./%;'
RUN mkdir -p lib64 && cp /lib64/ld-linux-x86-64.so.2 lib64/
#Second stage of build
FROM alpine:latest
RUN apk update && apk --no-cache add ca-certificates \
sqlite
COPY --from=build /app ./
EXPOSE 8080
ENTRYPOINT ["./main"]
After build when i run the container i face curl failure:
$ docker run -d -p 8080:8080 my-docker
$ curl http://localhost:8080
curl: (56) Recv failure: Connection reset by peer
how can i fix this problem ?
You need to run your application on the external port of your container.
r := router.Router()
r.Logger.Fatal(r.Start(":8080"))
This happens because the EXPOSE 8080 command forwards the application port open on the external port of the container (not internal, like localhost).
After this, command docker run -d -p 8080:8080 my-docker, more precisely a parameter -p, forwards external port from your container to external port on your machine.

Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work. This is a stub

I want to use sqlite3 in Golang project. But run it in docker container has some error.Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work. This is a stub
this is my build script
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o main main.go
I can't use CGO_ENABLED=1 in mac computer.
FROM golang:1.13-alpine
ENV WORK_DIR=/go
ENV TIME_ZONE=Asia/Singapore
RUN ln -snf /usr/share/zoneinfo/$TIME_ZONE /etc/localtime && echo $TIME_ZONE > /etc/timezone
WORKDIR $WORK_DIR
RUN mkdir -p logs
COPY main .
COPY config.conf .
COPY basic.db ./data
COPY db db
ENTRYPOINT ./main -c config.conf
How can I use sqlite3 in the docker container. Or how can I build CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -o main main.go Golang project
#LinPy Thank you for u help.
https://www.x-cellent.com/blog/cgo-bindings/
I solved the problem. But build takes a long time, about 10 minutes, and I'm still looking for a better solution.
Images Dockerfile : https://github.com/sillyhatxu/alpine-build
FROM xushikuan/alpine-build:2.0 AS builder
ENV WORK_DIR=$GOPATH/src/github.com/sillyhatxu/mini-mq
WORKDIR $WORK_DIR
COPY . .
RUN go build -a -ldflags "-linkmode external -extldflags '-static' -s -w" -o main main.go
FROM xushikuan/alpine-build:1.0
ENV BUILDER_WORK_DIR=/go/src/github.com/sillyhatxu/mini-mq
ENV WORK_DIR=/app
ENV TIME_ZONE=Asia/Singapore
WORKDIR $WORK_DIR
RUN ln -snf /usr/share/zoneinfo/$TIME_ZONE /etc/localtime && echo $TIME_ZONE > /etc/timezone
RUN mkdir -p logs
RUN mkdir -p db
RUN mkdir -p data
COPY --from=builder $BUILDER_WORK_DIR/main $WORK_DIR
COPY --from=builder $BUILDER_WORK_DIR/config.conf $WORK_DIR
COPY --from=builder $BUILDER_WORK_DIR/db $WORK_DIR/db
COPY --from=builder $BUILDER_WORK_DIR/basic.db $WORK_DIR/data
ENTRYPOINT ./main -c config.conf
Build with the correct image
FROM golang:1.18-alpine AS BUILDER
RUN apk add --no-cache gcc g++ git openssh-client
RUN GO111MODULE=on CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GOPROXY=https://goproxy.cn,direct \
go build -ldflags="-w -s" -o server

Go multi-stage build with stretch and alpine

I'm trying to build from go stretch using -race and then copy it to the small alpine container to reduce final size. But I got this error during starting:
standard_init_linux.go:211: exec user process caused "no such file or directory"
My Dockerfile:
FROM golang:1.12.6-stretch as build-env
WORKDIR /goman/
ADD . .
RUN GOFLAGS=-mod=vendor go test -race -v ./...
RUN CGO_ENABLED=0 GOBIN=/goman/apps/ GOOS=linux GOARCH=amd64 go install -v \
-a -tags netgo -installsuffix netgo -mod vendor -ldflags "-d -s -w" ./cmd/...
FROM alpine:3.10
RUN apk add --no-cache ca-certificates tzdata
COPY --from=build-env /goman/apps/ /apps
COPY --from=build-env /goman/conf /conf
WORKDIR /apps/
I can clearly see files when sh`ing in container and the permissions are correct
Try to use go build instead of go install. In this case, I think, you only need to build an executable from the source, not install and build the remote one.

use golang in docker container in multi-stage build

I want to use multi-stage build and I want that at the end I will have Golang inside the running container.
When I run the container and do go version I get error “unknown go”
# build stage
FROM golang:1.11.2-alpine3.8 AS builder-env
ENV CGO_ENABLED=0
ENV GOOS=linux
ADD https://github.com/golang/dep/releases/download/v0.5.0/dep-linux-amd64 /usr/bin/dep
RUN chmod +x /usr/bin/dep
RUN mkdir -p $GOPATH/src/github/mtp/myproj
WORKDIR $GOPATH/src/github/mtp/myproj
COPY Gopkg.toml Gopkg.lock ./
RUN dep ensure --vendor-only
COPY . ./
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix nocgo -o /myproj .
FROM alpine:latest
RUN mkdir /data
COPY --from=builder-env myproj ./
I can do something like this to make it work, but is it a good options ?
FROM alpine:latest
RUN mkdir /data
RUN update-ca-certificates && \
apk add go
COPY --from=builder-env myproj ./
You don't need the go executable to run a compiled program, just the resulting binary. If you add in the missing CMD line to your Dockerfile
CMD ["./myproj"]
I'd expect it would work fine.

Resources