2020年9月9日星期三

Step by step Create a node application running in docker container

 1. docker pull node image of docker.

$ docker pull lqwangxg/node:alpine
alpine: Pulling from lqwangxg/node
cbdbe7a5bc2a: Pull complete 
091983d60f65: Pull complete 
7bacc183740f: Pull complete 
c4a5a5d270d4: Pull complete 
Digest: sha256:0b5c7eb38785da1ad4c105930faca0bc546dfcceb0464724456e277fd5e3f6e2
Status: Downloaded newer image for lqwangxg/node:alpine
docker.io/lqwangxg/node:alpine

2. run and go into the container 

$ docker run --rm -it lqwangxg/node:alpine
/ # 

3. make app folder and npm init 

$ mkdir app && cd app && mkdir docker-node && cd docker-node
/app/docker-node # 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 init` 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: (docker-node) 
version: (1.0.0) 
description: A sample of node server application running in docker container
entry point: (index.js) 
test command: 
git repository: https://github.com/lqwangxg/docker-node.git
keywords: docker-node 
author: lqwangxg
license: (ISC) Apache-2.0
About to write to /app/docker-node/package.json: ...

4. Append scripts.start in package.json

$ vi package.json 

"scripts": {
    "start": "node index.js"
  },
...

5. Install express.js to dependencies

$ npm install express --save
$ cat package.json 
  "dependencies": {
    "express": "^4.17.1"
  }
...

6. Create index.js

$ vi index.js 
'use strict';

const express = require('express');

// Constants
const PORT = 8080;
const HOST = '0.0.0.0';

// App
const app = express();
app.get('/', (req, res) => {
  res.send('Hello World');
});

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);

7. Create Dockerfile

$ vi index.js 
FROM lqwangxg/node:alpine

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json 
# AND package-lock.json are copied
# where available (npm@5+)
# Only copying the package.json make you taking advantage of cached 
# Docker layers.
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

EXPOSE 8080
CMD [ "node", "server.js" ]

8. Building Docker image

$ docker build -t lqwangxg/docker-node . 
Sending build context to Docker daemon  74.75kB
Step 1/7 : FROM lqwangxg/node:alpine
 ---> 18f4bc975732
Step 2/7 : WORKDIR /usr/src/app
 ---> Running in 62fc426d53f6
Removing intermediate container 62fc426d53f6
 ---> b5c5adb657f0
Step 3/7 : COPY package*.json ./
 ---> 3afb928aed17
Step 4/7 : RUN npm install
 ---> Running in 27b661afc53e
npm notice created a lockfile as package-lock.json. You should commit this file.
added 50 packages from 37 contributors and audited 50 packages in 3.353s
found 0 vulnerabilities

Removing intermediate container 27b661afc53e
 ---> c3d8f229822f
Step 5/7 : COPY . .
 ---> c8c9739a3c03
Step 6/7 : EXPOSE 8080
 ---> Running in 3f1e5962f0cf
Removing intermediate container 3f1e5962f0cf
 ---> 18ee0a952621
Step 7/7 : CMD [ "node", "index.js" ]
 ---> Running in beb8fbaeb248
Removing intermediate container beb8fbaeb248
 ---> 18ea6031109f
Successfully built 18ea6031109f
Successfully tagged lqwangxg/docker-node:latest

9. Check built docker image

$ docker images
$ docker images 
REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
lqwangxg/docker-node   latest              18ea6031109f        25 seconds ago      92.6MB
lqwangxg/node          alpine              18f4bc975732        6 weeks ago         89.3MB

10. Run the image 

$ docker run -p 3000:8080 -d lqwangxg/docker-node
$ docker ps 
CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS              PORTS                    NAMES
169067f0460b        lqwangxg/docker-node   "docker-entrypoint.s…"   5 seconds ago       Up 3 seconds        0.0.0.0:3000->8080/tcp   clever_fermi

11. Go inside the container

$ docker exec -it 1690 /bin/sh
/usr/src/app # ls
Dockerfile         index.js           node_modules       package-lock.json  package.json

12. Check web server status by curl 

$ curl -i localhost:3000

HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 11
ETag: W/"b-Ck1VqNd45QIvq3AZd8XYQLvEhtA"
Date: Wed, 09 Sep 2020 01:33:06 GMT
Connection: keep-alive

13. Push the image to docker hub

$ docker login   # login before push to docker hub, 
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: lqwangxg
Password: 
$ docker push lqwangxg/docker-node
The push refers to repository [docker.io/lqwangxg/docker-node]
461f4e4967c3: Pushed 
102e53cb9bb0: Pushed 
771e08c88aa6: Pushed 
67e0b3d71510: Pushed 
aedafbecb0b3: Mounted from lqwangxg/node 
db809908a198: Mounted from lqwangxg/node 
1b235e8e7bda: Mounted from lqwangxg/node 
3e207b409db3: Mounted from lqwangxg/node 
latest: digest: sha256:4c956862a45b68ecbc3545c12ab15ffdc506b1bde403794cfe2ebbae8b2abb20 size: 1992

没有评论: