From 0898c7ce10d9e3e1af26ff34ef606431c9946e8b Mon Sep 17 00:00:00 2001 From: Francois Romain <francoisromain@gmail.com> Date: Fri, 25 May 2018 13:35:18 +0200 Subject: [PATCH] chore: configuration docker --- .dockerignore | 3 +-- .env-example | 1 + Dockerfile | 1 + README.md | 40 ++++++++++++++++++++++++++++++++---- conf/index.js | 9 +++----- conf/knex.js | 35 ++++++++++++++++++++----------- docker-compose.localhost.yml | 5 +++++ index.js | 6 +++--- postgres/index.js | 2 +- 9 files changed, 74 insertions(+), 28 deletions(-) diff --git a/.dockerignore b/.dockerignore index 5171c5408..b512c09d4 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,2 +1 @@ -node_modules -npm-debug.log \ No newline at end of file +node_modules \ No newline at end of file diff --git a/.env-example b/.env-example index 8a22a9fc5..8b2e78308 100644 --- a/.env-example +++ b/.env-example @@ -1,6 +1,7 @@ # Set your variables and rename to .env NODE_ENV=development +NODE_PORT=4000 PGHOST=localhost PGPORT=5432 diff --git a/Dockerfile b/Dockerfile index 069f686d6..cb37be980 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,6 +3,7 @@ LABEL maintainer=francois.romain@beta.gouv.fr ENV dir /api ENV NODE_ENV production +ENV NODE_PORT 4000 WORKDIR $dir # cache node_modules if no changes to package.json diff --git a/README.md b/README.md index a46b40758..2ef7d5ff4 100644 --- a/README.md +++ b/README.md @@ -9,24 +9,56 @@ Plus d'infos sur le projet [ici](http://camino.beta.gouv.fr/). ## Npm scripts ```bash -# Install dependencies. +# Installe les dépendances npm install -# Start server with nodemon +# Démarre le serveur avec nodemon npm run dev -# Start server with node +# Démarre le serveur avec node npm start ``` --- -## docker compose +## Docker + +### Pour du dévelopement local + +```bash +# Démarre l'application et la base de donnée dans des conteneurs Docker +# en mode `development` +# écoute sur http://localhost:4000 +docker-compose -f ./docker-compose.local.yml up --build +``` + +### Pour du tester l'application en local dans un environement de production + +requiert: + +* une installation locale et active de https://github.com/jwilder/nginx-proxy +* un certificat ssl auto-signé + +[instructions](https://medium.com/@francoisromain/set-a-local-web-development-environment-with-custom-urls-and-https-3fbe91d2eaf0) ```bash +# Démarre l'application dans un container Docker +# en mode `production` +# écoute sur https://api.camino.local docker-compose -f ./docker-compose.local.yml up --build ``` +### Version de production + +[instructions](https://medium.com/@francoisromain/host-multiple-websites-with-https-inside-docker-containers-on-a-single-server-18467484ab95) + +```bash +# Démarre l'application dans un container Docker +# en mode `production` +# écoute sur http://api.camino.pw +docker-compose up -d --build +``` + --- ## postgres diff --git a/conf/index.js b/conf/index.js index 010222c22..0b3d16bf3 100644 --- a/conf/index.js +++ b/conf/index.js @@ -4,15 +4,12 @@ const virtualHost = process.env.VIRTUAL_HOST const protocol = virtualHost ? 'https' : 'http' -const dir = '' -const port = 4000 -const host = virtualHost ? '0.0.0.0' : 'localhost' +const port = Number(process.env.NODE_PORT) const url = virtualHost - ? `${protocol}://${virtualHost}/${dir}` - : `${protocol}://${host}:${port}/${dir}` + ? `${protocol}://${virtualHost}/` + : `${protocol}://localhost:${port}/` module.exports = { port, - host, url } diff --git a/conf/knex.js b/conf/knex.js index 2019fe62b..d0ede036f 100644 --- a/conf/knex.js +++ b/conf/knex.js @@ -1,18 +1,29 @@ require('dotenv').config({ path: '../.env' }) module.exports = { - client: 'pg', - connection: { - host: process.env.PGHOST, - port: process.env.PGPORT, - database: process.env.PGDATABASE, - user: process.env.PGUSER + development: { + client: 'pg', + connection: { + host: process.env.PGHOST, + port: process.env.PGPORT, + database: process.env.PGDATABASE, + user: process.env.PGUSER + }, + debug: true, + migrations: { + directory: '../_tools/knex/migrations' + }, + seeds: { + directory: '../_tools/knex/seeds' + } }, - debug: true, - migrations: { - directory: '../_tools/knex/migrations' - }, - seeds: { - directory: '../_tools/knex/seeds' + production: { + client: 'pg', + connection: { + host: process.env.PGHOST, + port: process.env.PGPORT, + database: process.env.PGDATABASE, + user: process.env.PGUSER + } } } diff --git a/docker-compose.localhost.yml b/docker-compose.localhost.yml index cc04b3160..b98593442 100644 --- a/docker-compose.localhost.yml +++ b/docker-compose.localhost.yml @@ -11,6 +11,7 @@ services: - postgres environment: NODE_ENV: development + NODE_PORT: 4000 PGHOST: postgres PGPORT: 5432 PGDATABASE: camino @@ -21,6 +22,10 @@ services: postgres: image: postgres:alpine + environment: + PGUSER: postgres + expose: + - 5432 volumes: - data:/var/lib/postgresql/data diff --git a/index.js b/index.js index f526e8ee0..9d636b4ad 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,7 @@ require('./postgres') const chalk = require('chalk') const express = require('express') var graphqlHTTP = require('express-graphql') -const { port, host, url } = require('./conf') +const { port, url } = require('./conf') const schema = require('./graphql/schemas') const resolvers = require('./graphql/resolvers') @@ -18,6 +18,6 @@ app.use( }) ) -app.listen(port, host, () => { - console.log(chalk.bgWhiteBright.black.bold('Server:' + url)) +app.listen(port, () => { + console.log(chalk.bgWhiteBright.black.bold('Server: ' + url)) }) diff --git a/postgres/index.js b/postgres/index.js index 53d452b71..7fbe385d8 100644 --- a/postgres/index.js +++ b/postgres/index.js @@ -1,5 +1,5 @@ const Knex = require('knex') -const knexConf = require('../conf/knex') +const knexConf = require('../conf/knex')[process.env.NODE_ENV] const { Model } = require('objection') const knex = Knex(knexConf) -- GitLab