服务docker化

1、服务docker化

部署user-service

  • 编写Dockfile文件
#源镜像
FROM golang:1.13

#设置工作目录 WORKDIR $GOPATH/src RUN mkdir user-service COPY user-service $GOPATH/src/user-service #切换工作目录 WORKDIR $GOPATH/src/user-service RUN mkdir log #暴露端口 EXPOSE 9090 #最终运行docker的命令 ENTRYPOINT ["nohup","./user-service",">log/log.log", "&"] 
  • 编写build.sh脚本
#!/bin/bash
rm -rf user-service
go build .
docker build -t mic-user-service -f Dockerfile .

执行build.sh构建镜像

  • 编写start.sh脚本
#!/bin/bash
docker stop mic_user_service
docker rm mic_user_service
docker run -p 9090:9090 --name mic_user_service -v /usr/local/docker/micservice/user-service/log:/go/src/user-service/log -d mic-user-service

启动容器 ./start.sh

user-server服务依赖mysql服务,请访问 docker搭建mysql

部署user-edge-service

  • 编写Dockfile文件
#源镜像
FROM golang:1.13

#设置工作目录 WORKDIR $GOPATH/src RUN mkdir user-edge-service COPY user-edge-service $GOPATH/src/user-edge-service COPY config.ini $GOPATH/src/user-edge-service #切换工作目录 WORKDIR $GOPATH/src/user-edge-service RUN mkdir log #暴露端口 EXPOSE 9091 #最终运行docker的命令 ENTRYPOINT ["nohup","./user-edge-service",">/dev/null", "&"] 
  • 编写build.sh脚本
#!/bin/bash
rm -rf user-edge-service
go build .
docker build -t mic-user-edge-service:latest

执行build.sh构建镜像

  • 编写start.sh脚本
#!/bin/bash
docker stop mic_user_edge_service
docker rm mic_user_edge_service
docker run -p 9091:9091 --name mic_user_edge_service -v /usr/local/docker/micservice/user-edge-service/log:/go/src/user-edge-service/log -d mic-user-edge-service

启动容器 ./start.sh

  • 访问user-edge-service服务测试

    http://172.16.57.110:9091/adapi/login

使用docker-compose编排容器

上面的方式需要一个一个单独启动容器服务,我们可以是用docker-compose来管理容器

  • 安装docker-compose

    yum install docker-conpose

  • 编写docker-compose.yml

version: '3'

networks:
 default:  external:  name: micservice-network  services:  mysql:  image: mysql:5.7  ports: - 3306:3306  volumes: - /usr/local/docker/mysql/conf:/etc/mysql - /usr/local/docker/mysql/logs:/var/log/mysql - /usr/local/docker/mysql/data:/var/lib/mysql  environment: - TZ="Asia/Shanghai"  redis:  image: redis:5.0.4  ports: - 6379:6379  volumes: - /usr/local/docker/redis/logs:/usr/local/redis/logs - /usr/local/docker/redis/redis.conf:/etc/redis_default.conf - /usr/local/docker/redis/data:/data  environment: - TZ="Asia/Shanghai"  mongo:  image: mongo:latest  ports: - 27017:27017  volumes: - /usr/local/docker/mongo/data:/data/db - /usr/local/docker/mongo/conf/mongodb.cnf:/etc/mongo.conf  environment: - TZ="Asia/Shanghai" user-service:  image: mic-user-service:latest  ports: - 9090:9090  volumes: - /usr/local/docker/micservice/user-service/log:/go/src/user-service/log  environment: - TZ="Asia/Shanghai" user-edge-service:  image: mic-user-edge-service:latest  ports: - 9091:9091  links: - user-service  volumes: - /usr/local/docker/micservice/user-edge-service/log:/go/src/user-edge-service/log  environment: - TZ="Asia/Shanghai" 
  • 相关命令
#启动所有服务
 docker-compose up -d 
 #启动某个
 docker-compose up user-service -d 
 # 停止所有容器服务
 docker-compose stop
  • 访问user-edge-service服务测试

    http://172.16.57.110:9091/adapi/login

2、服务docker镜像仓库

公有仓库

这里使用阿里云镜像仓库

  • 先登录仓库

    docker login --username=weylau@xxxx registry.cn-shenzhen.aliyuncs.com

  • 标记本地镜像

    docker tag mic-user-service:1.0 registry.cn-shenzhen.aliyuncs.com/weylau/mic-user-service:1.0

  • 将镜像推向远程仓库

    docker push registry.cn-shenzhen.aliyuncs.com/weylau/mic-user-service:1.0

  • 修改build.sh构建完镜像直接推送到远程仓库

#!/bin/bash
rm -rf user-service
go build .
docker build -t registry.cn-shenzhen.aliyuncs.com/weylau/mic-user-service:latest -f Dockerfile .
docker push registry.cn-shenzhen.aliyuncs.com/weylau/mic-user-service:latest

私有仓库

通过registry创建

Docker 官方提供了一个搭建私有仓库的镜像 registry ,只需把镜像下载下来,运行容器并暴露5000端口,就可以使用了

docker pull registry:2

运行

docker run -d -v /usr/local/docker/registry:/var/lib/registry -p 5000:5000 --name myregistry registry:2

标记本地镜像

docker tag mic-user-edge-service:latest localhost:5000/mic-user-edge-service:1.0

push到私有仓库

docker push localhost:5000/mic-user-edge-service:1.0

harbor搭建(适合生产部署)

docker 官方提供的私有仓库 registry,用起来虽然简单 ,但在管理的功能上存在不足。 Harbor是一个用于存储和分发Docker镜像的企业级Registry服务器,harbor使用的是官方的docker registry(v2命名是distribution)服务去完成。harbor在docker distribution的基础上增加了一些安全、访问控制、管理的功能以满足企业对于镜像仓库的需求。

你可能感兴趣的:(服务docker化)