Centos7系统Docker环境下部署企业项目
1.简介
项目介绍:
系统:Centos7
环境:Docker
项目: Netcore2.1
数据库:Mysql
在部署前需要拉取三个官方镜像:
mysql,nginx,microsoft/dotnet
[root@localhost NETCore]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest d72169616e20 3 days ago 443MB
nginx latest 27a188018e18 11 days ago 109MB
microsoft/dotnet latest e268893be733 2 weeks ago 1.74GB
2.项目部署配置
项目路径:/root/Documents/NETCore
项目结构:
只显示主要配置的文件
├── database
└── jhgdsm.sql (数据库配置)
├── data.dll.config (数据库连接字符串)
├── Dockerfile (docker配置)
├── proxy.conf (nginx配置)
├── docker-compose.yml(compose配置)
各个配置文件说明如下:
1.jhgdsm.sql :
文件内容如下:
USE mysql;
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
DROP DATABASE IF EXISTS `jhgdsm`;
CREATE DATABASE jhgdsm;
USE jhgdsm;
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `jh_user`;
CREATE TABLE `jh_user` (
`id` varchar(36) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`account` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
`password` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`mtype` varchar(2) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '2',
`nickname` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`sex` varchar(2) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '1',
`mstatus` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT 'normal',
`company` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`email` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`mobile` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`img` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`createtime` datetime(0) NULL DEFAULT NULL,
`createuser_id` varchar(36) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`updatetime` datetime(0) NULL DEFAULT NULL,
`updateuser_id` varchar(36) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`logintime` datetime(0) NULL DEFAULT NULL,
`limitdevcount` int(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of jh_user
-- ----------------------------
INSERT INTO `jh_user` VALUES ('effd9698-01ab-49c8-9399-5f4ac7fd8d1b', 'admin', 'F5AAE2AA35E781EE3E1C03957B54F6F3', '1', '管理员11', '', 'normal', 'Giada', '', '', '', '2018-11-15 10:36:55', '', '2019-02-19 15:21:16', 'effd9698-01ab-49c8-9399-5f4ac7fd8d1b', '2019-04-10 17:09:36', 10);
#后面都是一些表的创建,忽略了
该文件是数据库的数据表结构和数据,用于创建镜像的时候初始化数据库。数据库名为:jhgdsm,密码123456。
2.data.dll.config
文件内容如下:
<?xml version="1.0" encoding="utf-8" ?>
<BaseSetting>
<MySqlConnection value="server=db;User ID=root;Password=123456;Database=jhgdsm;Allow User Variables=True;"></MySqlConnection>
</BaseSetting>
该文件就是mysql的连接字符串。注意“server=db”,后面会配置到。
3.Dockerfile
内容如下:
FROM microsoft/dotnet:latest
WORKDIR /app
COPY . /app
EXPOSE 5000
ENV ASPNETCORE_URLS http://*:5000
ENTRYPOINT ["dotnet","GDSMPlateForm.dll"]
FROM指定容器需要的镜像,GDSMPlateForm.dll是运行项目的dll文件,端口号是5000
4.proxy.conf
内容如下:
server {
listen 8080;
location / {
proxy_pass http://gdsm:5000;
}
}
监听docker下的8080端口,转发路径为gdsm:5000,注意“gdsm”,后面会配置到。
5.docker-compose.yml
内容如下:
version: '2'
services:
db:
container_name: gdsm.db
image: mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: 123456
volumes:
- ./mysql:/var/lib/mysql
- ./database:/docker-entrypoint-initdb.d/
gdsm:
container_name: gdsm.web
build: .
depends_on:
- db
links:
- db
reverse-proxy:
container_name: gdsm.proxy
image: nginx
depends_on:
- gdsm
ports:
- "80:8080"
volumes:
- ./proxy.conf:/etc/nginx/conf.d/default.conf
以上内容部署了3个服务:
db:数据库服务,data.dll.config文件的“server=db”相对应。
容器命名为:gdsm.db,
参数“MYSQL_ROOT_PASSWORD: 123456”是数据库密码。
./mysql:/var/lib/mysql:挂载当前项目下的mysql文件夹来持久化存储。
./database:/docker-entrypoint-initdb.d/:将/database目录下的文件拷贝到/docker-entrypoint-initdb.d目录下,也就是拷贝jhgdsm.sql文件。docker会遍历docker-entrypoint-initdb.d目录下所有的.sh和.sql后缀的文件并执行。
gdsm:web项目服务
容器命名为:gdsm.web
build: .表示根据当前路径来创建镜像,创建过程与Dockerfile相关。
depends_on:表示依赖于db服务。
links:表示与db服务连接。
reverse-proxy:nginx服务
容器命名为:gdsm.proxy
依赖于gdsm服务。
本机80端口映射到8080端口
./proxy.conf:/etc/nginx/conf.d/default.conf :挂载了当前项目下的proxy.conf文件作为反向代理配置文件。其中该文件下的 "http://gdsm:5000"的gdsm映射的是gdsm服务。
3.项目部署
[root@localhost ~]# cd /root/Documents/NETCore/
[root@localhost NETCore]# docker-compose up -d
Building gdsm
Step 1/6 : FROM microsoft/dotnet:latest
---> e268893be733
Step 2/6 : WORKDIR /app
---> Running in 4fc18b3680d0
Removing intermediate container 4fc18b3680d0
---> 43376cb667e1
Step 3/6 : COPY . /app
---> 75f4a926b38c
Step 4/6 : EXPOSE 5000
---> Running in 1e1876652a14
Removing intermediate container 1e1876652a14
---> 0bd0204fac76
Step 5/6 : ENV ASPNETCORE_URLS http://*:5000
---> Running in cbbb308d7100
Removing intermediate container cbbb308d7100
---> ab5978301ee1
Step 6/6 : ENTRYPOINT ["dotnet","GDSMPlateForm.dll"]
---> Running in 0c089e311d50
Removing intermediate container 0c089e311d50
---> 600f8e56ef40
Successfully built 600f8e56ef40
Successfully tagged netcore_gdsm:latest
WARNING: Image for service gdsm was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating gdsm.db ... done
Creating gdsm.web ... done
Creating gdsm.proxy ... done
[root@localhost NETCore]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9a2b8448e42a nginx "nginx -g 'daemon of…" 45 seconds ago Up 41 seconds 80/tcp, 0.0.0.0:80->8080/tcp gdsm.proxy
79aad0c9a764 netcore_gdsm "dotnet GDSMPlateFor…" 46 seconds ago Up 45 seconds 5000/tcp gdsm.web
6e8659acfb21 mysql "docker-entrypoint.s…" 47 seconds ago Up 46 seconds 3306/tcp, 33060/tcp gdsm.db
[root@localhost NETCore]#
完!