猿问

如何在一个代码库中为多个服务器制作容器

我有一个存储库,其中有多个服务器正在运行。像这样的结构


// Golang Apps

- account = port 4001 

- event = port 4002

- place = port 4003


// Node js

- gateway = port 4000

我通常使用这样的脚本在本地运行


// script.sh here:

#!/bin/bash


EnvAPP="${ENV_APP:-dev}"


function cleanup {

    kill "$ACCOUNTS_PID"

    kill "$EVENTS_PID"

    kill "$PLACES_PID"

}

trap cleanup EXIT


go build -tags $EnvAPP -o ./tmp/srv-accounts ./cmd/server/accounts

go build -tags $EnvAPP -o ./tmp/srv-events ./cmd/server/events

go build -tags $EnvAPP -o ./tmp/srv-places ./cmd/server/places


./tmp/srv-accounts &

ACCOUNTS_PID=$!


./tmp/srv-events &

EVENTS_PID=$!


./tmp/srv-places &

PLACES_PID=$!


sleep 1


node ./cmd/gateway/index.js

有没有可能我为这种情况创建一个 Docker 文件进入生产环境?在这种情况下,我应该在 Docker 文件中运行 script.sh 吗?我应该在多克文件中使用的图像怎么样?我不知道这种情况使用docker,因为在一个代码库中运行多个服务器,并且问题还端口运行服务器


也许你们中的一个人曾经有过这种情况?知道如何解决这个问题会很棒


在这种情况下,我正在使用图形QL联合( Go ),所以我有多个服务和网关 ( NodeJS )


我想针对此问题将其部署到生产环境中


梵蒂冈之花
浏览 57回答 1
1回答

烙印99

为此,您需要四个单独的 Dockerfile,才能启动四个具有四个不同程序的独立容器。Go 组件多克文件可以相当简单:# Dockerfile.accountsFROM golang:1.16 AS buildWORKDIR /appCOPY . .ARG ENV_APP=devRUN go build -tags "$ENV_APP" -o /accounts ./cmd/server/accountsFROM ubuntu:20.04COPY --from=build /accounts /usr/local/binCMD accounts(如果除了正在构建的特定命令目录之外,这三个映像实际上是相同的,您也可以将其作为传递。我假设这些包需要源目录中其他位置的包,如 a 或其他任何内容,这将要求 Dockerfile 位于顶层。ARG./cmd/server/*./pkg/support由于您的脚本只运行这四个程序,我通常建议使用 Docker Compose 作为同时启动四个容器的一种方式。“使用已知选项启动一些容器”是 Compose 所做的唯一事情,但它可以完成脚本所做的一切。# docker-compose.ymlversion: '3.8'services:  accounts:    build:      context: .      dockerfile: Dockerfile.accounts  events:    build:      context: .      dockerfile: Dockerfile.events  places:    build:      context: .      dockerfile: Dockerfile.places  gateway:    build:      context: .      dockerfile: Dockerfile.gateway      # (Since a Node app can't reuse Go code, this could also      # reasonably be `build: cmd/gateway` using a      # `cmd/gateway/Dockerfile`)    ports:      - 3000:3000只需运行即可在前台启动所有四个容器;一旦它启动,按+将停止它们。您可以将网关配置为使用其他容器名称 , 作为主机名; 例如。docker-compose upCtrlCaccountseventsplaceshttp://accounts/graphql您还可以按原样调整启动器脚本。运行而不是构建映像,启动容器(可能具有固定的 s)来停止它们。您应该使用网络和它们上的所有容器,以便它们可以以与撰写设置相同的方式进行通信。docker buildgo builddocker run--namedocker stop && docker rmdocker network createdocker run --net
随时随地看视频慕课网APP

相关分类

Go
我要回答