Skip to content

chore(build): provide docker support #1522

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

Merged
merged 2 commits into from
Oct 24, 2016
Merged

chore(build): provide docker support #1522

merged 2 commits into from
Oct 24, 2016

Conversation

TheDonDope
Copy link
Contributor

@TheDonDope TheDonDope commented Oct 23, 2016

Provide docker deployment support for separate environments

As requested in #1422 this commit provides a basic setup for docker deployment.

Prerequisites:

  • Have docker-engine and docker-compose installed
  • Have a fresh clone of the project
  • Right after cloning (do not run $ npm install, it is not neccessary), cd into the folder and run one of the below listed deployments.

How to run:

For development deployment ($ npm start in a docker container) run the following (it will use the docker-compose.yml):

$ docker-compose build
$ docker-compose up -d

Open browser at http://localhost:5555/

For production deployment ($ npm run serve.prod in a docker container) run the following (explicitly using the docker-compose.production.yml):

$ docker-compose -f docker-compose.production.yml build
$ docker-compose -f docker-compose.production.yml up -d

Open browser at http://localhost:5555/

//cc @sclausen @mgechev @ludohenin

P.S.: It would be nice to maybe even provide a production deployment which builds the application via $ npm run build.prod and then serving the output in an nginx container.
I am working on something like that over here: TheDonDope/crudular#1
Maybe @vyakymenko has some experience on the nginx deployment part?

Provide docker deployment support for separate environments
Fixes a minor formatting issue in the issue template for github
@Shyam-Chen
Copy link
Contributor

Shyam-Chen commented Oct 24, 2016

Only one Dockerfile is needed

use docker-compose exec app npm run <NPM_LIFECYCLE_HOOK>

Example:

  • docker-compose exec app npm run build.prod.exp
  • docker-compose exec app npm run serve.prod

But this Dockerfile does not run tests yet

@sclausen
Copy link
Contributor

sclausen commented Oct 24, 2016

@TheDonDope @mgechev When I started to work on a docker solution for our dev-system, one requirement was to comfortably link a nginx to the app, so the developers can route multiple microservices in our environment to it without manually installing an nginx on their dev system.
It would be so nice, if you consider adding such a feature, too.
As said in #1422, I already have such a setup, but it differs from this one, which is more elegant, I think, but also I still have this annoying error regarding .tmp/Rx.min.js, if I don't remove the changes from #1390.

@TheDonDope
Copy link
Contributor Author

TheDonDope commented Oct 24, 2016

@Shyam-Chen: Yes, the bare minimum is a single Dockerfile. However the implementation in the PR provides the possibility do have specific setups per environment. For example, while the normal $ docker-compose build && docker-compose up -d executes a $ npm start, the $ docker-compose -f docker-compose.production.yml [...] executes a $ npm run serve.prod.

@TheDonDope
Copy link
Contributor Author

@sclausen
Tying in nginx is definitely a point i want to get into.
To add onto the current implementation here, my idea is, to have a second docker container in the docker-compose.production.yml, which hosts the nginx server.

The basic idea is (for production deployment):

  1. Start first container and execute $ npm run build.prod
  2. Make sure that the compiled /dist/prod is mounted as a shared named volume, so that it is accessible to other containers
  3. Start the second container and boot up nginx, while linking /dist/prod from the shared named volume via volumes_from.

Yet, there is still a bit of manual work to do here:
Since the $ npm run build.prod task takes some time to complete, a simple depends_on in the nginx container is not enough to handle startup order. This only guarantees, that the first container is started first, but it does not wait until completion before starting the second (the nginx) container.
So, what you would need to do is:

  1. Start build.prod: $ docker-compose -f docker-compose.production.yml build && docker-compose -f docker-compose.production.yml up -d dist-build
  2. Wait for the dist build to finish and having persisted/copied/linked/whatever the /dist/prod files
  3. Boot up the nginx container: $ docker-compose -f docker-compose.production.yml up -d nginx

@TheDonDope TheDonDope merged commit 7f0aadc into master Oct 24, 2016
@TheDonDope TheDonDope deleted the docker branch October 24, 2016 11:11
@TheDonDope
Copy link
Contributor Author

I'll merge for now, as he current PR already provides the basic functionality. We can track the NGINX additions in another issue / PR.

@sclausen
Copy link
Contributor

sclausen commented Oct 24, 2016

@TheDonDope here is our docker config

FROM          ubuntu:15.10
MAINTAINER    name <email>

ENV           DEBIAN_FRONTEND=noninteractive

COPY          nginx_signing.key .
RUN           echo "deb http://nginx.org/packages/mainline/ubuntu/ wily nginx" >> /etc/apt/sources.list \
              && apt-key add nginx_signing.key \
              && apt-get update -y \
              && apt-get install -y nginx

COPY          angular2-seed.conf /etc/nginx/conf.d/angular2-seed.conf
COPY          angular2-seed_server.crt /etc/nginx/
COPY          angular2-seed_server.pem /etc/nginx/

CMD           service nginx start && sleep infinity

and my docker-compose.yml

nginx:
  build: nginx
  ports:
    - 127.0.0.1:443:443
  extra_hosts:
    - microservice1:192.168.0.1
    - microservice2:192.168.0.2
  volumes_from:
    - angular2_seed

angular2_seed:
  build: .
  command:
    "$NPM_TASK"
  volumes:
    - ./src:/var/www/src:ro
    - ./node_modules:/var/www/node_modules
    - ./test:/var/www/test

@TheDonDope
Copy link
Contributor Author

@sclausen Thank you very much!

Can you share the contents of your angular2-seed.conf (Thats my other issue: actually deploying the /dist/prod to nginx with support for PathLocationStrategy, respectively HTML 5 Push State. Thanks in advance!

@sclausen
Copy link
Contributor

@TheDonDope sure, I will post it tomorrow.

mgechev added a commit that referenced this pull request Oct 24, 2016
* 'master' of https://github.com/mgechev/angular-seed:
  chore(build): provide docker support (#1522)
  use Config.APP_BASE so that it's configurable
  - write out absolute urls for the sourcemaps because of the way angular2   handles style injection
@sclausen
Copy link
Contributor

@TheDonDope here is our nginx conf

map $http_upgrade $connection_upgrade {
  default upgrade;
  ''      close;
}

server {

  listen              443 ssl http2;

  server_name         angular2-seed.local;
  ssl                 on;
  ssl_certificate     angular2-seed_server.crt;
  ssl_certificate_key angular2-seed_server.pem;

  ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;
  ssl_session_cache shared:SSL:10m;
  ssl_session_tickets off; # Requires nginx >= 1.5.9
  add_header Strict-Transport-Security "max-age=63072000; preload";
  add_header X-Frame-Options DENY;

  location /api/microservice1 {
    rewrite ^/api/microservice1/(.*)$ /$1 break;
    proxy_pass https://microservice1/;
    proxy_http_version 1.1;
    proxy_set_header X-Forwarded-For $remote_addr;
  }

  location / {
    root /var/www/dist/dev;
    try_files $uri /index.html;
    index index.html;
    gzip on;
    gzip_types text/css text/javascript application/x-javascript application/json;
  }

  location /node_modules {
    root /var/www;
    gzip on;
    gzip_types text/css text/javascript application/x-javascript application/json;
  }
}

@TheDonDope
Copy link
Contributor Author

@sclausen Thank you so much for sharing!!!! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants