Skip to content

Commit

Permalink
feat: docker support (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
thonatos authored and atian25 committed Mar 8, 2018
1 parent e3304c4 commit ce9247a
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 59 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
.git/
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
coverage
app/public/
app/public/
17 changes: 17 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM node:8.9.4-alpine

RUN mkdir -p /usr/src/app

WORKDIR /usr/src/app

COPY package.json /usr/src/app/

# RUN npm i --production

RUN npm i --production --registry=https://registry.npm.taobao.org

COPY . /usr/src/app

EXPOSE 7001

CMD npm run docker
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ $ npm run dev
$ open http://localhost:7001/
```

#### Develop with docker
Setup redis / mongodb, requirements:

- docker
- docker-compose

```bash
# start
docker-compose -f docker-compose.dev.yml up

# stop
docker-compose -f docker-compose.dev.yml down

# remove volume/cache
docker-compose -f docker-compose.dev.yml down -v
```

### Deploy

```bash
Expand Down
31 changes: 31 additions & 0 deletions config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,36 @@ module.exports = appInfo => {
ADMIN_USER: true,
};

// database
config.redis = {
client: {
host: process.env.EGG_REDIS_HOST || '127.0.0.1',
port: process.env.EGG_REDIS_PORT || 6379,
password: process.env.EGG_REDIS_PASSWORD || '',
db: process.env.EGG_REDIS_DB || '0',
},
};

/**
* @see http://mongodb.github.io/node-mongodb-native/2.2/api/Db.html#createCollection
*/
config.mongoose = {
url: process.env.EGG_MONGODB_URL || 'mongodb://127.0.0.1:27017/egg_cnode',
options: {
server: { poolSize: 20 },
},
};

// passport
config.passportGithub = {
key: process.env.EGG_PASSPORT_GITHUB_CLIENT_ID || 'test',
secret: process.env.EGG_PASSPORT_GITHUB_CLIENT_SECRET || 'test',
};

config.passportLocal = {
usernameField: 'name',
passwordField: 'pass',
};

return config;
};
30 changes: 0 additions & 30 deletions config/config.local.js

This file was deleted.

27 changes: 0 additions & 27 deletions config/config.unittest.js

This file was deleted.

34 changes: 34 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
version: '3'
services:
redis:
image: redis:3.2-alpine
command: redis-server --appendonly yes --requirepass egg_cnode
volumes:
- egg-redis:/data
networks:
- docker_cnode
ports:
- 6379:6379

mongodb:
image: mongo:3.2
restart: always
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=mongodb
- MONGO_INITDB_DATABASE=egg_cnode
volumes:
- egg-mongo:/data/db
- ./init.d/mongo/:/docker-entrypoint-initdb.d/
networks:
- docker_cnode
ports:
- 27017:27017

volumes:
egg-mongo:
egg-redis:

networks:
docker_cnode:
driver: bridge
58 changes: 58 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
version: '3'
services:
cnode:
build:
context: .
dockerfile: Dockerfile
# args:
# - env=value
environment:
- NODE_ENV=production
- EGG_SERVER_ENV=prod
- EGG_REDIS_DB=0
- EGG_REDIS_HOST=redis
- EGG_REDIS_PORT=6379
- EGG_REDIS_PASSWORD=egg_cnode
- EGG_MONGODB_URL=mongodb://egg_cnode:egg_cnode@mongodb:27017/egg_cnode
- EGG_PASSPORT_GITHUB_CLIENT_ID=test
- EGG_PASSPORT_GITHUB_CLIENT_SECRET=test
depends_on:
- redis
- mongodb
networks:
- docker_cnode
ports:
- 7001:7001

redis:
image: redis:3.2-alpine
command: redis-server --appendonly yes --requirepass egg_cnode
volumes:
- egg-redis:/data
networks:
- docker_cnode
ports:
- 6379:6379

mongodb:
image: mongo:3.2
restart: always
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=mongodb
- MONGO_INITDB_DATABASE=egg_cnode
volumes:
- egg-mongo:/data/db
- ./init.d/mongo:/docker-entrypoint-initdb.d
networks:
- docker_cnode
ports:
- 27017:27017

volumes:
egg-mongo:
egg-redis:

networks:
docker_cnode:
driver: bridge
20 changes: 20 additions & 0 deletions init.d/mongo/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* eslint-disable */

/**
* 1. create custom user
* 2. create collection (Before MongoDB can save your new database, a collection name must also be specified at the time of creation.)
*/
db.createUser({
user: 'egg_cnode',
pwd: 'egg_cnode',
roles: [
{
role: 'readWrite',
db: 'egg_cnode'
}
]
})

db.egg_cnode.insert({
egg_cnode: 'egg-cnode'
})
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"scripts": {
"start": "egg-scripts start --daemon --title=egg-server-cnode",
"stop": "egg-scripts stop --title=egg-server-cnode",
"dev": "egg-bin dev",
"docker": "egg-scripts start --title=egg-server-cnode",
"dev": "egg-bin dev",
"debug": "egg-bin debug",
"test": "npm run lint -- --fix && npm run test-local",
"test-local": "egg-bin test",
Expand Down

0 comments on commit ce9247a

Please sign in to comment.