Tuesday, December 4, 2018

docker nodejs IDE visual studio 2018


educacion@educacion:~/src/docker02$ cat Dockerfile 
FROM node
WORKDIR /code
RUN npm install -g nodemon
COPY package.json /code/package.json
RUN npm install && npm ls
RUN mv /code/node_modules /node_modules
COPY . /code
CMD ["npm", "start"]


educacion@educacion:~/src/docker02$ cat  docker-compose.yml 
version: "2"
services:
  web:
    build: .
    command: nodemon -L --inspect=0.0.0.0:5858
    volumes:
      - .:/code
    ports:
      - "8000:8000"
      - "5858:5858"


educacion@educacion:~/src/docker02$ cat package.json 
{
    "main": "app.js",
    "dependencies": {
        "express": "~4.14.0",
        "express-handlebars": "~3.0.0"
    }
}


educacion@educacion:~/src/docker02$ cat app.js
const express = require('express');
const expressHandlebars = require('express-handlebars');
const http = require('http');
const PORT = 8000;
const LINES = [
    "One thing at the time.",
    "Start from the beginning!",
    "Up to date!",
    "As Simple As Possible",
];
let lineIndex = 0;
const app = express();
app.engine('html', expressHandlebars());
app.set('view engine', 'html');
app.set('views', __dirname);
app.get('/', (req, res) => {
    let message = LINES[lineIndex];
    lineIndex += 1;
    if (lineIndex >= LINES.length) {
        lineIndex = 0;
    }
    res.render('index', {message: message});
});
http.Server(app).listen(PORT, () => {
    console.log("HTTP server listening on port %s", PORT);
});



educacion@educacion:~/src/docker02$ cat index.html
< !doctype html>
< html>
    < head>
        < meta http-equiv="refresh" content="2">
        < style type="text/css">
            .sample_docker_nodejs {
                font-family: Helvetica, Arial, sans-serif;
                font-weight: 600;
                font-size: 56pt;
                text-transform: uppercase;
                text-align: center;
                background: #3c3;
                color: white;
            }
        < /style>
    < /head>
    < body class='sample_docker_nodejs'>“{{message}}”
< /html>


root@educacion:/home/educacion# apt install libgconf-2-4 desktop-file-utils 
mime-support gconf2-common gconf-service


educacion@educacion:~/src/docker02$ docker-compose up
Creating network "docker02_default" with the default driver
Building web
Step 1/8 : FROM node
 ---> e6825fa3bd20
Step 2/8 : WORKDIR /code
 ---> Running in 7f475c6a9926
Removing intermediate container 7f475c6a9926
 ---> 325c768f008c
Step 3/8 : RUN npm install -g nodemon
 ---> Running in 37c3e446896d
/usr/local/bin/nodemon -> /usr/local/lib/node_modules/nodemon/bin/nodemon.js

> nodemon@1.18.7 postinstall /usr/local/lib/node_modules/nodemon
> node bin/postinstall || exit 0

Love nodemon? You can now support the project via the open collective:
 > https://opencollective.com/nodemon/donate

npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.4 
(node_modules/nodemon/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform 
for fsevents@1.2.4: wanted {"os":"darwin","arch":"any"} 
(current: {"os":"linux","arch":"x64"})

+ nodemon@1.18.7
added 223 packages from 130 contributors in 291.478s
Removing intermediate container 37c3e446896d
 ---> a7a86d38c2a1
Step 4/8 : COPY package.json /code/package.json
 ---> 80630e53397b
Step 5/8 : RUN npm install && npm ls
 ---> Running in c7347533b05c
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN code No description
npm WARN code No repository field.
npm WARN code No license field.

added 70 packages from 80 contributors in 474.487s
/code
+-- express@4.14.1
| +-- accepts@1.3.5
| | +-- mime-types@2.1.21
..........................

Removing intermediate container c7347533b05c
 ---> 5d15000e4998
Step 6/8 : RUN mv /code/node_modules /node_modules
 ---> Running in e7368c6b5409
Removing intermediate container e7368c6b5409
 ---> c80d1a0c6d8a
Step 7/8 : COPY . /code
 ---> 69f216de5856
Step 8/8 : CMD ["npm", "start"]
 ---> Running in 220eb2ed498c
Removing intermediate container 220eb2ed498c
 ---> 9ce32b100e72
Successfully built 9ce32b100e72
Successfully tagged docker02_web:latest
WARNING: Image for service web was built because it did not already exist. To rebuild this image 
you must use `docker-compose build` or `docker-compose up --build`.
Creating docker02_web_1
Attaching to docker02_web_1
web_1  | [nodemon] 1.18.7
web_1  | [nodemon] to restart at any time, enter `rs`
web_1  | [nodemon] watching: *.*
web_1  | [nodemon] starting `node --inspect=0.0.0.0:5858 app.js`
web_1  | Debugger listening on ws://0.0.0.0:5858/08100114-cd8e-4e44-be6f-855309ffa8b6
web_1  | For help, see: https://nodejs.org/en/docs/inspector
web_1  | HTTP server listening on port 8000


http://localhost:8000/

 * Edit with VSCode in realtime!


 * Connect with a container in runtime!

educacion@educacion:~/src/docker02$ docker exec -i -t docker02_web_1 /bin/bash

Reference:
https://github.com/elPoeta/docker-nodejs-postgres-example
https://github.com/docker/labs/blob/master/developer-tools/README.md
https://code.visualstudio.com/
https://github.com/docker/labs/blob/master/developer-tools/nodejs-debugging/VSCode-README.md


No comments: