dongzhuji1042 2019-01-11 10:50
浏览 19

使用AWS EC2 Golang GO端点在Docker部署上获取JSON时出现问题

I am having a hard time on GET request to endpoint of /biz on AWS with Docker file. I made the following project based on this course and would love to hear your insights.

I ran through all the following commands in terminal

docker build -t projectname
docker tag projectname username/projectname
docker push username/projectname
ssh -i ~/.ssh/projectAWS.pem ec2-user@[public-DNS

Inside terminal of AWS EC2

sudo yum update -y
sudo yum install -y dockersudo service docker start
sudo usermod -a -G docker ec2-user
docker run -d -p 80:80 usernam/projectname

When I hit my IPv4 Public IP http://xxx.xxx.xxx/biz, nothing return but "This site can’t be reached"It works on localhost:80/biz when docker run locally. Do you think there's something I am missing? Like the Dockerfile? Missing imports? AWS set-up? Endpoints? or main.go itself? Thanks in advance!

# Dockerfile from Docker
# Start from a Debian image with the latest version of Go installed
# and a workspace (GOPATH) configured at /go.
FROM golang

# Copy the local package files to the container's workspace.
ADD . /go/src/github.com/seintun/dinesty.ninja-backend
WORKDIR /go/src/github.com/seintun/dinesty.ninja-backend

# Build the outyet command inside the container.
RUN go get ./
RUN go build

# Run the outyet command by default when the container starts.
ENTRYPOINT /go/bin/dinesty.ninja-backend

# Document that the service listens on port 8080.
EXPOSE 8080

package main

import (
  "log"
  "net/http"
  "os"

  "github.com/gorilla/handlers"
  "github.com/gorilla/mux"
  ctrl "github.com/username/projectname/controllers"
)

func main() {
    r := mux.NewRouter()
    r.HandleFunc("/biz", ctrl.FetchBiz).Methods("GET")
rLog := handlers.LoggingHandler(os.Stdout, r)
    if err := http.ListenAndServe(":8080", rLog); err != nil {
        log.Fatal(err)
    }
}

// In case you wonder what ctrl.FetchBiz do, it just GET JSON from mongoDB package ctrl

import (
  "encoding/json"
  "io/ioutil"
  "log"
  "net/http"

  . "github.com/username/projectname/models"
  "gopkg.in/mgo.v2/bson"

  mgo "gopkg.in/mgo.v2"
)

// FetchBiz return list of bizs
func (b *BizDAO) FetchBiz() ([]Biz, error) {
    query := bson.M{"active": true}
    var bizs []Biz
    err := db.C(BCOLLECTION).Find(query).All(&bizs)
    return bizs, err
}
</div>
  • 写回答

1条回答 默认 最新

  • du7979 2019-01-11 13:54
    关注

    Your webserver serves to port 8080 if err := http.ListenAndServe(":8080", rLog); err != nil { while you expose and access port 80 docker run -d -p 80:80 usernam/projectname

    When you use docker run -d -p 80:8080 usernam/projectname instead, your webservice should be accessible on port 80 of your host.

    评论

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数