Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

Module path issue while trying to run Go Echo app in docker

I am having a weird issue to run a Go Echo Framework. It works fine when I use the docker run command, that is no dependency issue and it executes the file properly but while running docker-compose up it gives error:

Creating echo_app ... done
Attaching to echo_app
echo_app | server.go:6:2: cannot find package "github.com/labstack/echo/v4" in any of:
echo_app |  /usr/local/go/src/github.com/labstack/echo/v4 (from $GOROOT)
echo_app |  /go/src/github.com/labstack/echo/v4 (from $GOPATH)
echo_app exited with code 1

Below are my files:

Dockerfile

FROM golang:1.15-alpine
LABEL maintainer="Me"

RUN apk update
RUN apk add git

# Setting up Dev environment
RUN mkdir /echo_app
COPY . /echo_app/
WORKDIR /echo_app/

# Download Echo Framework with help of Go mod (Credit: https://github.com/labstack/echo/issues/1374#issuecomment-559990790)
# Init to create Go Module
RUN go mod init app.com/main
# Building to download echo framework and other dependencies
RUN go build

# RUN the server
#CMD go run server.go 
EXPOSE 1323

docker-compose.yml

version: "3"
services: 
  web:
    container_name: echo_app
    image: echo_app:latest
    build: ./
    command: go run server.go
    volumes:
      - .:/echo_app
    ports: 
      - "1323:1323"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
FROM golang:1.15-alpine
LABEL maintainer="Me"


# Setting up Dev environment

WORKDIR /echo_app/
# note this file, go.mod exists locally. and contain reference 
# to direct/indirect dependencies. this step allows to download 
# dependencies and speedup build for docker images (if it used 
# to build artifacts, and not as dev env).  
COPY go.mod  /echo_app/go.mod
RUN go mod download


EXPOSE 1323

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...