martes, 1 de octubre de 2019

node.js Docker simpleRest

node.js Docker simpleRest


app.js

const express = require('express');
const app = express();

app.get('/', (reqres=> res.send('response de simpreRest powered by node.js [Bext]\n'));

app.listen(3000, () => { 
    console.log('LOG: aplicacion node.js corriendo en puerto 3000. ok.')
})

creamos el package.json

ext@bext-G3-3779:~/nodeWorkspace/simpleRest$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (simplerest) rest-api-docker
version: (1.0.0)
description:
entry point: (app.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to /home/bext/nodeWorkspace/simpleRest/package.json:

{
  "name": "rest-api-docker",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}


Is this OK? (yes)

Instalamos el express

bext@bext-G3-3779:~/nodeWorkspace/simpleRest$ npm install --save express
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN rest-api-docker@1.0.0 No description
npm WARN rest-api-docker@1.0.0 No repository field.

+ express@4.17.1
added 50 packages from 37 contributors and audited 126 packages in 9.765s
found 0 vulnerabilities

al package.json le indicamos el start
bext@bext-G3-3779:~/nodeWorkspace/simpleRest$ cat package.json
{
  "name": "rest-api-docker",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "start": "node app.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1"
  }
}

ejecutamos el código

bext@bext-G3-3779:~/nodeWorkspace/simpleRest$ npm run start

> rest-api-docker@1.0.0 start /home/bext/nodeWorkspace/simpleRest
> node app.js

LOG: aplicacion node.js corriendo en puerto 3000. ok.


lo probamos en otra terminal o browser

bext@bext-G3-3779:~$ curl http://localhost:3000
response de simpreRest powered by node.js [Bext]

Para ejecutarlo en docker creamos archivo Dockerfile
FROM node:9-slim
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
CMD ["npm","start"]

creamos la imagen docker

bext@bext-G3-3779:~/nodeWorkspace/simpleRest$ docker build -t node-docker-rest .
Sending build context to Docker daemon  1.998MB
Step 1/6 : FROM node:9-slim
9-slim: Pulling from library/node
5bba3ecb4cd6: Pull complete 
196b8e3c919d: Pull complete 
7d083412657b: Pull complete 
a88538b9cd05: Pull complete 
e01bd28a434b: Pull complete 
Digest: sha256:288b29c1925d65b2d7d8701f8ada201e7dcd066438c0fb4299c35dff129b893f
Status: Downloaded newer image for node:9-slim
 ---> e20bb4abe4ee
Step 2/6 : WORKDIR /app
 ---> Running in a14c1163c0bd
Removing intermediate container a14c1163c0bd
 ---> 7599e3d9b053
Step 3/6 : COPY package.json /app
 ---> 4651f4ade264
Step 4/6 : RUN npm install
 ---> Running in f7e61f4fe30f
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN rest-api-docker@1.0.0 No description
npm WARN rest-api-docker@1.0.0 No repository field.

added 50 packages in 2.416s
Removing intermediate container f7e61f4fe30f
 ---> 2974f3cfcb4e
Step 5/6 : COPY . /app
 ---> 8ee60917ff26
Step 6/6 : CMD ["nmp","start"]
 ---> Running in 55ab8eabfcd2
Removing intermediate container 55ab8eabfcd2
 ---> d5de06ec0f1f
Successfully built d5de06ec0f1f
Successfully tagged node-docker-rest:latest
bext@bext-G3-3779:~/nodeWorkspace/simpleRest$ 

Corremos el contenedor pero lo exponemos al puerto 9000, pues nomas.

bext@bext-G3-3779:~/nodeWorkspace/simpleRest$ docker run -it -p 9000:3000 node-docker-rest

> rest-api-docker@1.0.0 start /app
> node app.js

LOG: aplicacion node.js corriendo en puerto 3000. ok. 
 
probamos el contenedor docker ejecutandose

bext@bext-G3-3779:~$ curl http://localhost:3000
curl: (7) Failed to connect to localhost port 3000: Connection refused
bext@bext-G3-3779:~$ curl http://localhost:9000
response de simpreRest powered by node.js [Bext]
bext@bext-G3-3779:~$ 

Lo podemos ejecutar en background

bext@bext-G3-3779:~/nodeWorkspace/simpleRest$ docker run -d -p 9000:3000 node-docker-rest
152a49bdcaef64a2d4205cd3d3c883a40ba66d93c8735db5ac1ff681922a5eb9 
bext@bext-G3-3779:~/nodeWorkspace/simpleRest$ 
 
bext@bext-G3-3779:~/nodeWorkspace/simpleRest$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
b2cac9c69ebc        node-docker-rest    "npm start"         7 seconds ago       Up 2 seconds        0.0.0.0:9000->3000/tcp   youthful_ramanujan
bext@bext-G3-3779:~/nodeWorkspace/simpleRest$ 
 
 
 
instalamos nodemon y actualizamos el package.json en start
{
  "name""rest-api-docker",
  "version""1.0.0",
  "description""",
  "main""app.js",
  "scripts": {
    "start""nodemon app.js",
    "test""echo \"Error: no test specified\" && exit 1"
  },
  "author""",
  "license""ISC",
  "dependencies": {
    "express""^4.17.1"
  }
}

bext@bext-G3-3779:~/nodeWorkspace/simpleRest$ npm install -g nodemon
/home/bext/.nvm/versions/node/v10.16.3/bin/nodemon -> /home/bext/.nvm/versions/node/v10.16.3/lib/node_modules/nodemon/bin/nodemon.js

> nodemon@1.19.3 postinstall /home/bext/.nvm/versions/node/v10.16.3/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.9 (node_modules/nodemon/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

+ nodemon@1.19.3
added 221 packages from 128 contributors in 20.673s
bext@bext-G3-3779:~/nodeWorkspace/simpleRest$ nodemon
[nodemon] 1.19.3
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node app.js`
LOG: aplicacion node.js corriendo en puerto 3000. ok.
^Cbext@bext-G3-3779:~/nodeWorkspace/simpleRest$ 

bext@bext-G3-3779:~/nodeWorkspace/simpleRest$ npm run start

> rest-api-docker@1.0.0 start /home/bext/nodeWorkspace/simpleRest
> nodemon app.js

[nodemon] 1.19.3
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node app.js`
LOG: aplicacion node.js corriendo en puerto 3000. ok.

Con nodemon cada cambio en código fuente hace que se actualice el runtime en tiempo real.

[nodemon] 1.19.3
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node app.js`
LOG: aplicacion node.js corriendo en puerto 3000. ok.
[nodemon] restarting due to changes...
[nodemon] starting `node app.js`
LOG: aplicacion node.js corriendo en puerto 3000. ok.
[nodemon] restarting due to changes...
[nodemon] starting `node app.js`
LOG: aplicacion node.js corriendo en puerto 3000. ok. 
 
bext@bext-G3-3779:~$ curl http://localhost:3000
response de simpreRest powered by node.js cambiando para nodemon test2 [Bext]
bext@bext-G3-3779:~$

Ahora instalamos nodemon para nuestro docker image

bext@bext-G3-3779:~/nodeWorkspace/simpleRest$ npm install --save nodemon

> nodemon@1.19.3 postinstall /home/bext/nodeWorkspace/simpleRest/node_modules/nodemon
> node bin/postinstall || exit 0

npm WARN rest-api-docker@1.0.0 No description
npm WARN rest-api-docker@1.0.0 No repository field.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

+ nodemon@1.19.3
added 215 packages from 128 contributors and audited 2395 packages in 10.033s
found 0 vulnerabilities

bext@bext-G3-3779:~/nodeWorkspace/simpleRest$ 

Creamos nuevamente la imagen ahora con nodemon ya integrada

bext@bext-G3-3779:~/nodeWorkspace/simpleRest$ docker build -t node-docker-rest .Sending build context to Docker daemon  6.879MB
Step 1/6 : FROM node:9-slim
 ---> e20bb4abe4ee
Step 2/6 : WORKDIR /app
 ---> Using cache
 ---> 7599e3d9b053
Step 3/6 : COPY package.json /app
 ---> 014a34fcebec
Step 4/6 : RUN npm install
 ---> Running in 0655d10a5744

> nodemon@1.19.3 postinstall /app/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 notice created a lockfile as package-lock.json. You should commit this file.
npm WARN rest-api-docker@1.0.0 No description
npm WARN rest-api-docker@1.0.0 No repository field.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

added 265 packages in 6.955s
Removing intermediate container 0655d10a5744
 ---> 7a58b1e89687
Step 5/6 : COPY . /app
 ---> 67e9107b70cc
Step 6/6 : CMD ["npm","start"]
 ---> Running in 04156c88639a
Removing intermediate container 04156c88639a
 ---> 5d24eff02a6f
Successfully built 5d24eff02a6f
Successfully tagged node-docker-rest:latest
bext@bext-G3-3779:~/nodeWorkspace/simpleRest
 
ejecutamos el contenedor con indicación de volumen en directorio de usuario y app
bext@bext-G3-3779:~/nodeWorkspace/simpleRest$ docker run -it -p 9000:3000 -v $(pwd):/app node-docker-rest

> rest-api-docker@1.0.0 start /app
> nodemon app.js

[nodemon] 1.19.3
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node app.js`
LOG: aplicacion node.js corriendo en puerto 3000. ok. 
 
Con esto, cualqueir cambio en el código de node es detectado por nodemon y hace que se el cambio se tome en tiempo real. hacemos un cambio en el mensaje del rest y vemos que se reinicia debido a los cambios, y el cambio lo observamos en el curl.

[nodemon] 1.19.3
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node app.js`
LOG: aplicacion node.js corriendo en puerto 3000. ok.
[nodemon] restarting due to changes...
[nodemon] starting `node app.js`
LOG: aplicacion node.js corriendo en puerto 3000. ok.

bext@bext-G3-3779:~$ curl http://localhost:9000
response de simpreRest powered by node.js cambiando para nodemon test2 [Bext]
bext@bext-G3-3779:~$ curl http://localhost:9000
response de simpreRest node.js cambiando para nodemon combio Docker [Bext]
bext@bext-G3-3779:~$ 

Detenemos el contenedor y lo ejecutamos en modo background para ver si cambiando el fuente en app.js el mensaje en este caso se actualiza el contenedor, y sorpresa , se actualiza OK.

bext@bext-G3-3779:~/nodeWorkspace/simpleRest$ docker run -d -p 9000:3000 -v $(pwd):/app node-docker-rest
a5d0846bf794a9af071fc874a3a61582afb8598b7f2c07de2012c2d5a1de4d94
bext@bext-G3-3779:~/nodeWorkspace/simpleRest$ 

Consultamos el servcio rest antes de modificar el código.

bext@bext-G3-3779:~$ curl http://localhost:9000
response de simpreRest node.js cambiando para nodemon combio Docker [Bext] 

modificamos el código


app.get('/', (reqres=> res.send('response de simpreRest node.js Cambio Docker background [Bext]\n'));

consultamos nuevamente y observamos el cambio.

bext@bext-G3-3779:~$ curl http://localhost:9000
response de simpreRest node.js Cambio Docker background [Bext]
bext@bext-G3-3779:~$ 

eot

No hay comentarios:

Publicar un comentario