Docker Build Issue: Cannot Find Go Package
Attempting to build a Docker image with a Go application, you may encounter the error "can't load package: package myapp: cannot find package".
Problem Background
Your Dockerfile directs the build process to copy all files to the image root, then build and run the myapp binary from /go/bin/myapp. However, this approach results in the error because the binary is not found in that directory.
Solution
Instead, modify your Dockerfile as follows:
FROM golang:1.9.2 ADD . /go/src/myapp WORKDIR /go/src/myapp RUN go get myapp RUN go install ENTRYPOINT ["/go/bin/myapp"]
This updated Dockerfile will perform the following steps:
Additional Troubleshooting
If needed, you can verify the container's file structure after each ADD command using docker exec
To inspect the image in more detail, enter a shell within the generated image using docker run --rm -it
The above is the detailed content of Why Can\'t My Docker Image Find My Go Package?. For more information, please follow other related articles on the PHP Chinese website!