项目介绍
# 简介
# 整体架构
eggjs框架新项目cloud-apps之Docker容器化
一个主项目:Dockerfile + docker-compose
四个微服务项目:Dockerfile
- neocloud:主项目,书内容管理系统CMS;
- **book_server:**网页生产epub项目;
- **neocloud-statistics:**统计服务;
- **onyx_drm:**下单后的图书加密服务;
- **onyx-pay-micro:**支付服务;
# 内容管理表结构设计
内容管理表结构设计&lib层级设计mysql
# 内容管理新表结构
电子书包(内容管理)新表结构
# 各大支付系统对接
# alipay
# stripe
# 其他相关
配置中碰到的主要问题有:
1:压缩优化;
2:多容器相互依赖,内容资源共享问题;
配置步骤总括:
- 拷贝docker文件的基础脚本;
- 把.dockerignore拷贝到上一级目录;
- 修改没有项目对应的脚本;
简单eggjs服务容器脚本;
build.sh
docker build -t ${tagName} . 来构建image文件;示例:
sudo docker build -t mytomcat .
sudo docker run -p 8080:8080 mytomcat //在这里你可以加上特定参数
2
# sudo docker rmi $(sudo docker images -f "dangling=true" -q) //rm none images
# sudo docker build -t test_boilerplate .
sudo docker build -t test_boilerplate -f docker/Dockerfile .
# docker run --rm -d -p 8001:8001/tcp test_boilerplate:latest
# docker run --rm -it -p 8001:8001/tcp test_boilerplate:latest
2
3
4
5
Dockerfile; 多参照https://hub.docker.com/search 搜索后的例子配置:
#FROM node:8.6-alpine
FROM node:10-alpine
RUN apk --update add tzdata \
&& cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone \
&& apk del tzdata
# USER root
#最后发现分开多层是没有用,已经有层次容器的话,不会再次更新.导致更新不及时;
RUN apk update \
&& apk add --upgrade apk-tools \
&& apk upgrade --available \
&& apk add curl bash tree
RUN apk --update upgrade && \
apk add --no-cache curl
#ENTRYPOINT ["/usr/bin/curl"]
RUN rm -r /usr/share/man && \
find / -type f -iname \*.apk-new -delete && \
rm -r /var/cache/apk
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json /usr/src/app/
RUN npm i --registry=https://registry.npm.taobao.org --production
# RUN npm i
COPY . /usr/src/app
# COPY ./ /usr/src/app
# COPY . .
#ADD ./ ./
EXPOSE 8001
# ENTRYPOINT [ "npm", "start" ]
# CMD npm start
CMD npm run docker
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
.dockerignore
.git
.idea/
.vscode/
node_modules/
logs/
run/
test/
docker/
typings/
.dockerignore
Dockerfile
README.md
README.zh-CN.md
2
3
4
5
6
7
8
9
10
11
12
13
反向设置处理:[推荐]
*
!app/
!config/
!lib/
!pem/
!app.js
!package.json
!.sequelizerc
2
3
4
5
6
7
8
Dockerignore: Ignore everything except a file and the Dockerfile (opens new window)
# Ignore everything
**
# Allow files and directories
!/file.txt
!/src/**
# Ignore unnecessary files inside allowed directories
# This should go after the allowed directories
**/*~
**/*.log
**/.DS_Store
**/Thumbs.db
2
3
4
5
6
7
8
9
10
11
注意这里全部拷贝和部分拷贝;
COPY package.json /usr/src/app/
RUN npm i --registry=https://registry.npm.taobao.org --production
# RUN npm i
# COPY . . # 全部拷贝,跟.dockerignore配合使用;
ADD app app # 部分拷贝,不用配置.dockerignore,指定目录就可以,但是要拷贝的目录多了了话,会影响大小;
ADD config config # 可以考虑,这一句跟上面一句合并处理;
2
3
4
5
6
volumes处理时,可以通过这个简单的处理,不用写脚本:
$PWD 当前文件夹
nginx:
image: nginx:latest
# 端口映射
ports:
- "80:80"
# 依赖关系 先跑php
depends_on:
- "php"
# 数据卷
volumes:
# 映射主机./conf.d目录到容器/etc/nginx/conf.d目录
- "$PWD/conf.d:/etc/nginx/conf.d"
- "$PWD/html:/usr/share/nginx/html"
networks:
- app_net
# 容器名称
container_name: "compose-nginx"
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
docker-compose处理部分
docker-compose.yml.bak
version: '2.2'
services:
egg:
build: ./
image: egg
ports:
- 7001:7001
networks:
- docker_egg
mysql:
image: mysql:5.7.13
restart: always
ports:
- 3306:3306
volumes:
- ./data:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=mysql
networks:
- docker_egg
networks:
docker_egg:
driver: bridge
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
pay-micro项目
启动脚本及docker-compose脚本:
start.sh
#!/bin/bash
# 使用说明:
# 启动脚本:sudo bash ./docker/build.sh
sudo docker build -t pay-micro -f docker/Dockerfile .
2
3
4
build.sh
#!/bin/bash
# 使用说明:
# 启动脚本:sudo bash ./docker/start.sh
sudo mkdir -p /srv/pay-micro/config/
sudo cp ./docker/config/config.prod.js /srv/pay-micro/config/config.prod.js
sudo docker-compose -p pay-micro-compose -f docker/docker-compose.yml rm -f
sudo docker-compose -p pay-micro-compose -f docker/docker-compose.yml up -d
2
3
4
5
6
7
Dockerfile
FROM node:10-alpine
RUN apk --update add tzdata \
&& cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone \
&& apk del tzdata
RUN apk add curl bash tree && \
rm -r /usr/share/man && \
rm -r /var/cache/apk
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json /usr/src/app/
RUN npm i --registry=https://registry.npm.taobao.org --production
COPY . .
EXPOSE 7004
CMD npm run docker
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
docker-compose.yml
version: '2.2'
services:
mongodb:
image: mongo:3.4
ports:
- 27084:27017
volumes:
- /srv/pay-micro/mongodb:/data/db
networks:
- docker_egg
restart: always
pay-micro:
depends_on:
- mongodb
image: pay-micro
ports:
- 7084:7004
volumes:
- /srv/pay-micro/uploads:/usr/src/app/uploads
- /srv/pay-micro/config/config.prod.js:/usr/src/app/config/config.prod.js
networks:
- docker_egg
restart: always
networks:
docker_egg:
driver: bridge
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
pay-micro项目
跟pay类似,就是添加了redis及跟主项目cms有关联;
启动脚本及docker-compose脚本:
redis:
image: redis:5.0-alpine
volumes:
- /srv/neocloud-statistics/redis:/data
networks:
- docker_egg
restart: always
depends_on:
- redis
- mongodb
2
3
4
5
6
7
8
9
10
11
book_server项目
项目依赖graphicsmagick书本封面处理库;Dockerfile得配置安装库;
只有项目自己,本来是可以直接用dockerfile就可以处理,但考虑到文件路径映射.还是用docker-copose.yml处理;
启动脚本及docker-compose脚本:
Dockerfile安装graphicsmagick库:
方式一: as gm的方式有问题,最后直接安装alpine-node-imagemagick,但是这种加载方式推荐;
https://hub.docker.com/r/jameskyburz/graphicsmagick-alpine/dockerfile
FROM jameskyburz/graphicsmagick-alpine:v3.0.0 as gm
FROM node:10-alpine
# RUN apk add --no-cache imagemagick graphicsmagick
COPY --from=gm /usr/ /usr/
# FROM mmarcant/alpine-node-imagemagick
2
3
4
5
mmarcant/alpine-node-imagemagick 的Dockerfile
多了个ffmpeg库;
# Start from base node alpine image
FROM node:10-alpine
# install imagemagick, graphicsmagick, ffmgeg and
RUN apk add --no-cache imagemagick ffmpeg graphicsmagick
# install bash
RUN apk add --no-cache bash
# try fix
RUN ln -s /bin/sh /bin/source
ENTRYPOINT ["/bin/sh", "-c"]
2
3
4
5
6
7
8
9
方式二:
# FROM jameskyburz/graphicsmagick-alpine:v3.0.0 as gm
FROM node:10-alpine
# FROM mmarcant/alpine-node-imagemagick
RUN apk add --no-cache imagemagick graphicsmagick
# COPY --from=gm /usr/ /usr/
2
3
4
5
version: '2.2'
services:
book_server:
#image: book_server
container_name: book_server
build:
context: ../
dockerfile: ./docker/Dockerfile
args:
t: 'book_server'
ports:
- 7005:7005
volumes:
- /srv/book_server/uploads:/usr/src/app/uploads
- /srv/book_server/config/config.prod.js:/usr/src/app/config/config.prod.js
- /srv/book_server/book_epubs:/root/srv/book_server
networks:
- docker_egg
restart: always
networks:
docker_egg:
driver: bridge
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#image: book_server
container_name: book_server
build:
context: ../
dockerfile: ./docker/Dockerfile
一起处理Dockerfile,不用再去触发build.sh打包容器,其再次启动;
缺点就是不能控制生成book_server 镜像的项目名字,不利于复用;
最后通过参数处理:t: 'book_server',发现没有用;
最后可以通过container_name: book_server配置,及不要配置docker-compose -p 设置name就可以;
也还是不可以;
最最后,还是回归到这里的;直接配置image就可以;
build: ./pay-micro # image指定build Dockerfile生成镜像的名称 image: pay-micro:1.0
其名字变为:bookservercompose_book_server
之前期望名字为: sudo docker build -t book_server -f docker/Dockerfile .
2
book_server:
image: book_server
#container_name: book_server
build:
context: ../
dockerfile: ./docker/Dockerfile
args:
buildno: 1
ports:
- 7005:7005
volumes:
- /srv/book_server/uploads:/usr/src/app/uploads
- /srv/book_server/config/config.prod.js:/usr/src/app/config/config.prod.js
- /srv/book_server/book_epubs:/root/srv/book_server
networks:
- docker_egg
restart: always
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
得找个命名去重新触发book_server镜像重新编译:
docker-compose build
Successfully tagged bookservercompose_book_server:latest
WARNING: Image for service book_server was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
2
onyx_drm项目
项目依赖java命令,linux安装openJDK8版本;Dockerfile得配置安装库;
因drm.jar是在1.8版本环境下编译的,使用得配置安装;
# add other lib
RUN apk update && \
apk upgrade && \
apk add openjdk8 && \
rm -r /usr/share/man && \
rm -rf /var/cache/apk/*
ENV JAVA_HOME /usr/lib/jvm/java-1.8-openjdk
2
3
4
5
6
7
docker-compose.yml
version: '2.2'
services:
onyx_drm:
#image: onyx_drm
container_name: onyx_drm
build:
context: ../
dockerfile: ./docker/Dockerfile
ports:
- 7003:7003
volumes:
- /srv/onyx_drm/uploads:/usr/src/app/uploads
- /srv/onyx_drm/config/config.prod.js:/usr/src/app/config/config.prod.js
- /srv/onyx_drm/libs:/root/.onyx/libs # 因涉及到加密,先提前拷贝的libs目录
networks:
- docker_egg
restart: always
networks:
docker_egg:
driver: bridge
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
start.sh
#!/bin/bash
# 使用说明:
# 启动脚本:sudo bash ./docker/start.sh
sudo mkdir -p /srv/onyx_drm/config/
# sudo cp /lib/drm.jar /srv/onyx_drm/libs/
sudo cp ./docker/config/config.prod.js /srv/onyx_drm/config/config.prod.js
sudo docker-compose -f docker/docker-compose.yml rm -f
sudo docker-compose -p onyx_drm-compose -f docker/docker-compose.yml up -d
2
3
4
5
6
7
8
neocloud项目
项目依赖pdftk,openJDK8, 加载在线的git库下载(要用https方式),得安装git相关库;
"egg-passport-wechat": "https://github.com:jige666/egg-passport-wechat.git",及本地父目录加载处理;(做多层处理)"onyx-alioss": "../onyx_alioss",
volumes映射目录也记得修改:
volumes:
- /srv/neocloud/logs:/root/logs
- /srv/neocloud/uploads:/usr/src/app/neocloud/uploads
- /srv/neocloud/config/config.json:/usr/src/app/neocloud/database/config.json
- /srv/neocloud/config/config.prod.js:/usr/src/app/neocloud/config/config.prod.js
2
3
4
5
FROM node:10-alpine
RUN apk --update add tzdata \
&& cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone \
&& apk del tzdata
RUN apk --update add curl bash tree && \
apk add openssh git && \
rm -r /usr/share/man && \
rm -r /var/cache/apk
#RUN apk --update add curl bash tree && \
# apk add openssh git && \
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY tmp /usr/src/app/onyx_alioss
# COPY package.json /usr/src/app/
# COPY . .
COPY package.json /usr/src/app/neocloud/
COPY . ./neocloud/
RUN cd /usr/src/app/neocloud && npm i --registry=https://registry.npm.taobao.org --production
EXPOSE 7002
CMD cd neocloud && npm run docker
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
CMD cd neocloud && \
NODE_ENV=production npx sequelize db:migrate && \
npx sequelize db:seed:all && \
npm run docker
2
3
4
node-pdftk https://hub.docker.com/r/r3ddox/node-pdftk/dockerfile
FROM node:10-alpine
RUN apk update && apk upgrade && apk add --no-cache \
alpine-sdk \
python \
pdftk \
ocaml \
libelf-dev
2
3
4
5
6
7
Dockerfile除安装calibredb安装配置好的版本:
FROM node:10-alpine
RUN apk --update add tzdata \
&& cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone \
&& apk del tzdata
RUN apk --update add curl bash tree && \
apk add openssh git && \
rm -r /usr/share/man && \
rm -r /var/cache/apk
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY tmp /usr/src/app/onyx_alioss
COPY package.json /usr/src/app/neocloud/
COPY . ./neocloud/
RUN cd /usr/src/app/neocloud && npm i --registry=https://registry.npm.taobao.org --production
# RUN NODE_ENV=production npx sequelize db:migrate && npx sequelize db:seed:all
EXPOSE 7002
# NODE_ENV=production npx sequelize db:migrate && \
# NODE_ENV=production npx sequelize db:seed:all && \
CMD cd neocloud && \
npm run docker
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21