Tips for running Go with docker

docker image

Reference of patterns using go mod

#build image
FROM golang:1.13 AS builder

WORKDIR /go/src

##Download dependent libraries(I want to use the cache, so I'll do this first)
ENV GO111MODULE=on
COPY go.mod go.sum ./
RUN go mod download

ADD . /myapp
WORKDIR /myapp

## main.Compile go and save the executable binary
RUN CGO_ENABLED=0 GOOS=linux go build -o server ../myapp/cmd/main.go

# run-time image
FROM alpine:3.10
COPY --from=builder /myapp/server /app
EXPOSE 50051
ENTRYPOINT ["/app"]

Reference of patterns using dep

#build image
FROM golang:1.13 AS builder
ADD . /go/src/github.com/myself/myapp
WORKDIR /go/src/github.com/myself/myapp

##Install dep and resolve dependencies
RUN go get github.com/golang/dep/cmd/dep
RUN dep ensure

## main.Compile go and save the executable binary
RUN CGO_ENABLED=0 GOOS=linux go build -o server /myapp/cmd/main.go

# run-time image
FROM alpine:3.10
COPY --from=builder /go/src/github.com/myself/myapp/server /app
EXPOSE 50051
ENTRYPOINT ["/app"]

Clogged points

cannot find package error in go build

--Most likely the path is wrong or the file is missing

packages founded A and B error in go build

--There are multiple packages in the same directory --The following is a compilation error --Place multiple main package modules under the target directory --Put modules with different name package declarations in the same directory --Files ending in _test are interpreted as test code, so they can be placed as multiple files as an exception. --By the way, it is safe to declare the package with a different name from the directory. --Because it imports on a path basis instead of a package name

In go get, XX is not using a known version control system error

dep ensure / hoge is not within a known GOPATH / src error

――I don't know, but the directory structure seems to be useless. --A feature of Go's directory structure is that all source code is placed under the environment variable $ GOPATH / src. --There are two solutions: --Place the repository under / go / src in the container --Put a symbolic link

dep ensure root project import: dep does not currently support using GOPATH / src as the project root error

--The workdir was specified as GOPATH / src, but it was fixed by changing it to GOPATH / src / app.

With dep ensure at docker build, I get angry because I can't access github like fatal: could not read Username for'https://github.com'~

--When you take resources from private repository, you have to use github token etc. ――However, you should be able to use the local path when taking resources from your repository. If you can't get it, it's likely that the path is wrong --In fact, I was able to change docker's workdir from go / src / app to /go/src/github.com/myself/my-app

When I do docker run, I get angry with ʻexec user process caused "no such file or directory" `

--Recent Go binaries seem to do a Dynamic link, which seems to be caused by the lack of the link target. --You can add CGO_ENABLED = 0 at build time - CGO_ENABLED=0 go build

Recommended Posts

Tips for running Go with docker
Create a development environment for Go + MySQL + nginx with Docker (docker-compose)
Tips for dealing with binaries in Python
Let's try gRPC with Go and Docker
Tips for using python + caffe with TSUBAME
Tips for plotting multiple lines with pandas
Python with Go
Hello World with gRPC / go in Docker environment
Go (Echo) Go Modules × Build development environment with Docker
nvidia-docker2 installation guide for using gpu with docker
~ Tips for Python beginners from Pythonista with love ① ~
I tried running faiss with python, Go, Rust
~ Tips for Python beginners from Pythonista with love ② ~
Tftp server with Docker
Learn Service Bus Queue with Azure SDK for Go (1)
(For those unfamiliar with Matplotlib) Tips for drawing graphs with Seaborn
Tips for speeding up python code correctly with numba
Use python with docker
Proxy server with Docker
Create a Layer for AWS Lambda Python with Docker
Hello, World with Docker
Solution for inaccessible Gin server running on Docker container
Tips for opening a scene with a broken reference with a script
Create an environment for test automation with AirtestIDE (Tips)
Detonation Velocity Framework light-4j Verified with Docker for Mac: Footprint
[Memo] Build a development environment for Django + Nuxt.js with Docker
Tips for developing apps with Azure Cosmos DB in Python
Create an environment for "Deep Learning from scratch" with Docker
From environment construction to deployment for flask + Heroku with Docker
Draw Bezier curves with Go
Bit full search with Go
Connect to Postgresql with GO
Try running CNN with ChainerRL
Hot reload with Go + Air
Implement PyTorch + GPU with Docker
PySpark life starting with Docker
Prepare python3 environment with Docker
[Tips] Handle Athena with Python
Overview of Docker (for beginners)
Rolling update for Docker Swarm
Try running Python with Try Jupyter
Try implementing perfume with Go
Try Selenium Grid with Docker
[Python + Selenium] Tips for scraping
~ Tips for beginners to Python ③ ~
Try building JupyterHub with Docker
Data processing tips with Pandas
Rails application building with Docker
Tips for data analysis ・ Notes
Detonation velocity framework light-4j verified with Docker for Mac: Response speed
Machine Learning with docker (42) Programming PyTorch for Deep Learning By Ian Pointer
I made a development environment for Django 3.0 with Docker, Docker-compose, Poetry
It's time to install DB with Docker! DB installation for beginners on Docker
From ubuntu installation to running kinect with docker and ros (overview)
I tried running the TensorFlow tutorial with comments (_TensorFlow_2_0_Introduction for beginners)
[DynamoDB] [Docker] Build a development environment for DynamoDB and Django with docker-compose