1.win10环境
2.下载建议迅雷
二.安装docker1.右击开始->应用与功能->启用Hyper-V和容器功能
2.安装docker
官方地址:下载docker
安装后打开docker,在命令行输入 docker version查看client和server信息
3.安装mingw使用make
下载地址: mingw 并加上环境变量
三.docker 部署1.交叉编译
创建makefile文件,代码如下:主要使用 build-linux
# Go parameters
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get
PROJECT_DIR=..
BINARY_DIR=dist
BINARY_NAME=$(BINARY_DIR)/ams_backend
BINARY_LINUX=$(BINARY_NAME)_linux
all: test build
build:
$(GOBUILD) -o $(BINARY_NAME) -v $(PROJECT_DIR) && cp -rf $(PROJECT_DIR)/config $(PROJECT_DIR)/docs $(BINARY_DIR)
test:
$(GOTEST) -v $(PROJECT_DIR)/...
test_coverage:
$(GOTEST) $(PROJECT_DIR)/... -coverprofile=coverage.out
clean:
$(GOCLEAN)
rm -f $(BINARY_NAME)
rm -f $(BINARY_LINUX)
run:
$(GOBUILD) -o $(BINARY_NAME) -v $(PROJECT_DIR)/...
./$(BINARY_NAME)
tidy:
go mod tidy
vet:
go vet
lint:
golangci-lint run --enable-all
# Cross compilation
build-linux:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -o $(BINARY_LINUX) -v $(PROJECT_DIR) && cp -rf $(PROJECT_DIR)/config $(PROJECT_DIR)/docs $(BINARY_DIR)
2.服务器镜像配置文件
创建ams.dockerfile
dist文件夹包含 make build-linux的文件和服务器配置文件
复制文件夹到镜像工作路径
设置命令CMD
初始化服务器,当前服务器需要参数:init -c ${E_CONFIG_FILE}
启动服务器,当前服务器需要参数 :server -c ${E_CONFIG_FILE}
#-------------------------------------------------------------------------------------------------------------
# Copyright (c) xxx. All rights reserved.
#-------------------------------------------------------------------------------------------------------------
FROM alpine:3.15.0
ARG RUN_ENV=test
ARG BIN_DIR=dist
ARG BIN_FILE=ams_backend_linux
ARG CONFIG_FILE=settings.${RUN_ENV}.yml
ARG WORK_DIR=/root
ENV E_WORK_DIR=${WORK_DIR}
ENV E_BIN_FILE=${BIN_FILE}
ENV E_CONFIG_FILE=config/${CONFIG_FILE}
COPY ${BIN_DIR}/ ${WORK_DIR}/
CMD ["sh", "-c", "cd ${E_WORK_DIR} && ./${E_BIN_FILE} init -c ${E_CONFIG_FILE} && ./${E_BIN_FILE} server -c ${E_CONFIG_FILE}"]
3.docker-compose 配置文件
创建docker-compose.yml
容器配置和镜像build配置
基础镜像大概使用了nginx,mariadb, redis,和前面的服务器基础镜像apline
version: "3.9"
services:
# 网关服务,转发 文件服务请求 和 ams backend
ams_proxy:
depends_on:
- ams_backend
- nise_fileserv
image: nginx:1.21.5-alpine
volumes:
- ./data/nginx/conf.d:/etc/nginx/conf.d:ro
- ./data/nginx/logs:/var/log/nginx
networks:
- ams-backend
ports:
- "8080:80"
deploy:
placement:
constraints:
- node.role == manager
# 数据库
ams_db:
image: mariadb:10.3.32
volumes:
# - ./data/mariadb/conf/conf.d:/etc/mysql/conf.d:ro
# - ./data/mariadb/conf/mariadb.conf.d:/etc/mysql/mariadb.conf.d:ro
- ./data/mariadb/data:/var/lib/mysql
- ./data/mariadb/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d:ro
env_file:
- .env
environment:
# TODO: 使用输入加密密码
- MYSQL_ROOT_PASSWORD=nzai123!@#
- MYSQL_TZ=Asia/Shanghai
networks:
- ams-backend
expose:
- "3306"
ports:
- "13306:3306"
restart: always
# https://hub.docker.com/_/redis?tab=description
# redis
ams_redis:
image: redis:6.2.6-alpine
volumes:
- ./data/redis/data:/data
# TODO: password and other config items should be set
# - ./data/redis/conf:/usr/local/etc/redis
# command: redis-server /usr/local/etc/redis/redis.conf --save 300 1 --loglevel warning
command: redis-server --save 300 1 --loglevel warning
networks:
- ams-backend
ports:
- "16379:6379"
restart: always
# 后台服务
ams_backend:
depends_on:
- ams_db
image: '${DOCKER_IMAGE_AMS_BACKEND?Variable not set}:${TAG-latest}'
env_file:
- .env
environment:
- AMS_DB_HOST=ams_db
- AMS_DB_USER_HOST=ams_db
networks:
- ams-backend
expose:
- "8003"
restart: always
build:
context: .
dockerfile: ams.dockerfile
args:
RUN_ENV: ${RUN_ENV-test}
# 网络配置
networks:
ams-backend:
external: false
4.使用shell开始build
创建build.sh文件:docker-compose创建镜像配置
#! /usr/bin/env sh
# Exit in case of error
set -e
TAG=${TAG?Variable not set} \
DOCKER_IMAGE_AMS_BACKEND=${DOCKER_IMAGE_AMS_BACKEND?Variable not set} \
docker-compose \
-f docker-compose.yml -p ams-backend \
build
创建v1.0_build.sh文件:更新依赖,交叉编译,执行build.sh创建镜像
#! /usr/bin/env sh
# Exit in case of error
set -e
cur_dir=$(pwd)
# update dependencies
cd .. && go mod tidy && go mod vendor
cd "${cur_dir}"
# read Makefile
make build-linux
# execute build.sh with params
TAG=v1.0 DOCKER_IMAGE_AMS_BACKEND=nise/ams/backend ./build.sh
5.开启容器服务组
创建v1.0_start.sh文件:
#! /usr/bin/env sh
# Exit in case of error
TAG=v1.0 DOCKER_IMAGE_AMS_BACKEND=nise/ams/backend docker-compose -f docker-compose.yml -p nise_ams up -d
四.部署结果
1.nginx,redis,mariadb,ams/backend四个镜像
2. 一个容器组,里面这四个容器
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)