I setup a simple server with golang:
package main
import (
"golang-server/database"
"golang-server/helper"
"log"
"net/http"
)
func main() {
database.Connect()
port := helper.GetPort()
SetupRoutes()
log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
log.Fatal(http.ListenAndServe(":" port, router))
}
Here is what my folder structure look like:
Here is what my dockerfile look like:
FROM golang:1.18
WORKDIR $GOPATH/src
COPY . .
RUN go mod download
RUN go build -o /golang-server
EXPOSE 8080
CMD ["golang-server"]
I am running these docker commands in the main directory:
docker build . -t golang-server
docker run --network=golang-server --name=golang-server golang-server
However I am getting this error when I run:
docker: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "golang-server": executable file not found in $PATH: unknown.
What should I set the dockerfile so I can successfully deploy to docker?
CodePudding user response:
The problem is related with your last line in Dockerfile CMD ["golang-server"]
. When you put it there, the system (inside of the container) are trying to find an executable file inside of your $PATH
variable called golang-server
.
To solve this issue, you can just edit your last line from your Dockerfile to CMD ["/golang-server"]
, once you are building your application in /
(RUN go build -o /golang-server
). The final Dockerfile should be something like:
FROM golang:1.18
WORKDIR $GOPATH/src
COPY . .
RUN go mod download
RUN go build -o /golang-server
EXPOSE 8080
CMD ["/golang-server"]