Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Docker Compose-based development workflow #4

Merged
merged 9 commits into from
Nov 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
./.git/*
./.vscode/*
**/node_modules/*
./.git/**/*
./.vscode/**/*
**/node_modules/**/*
secrets/**/*.enc
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:alpine
FROM node:9-alpine

RUN apk update -q && apk add git -q

Expand Down
16 changes: 16 additions & 0 deletions Dockerfile-dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM node:9-alpine

RUN apk update -q && apk add git -q

ENV NODE_ENV=development
ENV PORT=8080

EXPOSE 8080
EXPOSE 9229

VOLUME /code
VOLUME /code/node_modules

WORKDIR /code

CMD yarn && yarn dev
3 changes: 2 additions & 1 deletion commonconfig.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module.exports = {
ignoredPatterns: [
'*/**/typings/*',
'Dockerfile',
'Dockerfile*',
'docker-compose.yml',
'**/LICENSE',
'**/README.md',
],
Expand Down
43 changes: 43 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
version: "3.3"

services:
database:
image: mysql
restart: on-failure
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's on-failure?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tells Docker Compose to restart MySQL server when the server command exits with a non-zero exit code.

ports:
- 3306:3306
expose:
- 3306
environment:
- MYSQL_DATABASE=hollowverse
- MYSQL_ROOT_PASSWORD=123456
- MYSQL_ROOT_HOST=%

api:
restart: always
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's always?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

always means that the container will restart whenever the entry point command (yarn dev) exits, regardless of the exit code. yarn dev is not supposed to exit at all so I'm not sure about using always. on-failure could also work.

depends_on:
- database
build:
dockerfile: Dockerfile-dev
context: .
links:
- database
ports:
- 8080:8080
- 9229:9229
volumes:
- type: bind
source: .
target: /code

- type: volume
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference between type bind and type volume?

Copy link
Contributor Author

@forabi forabi Nov 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bind tells Docker to use an existing directory on the host OS as a volume inside the container. volume, on the other hand, creates an empty volume, managed by Docker, so that the data can be persisted even if the container is stopped. We need bind to mount the project code and allow automatic reloading on code changes. volume is used to cache node_modules separately from the host node_modules so that dependencies do not conflict (suppose that the host is Windows, if the container overwrites node_modules, some native dependencies will not work when running the server outside the container)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The volume created with type volume can be inspected like this:

❯ docker volume inspect api_node_modules
[
    {
        "CreatedAt": "2017-10-30T07:23:24+02:00",
        "Driver": "local",
        "Labels": {
            "com.docker.compose.project": "api",
            "com.docker.compose.volume": "node_modules"
        },
        "Mountpoint": "/var/lib/docker/volumes/api_node_modules/_data",
        "Name": "api_node_modules",
        "Options": {},
        "Scope": "local"
    }
]

source: nm
target: /code/node_modules
environment:
- RDS_HOSTNAME=database
- RDS_USERNAME=root
- RDS_PASSWORD=123456

volumes:
nm:

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"lint-js": "eslint '**/*.js{,x}'",
"lint-ts": "tslint './*.ts' 'src/**/*.ts{,x}' -e 'src/typings/schema.ts' --project tsconfig.json",
"generate-schema-types": "graphql-to-typescript schema.graphql src/typings/schema.ts",
"generate-schema-types/dev": "nodemon --watch src/schema.graphql --exec 'run-p generate-schema-types'",
"server/dev": "PORT=8080 nodemon --watch ./src --ext ts,graphql,tsx,json --exec 'ts-node src/server.ts --project ./src'",
"generate-schema-types/dev": "nodemon --watch schema.graphql --exec 'run-p generate-schema-types'",
"server/dev": "PORT=8080 nodemon --watch ./src --ext ts,graphql,tsx,json --exec 'node --inspect=0.0.0.0:9229 -r 'ts-node/register' src/server.ts'",
"dev": "run-p '*/dev'",
"database/mock": "ts-node src/database/mock.ts --project ./src",
"build": "tsc --project ./src",
Expand Down
2 changes: 1 addition & 1 deletion src/database/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { NotablePersonLabel } from './entities/label';
import { NotablePersonEventComment } from './entities/comment';
import { User } from './entities/user';
import { readJson } from '../helpers/readFile';
import { isUsingProductionDatabase } from './env';
import { isUsingProductionDatabase } from '../env';

const {
// These variables are for the development database
Expand Down
2 changes: 1 addition & 1 deletion src/database/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { NotablePersonEventComment } from './entities/comment';
import { NotablePersonLabel } from './entities/label';
import * as Chance from 'chance';
import { times, kebabCase } from 'lodash';
import { isUsingProductionDatabase } from './env';
import { isUsingProductionDatabase } from '../env';

if (isUsingProductionDatabase === false) {
const chance = new Chance(process.env.SEED || 1);
Expand Down
6 changes: 5 additions & 1 deletion src/database/env.ts → src/env.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
const {
NODE_ENV,

// To use the production database, this must be set explicitly to 'true'
// in EB environment settings. Otherwise, the testing database is used.
USE_PRODUCTION_DATABASE,
} = process.env;

export const isProd = NODE_ENV === 'production';

export const isUsingProductionDatabase =
process.env.NODE_ENV === 'production' && USE_PRODUCTION_DATABASE === 'true';
isProd && USE_PRODUCTION_DATABASE === 'true';