Skip to content

Commit

Permalink
修改docker配置方式和脚本等
Browse files Browse the repository at this point in the history
  • Loading branch information
ihahoo committed Jun 26, 2017
1 parent 8c93be4 commit 5efcefb
Show file tree
Hide file tree
Showing 13 changed files with 144 additions and 44 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ npm-debug.log*
node_modules
.npm
coverage
build
build/Release
.module-cache
.DS_Store
.idea
*.iml
.vscode

/lib
/data
data/redis
data/postgresql
6 changes: 0 additions & 6 deletions .tern-project

This file was deleted.

14 changes: 0 additions & 14 deletions Dockerfile

This file was deleted.

67 changes: 67 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,69 @@
# api-server-restify
A Node.js RESTful API server with restify

基于Node.js(版本:8), Restify, Docker, Postgresql, Redis开发后端RESTful API接口的脚手架

## 安装
- 安装[Node.js](https://nodejs.org) (请安装8的最新版)
- 安装[Docker](https://www.docker.com/) (请设置阿里云等镜像加速器, 不用加速器拉镜像速度很慢)
- 安装[Docker Compose](https://github.com/docker/compose/releases)

````
$ git clone https://github.com/hahoocn/api-server-restify
````
根据项目情况,修改镜像名称和配置:

- 开发环境:修改`/deploy/development/docker-compose.yml``/docker-compose.yml`中的`image: my-api-dev` 名称,默认是`my-api-dev`
- 生产和测试环境:
* 修改`/deploy/production/docker-compose.yml``/deploy/staging/docker-compose.yml`配置,镜像名称请修改配置文件中`api`部分的`image: my-api:latest`,比如可使用私有镜像域名为前缀的名称。
* 修改`/package.json``scripts`部分的`docker:build``docker:build-staging`中的`-t my-api:staging`镜像名称。
- 修改Postgresql数据库密码: 请修改以下配置文件相关Postgresql密码部分
* `/config/app/config.json`
* `/docker-compose.yml`
* `/deploy/production/docker-compose.yml`
* `/deploy/staging/docker-compose.yml`
- 修改Redis密码: 请修改以下配置文件相关Redis密码部分
* `/config/app/config.json`
* `/config/redis/redis.conf`中的`requirepass`部分

````
$ npm run docker:build-dev
````

## 构建开发环境
````
$ npm run docker:build-dev
````
生成开发用的docker镜像,并且通过镜像中的环境初始化安装node包,生成`node_modules`文件夹

## 启动开发环境
````
$ npm run docker:dev
````
或者在项目根文件夹下运行命令:
````
$ docker-compose up -d
````

通过docker-compose启动api web, postgres, redis三个容器,api web端口为8080, postgres端口为5432, redis端口为6379。数据库可通过本地客户端工具连接进行操作和调试。

测试接口是否正常启动:请访问`http://localhost:8080/hello`看是否有反馈信息。

## 停止开发环境
````
$ npm run docker:dev-stop
````
或者在项目根文件夹下运行命令:
````
$ docker-compose down
````

## 生成测试环境docker镜像
````
$ npm run docker:build-staging
````

## 生成生产环境docker镜像
````
$ npm run docker:build
````
16 changes: 16 additions & 0 deletions build/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM node:8-alpine

ENV appdir /usr/src/app
ARG port=8080

RUN mkdir -p $appdir
WORKDIR $appdir

ADD package.json .
RUN npm config set registry https://registry.npm.taobao.org && npm install --only=production

ADD ./config/app ./config/app
ADD ./config/redis ./config/redis
ADD ./lib ./lib

EXPOSE $port
3 changes: 1 addition & 2 deletions Dockerfile-dev → deploy/development/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
FROM node:6.10.0
FROM node:8

ENV appdir /usr/src/app
RUN mkdir -p $appdir
WORKDIR $appdir

RUN npm config set registry https://registry.npm.taobao.org && npm install pm2 -g
ADD . .
9 changes: 9 additions & 0 deletions deploy/development/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: '2'
services:
api:
build:
context: .
dockerfile: Dockerfile
image: my-api-dev
volumes:
- ../../:/usr/src/app
10 changes: 6 additions & 4 deletions docker-compose.prod.yml → deploy/production/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
version: '2'
services:
api:
image: hahoo/node-hello:latest
image: my-api:latest
command: npm start
ports:
- 80:8080
restart: always
volumes:
- ./config/app:/usr/src/app/config/app
- ./logs/app:/usr/src/app/logs/app
- ./logs:/usr/src/app/logs
depends_on:
- db
- redis
db:
image: postgres:9.6.2-alpine
image: postgres:9.6.3-alpine
restart: always
volumes:
- ./data/postgresql:/var/lib/postgresql/data/pgdata
environment:
POSTGRES_PASSWORD: SgQ6Vhc3u015xOL09se9
PGDATA: /var/lib/postgresql/data/pgdata
redis:
image: redis:3.2.8-alpine
image: redis:3.2.9-alpine
restart: always
volumes:
- ./config/redis/redis.conf:/usr/local/etc/redis/redis.conf
- ./data/redis:/var/opt/redis
- ./data/redis:/data
command: redis-server /usr/local/etc/redis/redis.conf
30 changes: 30 additions & 0 deletions deploy/staging/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
version: '2'
services:
api:
image: my-api:staging
command: npm start
ports:
- 80:8080
restart: always
volumes:
- ./config/app:/usr/src/app/config/app
- ./logs/app:/usr/src/app/logs/app
- ./logs:/usr/src/app/logs
depends_on:
- db
- redis
db:
image: postgres:9.6.3-alpine
restart: always
volumes:
- ./data/postgresql:/var/lib/postgresql/data/pgdata
environment:
POSTGRES_PASSWORD: SgQ6Vhc3u015xOL09se9
PGDATA: /var/lib/postgresql/data/pgdata
redis:
image: redis:3.2.9-alpine
restart: always
volumes:
- ./config/redis/redis.conf:/usr/local/etc/redis/redis.conf
- ./data/redis:/data
command: redis-server /usr/local/etc/redis/redis.conf
8 changes: 0 additions & 8 deletions docker-compose.dev.yml

This file was deleted.

11 changes: 6 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
version: '2'
services:
api_dev:
api:
build:
context: .
dockerfile: Dockerfile-dev
dockerfile: deploy/development/Dockerfile
image: my-api-dev
command: pm2-dev start process.dev.json
ports:
- 8080:8080
Expand All @@ -13,7 +14,7 @@ services:
- db
- redis
db:
image: postgres:9.6.2-alpine
image: postgres:9.6.3-alpine
volumes:
- ./data/postgresql:/var/lib/postgresql/data/pgdata
ports:
Expand All @@ -22,10 +23,10 @@ services:
POSTGRES_PASSWORD: SgQ6Vhc3u015xOL09se9
PGDATA: /var/lib/postgresql/data/pgdata
redis:
image: redis:3.2.8-alpine
image: redis:3.2.9-alpine
volumes:
- ./config/redis/redis.conf:/usr/local/etc/redis/redis.conf
- ./data/redis:/var/opt/redis
- ./data/redis:/data
ports:
- 6379:6379
command: redis-server /usr/local/etc/redis/redis.conf
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
{
"name": "api-server-restify",
"version": "0.2.1",
"version": "0.3.0",
"description": "A Node.js RESTful API server with restify",
"main": "lib/server.js",
"scripts": {
"docker:build-dev": "docker-compose -f deploy/development/docker-compose.yml run --rm api npm install",
"docker:dev": "docker-compose up -d",
"docker:dev-stop": "docker-compose down",
"docker:build": "npm run build && docker build -f build/Dockerfile -t my-api:latest .",
"docker:build-staging": "npm run build && docker build -f build/Dockerfile -t my-api:staging .",
"start": "cross-env NODE_ENV=production node lib/server",
"start:service": "cross-env NODE_ENV=production pm2 start process.prod.json",
"stop:service": "cross-env NODE_ENV=production pm2 stop process.prod.json",
Expand Down

0 comments on commit 5efcefb

Please sign in to comment.