首頁  >  文章  >  後端開發  >  在 AWS 上使用 Docker 建立 Go 應用程式:建立用於新增和檢索專案的 RESTful 介面

在 AWS 上使用 Docker 建立 Go 應用程式:建立用於新增和檢索專案的 RESTful 介面

Mary-Kate Olsen
Mary-Kate Olsen原創
2024-10-28 07:51:02181瀏覽

簡介

雖然大多數 Go 應用程式都編譯成單一二進位文件,但 Web 應用程式還附帶模板、資產和設定檔;這些可能會不同步並導致錯誤的部署。
Docker 允許我們建立一個獨立的映像,其中包含應用程式運作所需的一切。在本文中,我們將學習如何使用安裝在實例上的 Docker 來部署 Go Web 應用程序,以及 Docker 如何協助您改善開發工作流程和部署流程。

我們需要的步驟如下:

- 啟動一個實例(您的機器)來建置 Docker
申請

- 在實例中安裝 Docker
- 在實例中安裝 Go
- 為您的 Go 應用程式建立程式碼檔案
- 應用程式測試

啟動一個實例(您的機器)來建置 Docker
申請

您可以找到與文章中描述的實例啟動和連接相同的步驟:

https://dev.to/zahraajawad/building-a-jupyter-notebook-environment-in-docker-for-data-analysis-on-aws-ec2-376i

注意: 確保選擇安全群組:

  • SSH 連接埠 22: 使用 SSH 存取並連接到實例
    遠端管理系統的協定。

  • HTTP 連接埠 8080: 要在此連接埠 (8080) 上執行 Go 應用程式以從 Internet 或本地網路存取它,必須開啟此連接埠。

- 在我們的實例中安裝 Docker

我們將建立的特定工作流程架構使用 Docker 來提供整合的工作流程環境。
因此透過 SSH 連接到實例並獲得 root 權限後,使用以下命令自動化安裝 Docker:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - && sudo add-apt-repository "deb [arch=amd64] https://download.docker. com/linux/ubuntu $(lsb_release -cs) stable" && sudo apt-get update && apt-快取策略docker-ce

Docker 體驗:執行一個簡單的測試命令 docker -v 來檢查 Docker 是否正常運作並查看 Docker 版本:

安裝 Go

您可以從Go官方網站下載安裝Go https://go.dev/dl/
wget https://golang.org/dl/go1.20.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.20.linux-amd64.tar.gz
echo 'export PATH=$PATH:/usr/local/go/bin' >>> ~/.bash_profile
來源 ~/.bash_profile

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items
哪裡:
wget https://golang.org/dl/go1.20.linux-amd64.tar.gz 是下載Go二進位。


sudo tar -C /usr/local -xzf go1.20.linux-amd64.tar.gz 是將 tarball 解壓縮到 /usr/local。

echo 'export PATH=$PATH:/usr/local/go/bin' >>> ~/.bash_profile 更新 PATH 環境變數。
和 source ~/.bash_profile 以應用對設定檔所做的變更

執行指令並透過指令 ls 驗證執行後,顯示下載的檔案:

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items
使用以下程式碼初始化 Go 應用程式:
go mod init my-go-app
Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

現在我們需要透過指令建立一個專案資料夾:
mkdir
然後透過指令更改目前目錄:
cd
所以執行是:

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

為您的 Go 應用程式建立程式碼檔案

main.go 檔案

我們建立一個名為 main.go 的新文件,其中包含以下函數和程式碼,我們將詳細解釋這些函數和程式碼,然後將所有程式碼放入 main.go 檔案中:

  • 要匯入必要的套件,我們使用以下程式碼:
import (
    "encoding/json"
    "log"
    "net/http"
    "github.com/gorilla/mux"
    "os"
)
  • 對於資料結構項:
type Item struct {
    ID   int    `json:"id"`
    Name string `json:"name"`
}

其中item是一個包含識別碼(ID)和名稱(Name)的資料結構。這些欄位使用標籤轉換為 JSON 格式(json:"id" 和 json:"name".

  • 專案變數
var items []Item

這是儲存在伺服器記憶體中的項目片段。

  • 透過 main 函數,除了引導檢索或新增元素以及顯示簡單 HTML 頁面的各種請求之外,還透過讀取連接埠(此處為連接埠 8080)來排列結構。
import (
    "encoding/json"
    "log"
    "net/http"
    "github.com/gorilla/mux"
    "os"
)
  • 函數 getItems 傳回 JSON 格式的項目清單。標頭中的內容類型設定為 application/json。
type Item struct {
    ID   int    `json:"id"`
    Name string `json:"name"`
}
  • createItem 函數將一個新項目加入項目清單。資料以 JSON 格式從請求正文中讀取,根據現有項目的數量為項目分配 ID,新增的項目以 JSON 形式傳回。
var items []Item
  • serveHome 函數顯示一個簡單的 HTML 頁面,其中包含歡迎訊息(歡迎使用 Go 應用程式)以及用於存取資料的連結。
func main() {
    port := os.Getenv("PORT")
    if port == "" {
        port = "8080"
    }

    router := mux.NewRouter()
    router.HandleFunc("/items", getItems).Methods("GET")
    router.HandleFunc("/items", createItem).Methods("POST")
    router.HandleFunc("/", serveHome).Methods("GET")

    log.Printf("Server is running on port %s...\n", port)
    log.Fatal(http.ListenAndServe(":"+port, router))
}

所以整個main.go檔案是:

func getItems(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    json.NewEncoder(w).Encode(items)
}

現在透過命令vim或nano建立main.go檔案並將上面的程式碼放入檔案中,這裡我們將使用命令nano:

nano main.go

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items
並通過代碼:

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

然後透過鍵盤ctrl x退出文件,然後按y(儲存檔案)然後按一下輸入

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

Dockerfile:

是一個文字文檔,其中包含使用者可以在命令列上調用以組裝圖像的所有命令。
Dockerfile 可以透過讀取 Dockerfile 中的指令來自動建立映像。

建立 Dockerfile:

使用 Docker 建置容器映像需要包含建置指令的 Dockerfile。
我們建立一個 Dockerfile,並透過命令 nano Dockerfile 以與之前相同的方式新增以下程式碼:

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

Dockerfile 命令詳細資訊可以在 docker 文件主頁上找到 https://docs.docker.com/guides/golang/build-images/

現在我們已經準備好了 Dockerfile,是時候為 Go 應用程式建立 Docker 映像了。該映像可以由官方 Docker 映像製作而成,它們是:

docker build -t my-go-app .

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items
鏡像建置成功,使用指令確認建置:
碼頭工人圖像

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

然後,為了在建造鏡像後運行容器,我們使用:

docker run -p 8080:8080 my-go-app

其中 8080 是 Web 伺服器的端口,因此執行運行是:

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

應用測試

- 透過curl指令測試Go應用程式

透過curl指令測試Go應用程式是否正常運作:

curl http://localhost:8080/items

curl http://:8080/items

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

執行為null,這表示應用程式正在運行,但我們還沒有資料。

要新增項目,請使用以下指令:

curl -X POST -H "Content-Type: application/json" -d '{"name": "item"}' http://localhost:8080/items

curl -X POST -H "Content-Type: application/json" -d '{"name": "item"}' http://:8080/items

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

所以執行新增:

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

我們可以再增加一個項目:

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

- 透過網頁測試 Go 應用

透過網頁測試Go應用程式是否正常運作,步驟如下:

  • 傳回實例並透過複選框選擇它。
  • 前往詳細資訊並複製公共 IPv4 位址。
  • 將連接埠為 8080 的公用 IPv4 位址貼到瀏覽器中,然後按 輸入。

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

網頁正在運行,當我們按下頁面上的items時,我們會獲得透過curl命令新增的項目。

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

或可按漂亮列印的複選框:

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

參考文獻:

  • https://dev.to/zahraajawad/building-a-jupyter-notebook-environment-in-docker-for-data-analysis-on-aws-ec2-376i
  • https://semaphoreci.com/community/tutorials/how-to-deploy-a-go-web-application-with-docker
  • https://hub.docker.com/_/golang
  • https://docs.docker.com/guides/golang/build-images/
  • https://github.com/gorilla/mux

以上是在 AWS 上使用 Docker 建立 Go 應用程式:建立用於新增和檢索專案的 RESTful 介面的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn