侧边栏壁纸
博主头像
SeaDream乄造梦

Dream,Don't stop a day of hard and don't give up a little hope。 ——不停止一日努力&&不放弃一点希望。

  • 累计撰写 63 篇文章
  • 累计创建 21 个标签
  • 累计收到 14 条评论

目 录CONTENT

文章目录

docker-compose搭建服务器环境,部署go项目

SeaDream乄造梦
2024-08-19 / 0 评论 / 0 点赞 / 713 阅读 / 4,044 字
温馨提示:
亲爱的,如果觉得博主很有趣就留下你的足迹,并收藏下链接在走叭

1.安装docker

安装docker请看上文

2.安装docker-compose

2.1.安装pip3

yum -y install python3-pip
pip3 install --upgrade pip

2.2.安装docker-compose

pip3 install docker-compose

#如果安装不下来使用下面
pip3 install -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com docker-compose

如果出现报错:
ERROR: Cannot uninstall requests 2.25.1, RECORD file not found. Hint: The package was installed by rpm.
则执行下面的命令:
sudo yum remove python3-requests
最后再次执行安装docker-compose:
pip3 install docker-compose

查看docker-compose版本:

docker-compose version

3.安装基础环境

docker-compose up -d 运行下面文件

version: '3'
services:
  redis:
    image: redis
    ports:
      - 6379:6379
    restart: always
    privileged: true
    container_name: redis-master
    hostname: redis
    volumes:
      - /xxx/xxx/redis/data:/data
      - /xxx/xxx/redis/conf:/etc/redis

  rabbit-mq:
    image: rabbitmq
    ports:
      - 5672:5672
      - 15672:15672
    restart: always
    privileged: true
    container_name: rabbit-mq
    environment:
      RABBITMQ_DEFAULT_USER: user
      RABBITMQ_DEFAULT_PASS: "xxxx"

  mysql:
    image: mysql:8.0
    ports:
      - 3306:3306
    restart: always
    container_name: mysql-master
    environment:
      MYSQL_ROOT_PASSWORD: "xxxx"
      TZ: "Asia/Shanghai"
    volumes:
      - /xxx/xxx/mysql/cnf:/etc/mysql/conf.d
      - /xxx/xxx/mysql/data:/var/lib/mysql
      - /xxx/xxx/mysql/log:/var/log

  nginx:
    image: nginx
    ports:
      - 80:80
      - 443:443
      - 8080:8080
    restart: always
    privileged: true
    container_name: my-nginx
    volumes:
      - /xxx/xxx/nginx/log:/var/log/nginx
      - /xxx/xxx/nginx/conf.d:/etc/nginx/conf.d
      - /xxx/xxx/nginx/cert:/etc/nginx/cert
      - /xxx/xxx/server/html:/xxx/xxx1/server/html
     -  /xxx/xxx1/server/html:/xxx/xxx1/server/html
nginx初始配置
upstream  zhijian_mini {
  server  172.17.0.1:7788;
}

upstream  zhijian_admin {
  server  172.17.0.1:8084;
}

server {
   listen       8080;

   client_max_body_size 10M;   


  location / {
     root /xxx/xxx1/server/html/dist/;
     try_files $uri $uri/ /index.html;
  }  
}

server {
   listen       80;
   listen 443 ssl;
   server_name client.zzazhijian.com;

   ssl_certificate /etc/nginx/cert/client.xxx.com.pem;
   ssl_certificate_key /etc/nginx/cert/client.xxx.com.key;

   #ssl配置
   ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
   ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
   ssl_prefer_server_ciphers on;
   ssl_session_cache shared:SSL:10m;
   ssl_session_timeout 10m;
   
   client_max_body_size 10M;   


  location / {
     root /xxx/xxx/server/html/dist/;
     try_files $uri $uri/ /index.html;
  }  

  location /zjian/ {
     proxy_set_header HOST $host;
     proxy_set_header X-Forwarded-Proto $scheme;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_pass http://xxx_mini/zxx/;

  }

  location /v1/ {
     proxy_set_header HOST $host;
     proxy_set_header X-Forwarded-Proto $scheme;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_pass http://xxx_admin/vxx/;

  }

}



mysql初始配置
[mysqld]
## 设置server_id,注意要在同一局域网内唯一
server-id=1
## 开启binlog
log-bin=mysql-bin
## binlog缓存
binlog_cache_size=1M
## binlog格式(mixed、statement、row,默认格式是statement)
binlog_format=mixed

redis初始配置
# 注释掉 所有IP都可以访问redis服务
#bind 127.0.0.1 -::1

# 关闭保护模式
protected-mode no

# 端口号
port 6379

# 改为no 后台进程的模式,避免跟docker的-d命令冲实
daemonize no

pidfile /var/run/redis_6379.pid

loglevel notice

logfile ""

# 登录密码
requirepass xxxx

# 主节点密码
masterauth xxxx

# 持久化
# 开启后,Redis会把每次写入的数据在接收后都写入 appendonly.aof 文件,每次启动时Redis都会先把这个文件的数据读入内存里
appendonly yes



4.部署项目

本地goland中,设置go运行环境为linux

 go env -w  GOOS=linux 

在打包(在main当前目录下执行打包命令)

go build ./main

将打包后的二进制文件和配置文件上传至服务器,然后编写docker-compose和dockerfile文件
image.png

docker-compose文件

version: '3'
services:
  zhi-client:
    build:
      context: ./
    restart: always
    ports:
      - 8088:8088
    environment:
      TZ: "Asia/Shanghai"
    container_name: x-client
    hostname: x-client
    image: x-client
    volumes:
      - /usr/share:/usr/share
      - /rrr/xxx/xxx-web/cert:/rrr/xxx/cert

dockerfile文件

FROM alpine:latest

ENV TZ=Asia/Shanghai

# 设置宿主机的时间 软连接 到容器内
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# 创建必要的目录
RUN mkdir -p /rrr/xxx/config

WORKDIR /rrr/xxx

# 将可执行文件和配置文件复制到容器内
COPY ./main ./
COPY ./config.yaml ./config

# 确保可执行文件具有执行权限
RUN chmod +x main

# 暴露端口
EXPOSE 8084

# 启动程序,假设您的程序使用 -c 参数来指定配置文件
ENTRYPOINT ["./main", "-c", "./config/config.yaml"]
CMD ["|", "tee", "-a", "log/app.log"]

运行docker-compose up -d --build
打包镜像并启动服务

start.sh

docker-compose down
docker rmi xx-xx:latest
docker-compose up -d --build
docker logs -f xx-xx
0

评论区