Summary
This post is a basic primer on getting a Node app containerized.
Step 1: Create the app
Below is a basic HTTP server implementation using
Express.
'use strict';
const express = require('express');
const app = express();
const port = process.env.PORT || 8080;
app.get('/', function(req, res) {
res.send('hello world')
});
app.listen(port, () => {
console.log(`listening on ${port}`);
});
Below is the resulting package.json containing the app dependencies. This file is created via the '
npm init' command. '
npm install --save' causes the dependencies to be updated for each module used in the app.
{
"name": "containertest",
"version": "1.0.0",
"description": "simple express server",
"main": "server.js",
"repository": "none",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "joey whelan",
"license": "MIT",
"dependencies": {
"express": "^4.17.1"
}
}
Step 2: Create the Dockerfile and .dockerignore
Below is the
Dockerfile to support the above app. It's written per best practices to cause the app source code to be added as the last layer to enable caching of modules.
FROM node:lts
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD ["npm", "start"]
.dockerignore below.
node_modules
npm-debug.log
Step 3: Build the Docker image
$ docker build -t containertest .
Sending build context to Docker daemon 19.97kB
Step 1/7 : FROM node:lts
lts: Pulling from library/node
844c33c7e6ea: Pull complete
ada5d61ae65d: Pull complete
f8427fdf4292: Pull complete
f025bafc4ab8: Pull complete
7a9577c07934: Pull complete
9b4289f800f5: Pull complete
55e3fcab47b9: Pull complete
c7a94e331913: Pull complete
bb9efc0c132a: Pull complete
Digest: sha256:88ee7d2a5e18d359b4b5750ecb50a9b238ab467397c306aeb9955f4f11be44ce
Status: Downloaded newer image for node:lts
---> 7be6a8478f5f
Step 2/7 : WORKDIR /usr/src/app
---> df2833d84c36
Removing intermediate container 11f00574e18a
Step 3/7 : COPY package*.json ./
---> a892506a76df
Removing intermediate container 07fc76863a41
Step 4/7 : RUN npm install
---> Running in 7abdbc6d6e64
added 50 packages from 37 contributors and audited 126 packages in 1.197s
found 0 vulnerabilities
---> 9ab0afa5e750
Removing intermediate container 7abdbc6d6e64
Step 5/7 : COPY . .
---> 39620b323b38
Removing intermediate container f2692a5064a0
Step 6/7 : EXPOSE 8080
---> Running in 4e758301eaad
---> 076d56510119
Removing intermediate container 4e758301eaad
Step 7/7 : CMD npm start
---> Running in 80f1a6be42cc
---> c9c55b4ddca7
Removing intermediate container 80f1a6be42cc
Successfully built c9c55b4ddca7
Successfully tagged containertest:latest
Resulting images below:
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
containertest latest c9c55b4ddca7 3 minutes ago 911MB
node lts 7be6a8478f5f 2 weeks ago 908MB
Step 4: Run the container
$ docker run -p 8080:8080 -d containertest
d8d98f3fff8e752c186f99599ca475682790e3a5645d16705a398404ddf9ec74
Resulting running container below:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d8d98f3fff8e containertest "docker-entrypoint..." 2 minutes ago Up 2 minutes 0.0.0.0:8080->8080/tcp thirsty_dijkstra
Execution of HTTP request against the containerized server below:
$ curl -v localhost:8080/
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.58.0
> Accept: */*
>
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Content-Type: text/html; charset=utf-8
< Content-Length: 11
< ETag: W/"b-Kq5sNclPz7QV2+lfQIuc6R7oRu0"
< Date: Mon, 09 Dec 2019 16:44:36 GMT
< Connection: keep-alive
<
* Connection #0 to host localhost left intact
hello world
Step 5: Stop the container
Stop command results below. Note it's possible to abbreviate the container ID.
$ docker stop d8
d8
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Step 6: Clean up
Commands below to delete the container and its image.
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d8d98f3fff8e containertest "docker-entrypoint..." 17 minutes ago Exited (0) 4 minutes ago thirsty_dijkstra
$ docker rm d8
d8
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
containertest latest c9c55b4ddca7 26 minutes ago 911MB
node lts 7be6a8478f5f 2 weeks ago 908MB
$ docker rmi c9
Untagged: containertest:latest
Deleted: sha256:c9c55b4ddca7aac2d12a93c66ee1adf95b5e2b4b09a7b93aac2e0981b45006be
Deleted: sha256:076d565101195290512b90eba0d25f16a3798db0f1cf49aea4d096e926d03250
Deleted: sha256:39620b323b38b824b8f074183eda49339748f2516a70d19718d821a086826234
Deleted: sha256:20407ee7b27893df082e6fa7eddb9608517d53a930beaf426c37ac2453949714
Deleted: sha256:9ab0afa5e750b610d08ed12258972e8d880d8acdd8b3034bd96add8c5daea705
Deleted: sha256:b972df701627963f9fa4dbb2ef1c20148cdddd8a8922aea6c3ba8e2ceca62c27
Deleted: sha256:a892506a76df6ceaaff88b3fe14ee30de477fc9596cb8236aeeee0b3a0106e76
Deleted: sha256:4fab789311ef158be2b924dcdaa1646802900913e07d645f95adb299ee09c506
Deleted: sha256:df2833d84c365c86e3c5218cc997d3ec958e1e4f68eb47cb82483cbd2a14c738
Deleted: sha256:43dd5d9ada9dc352d7fdf5cd3b179cd0855681851eef378a5c82b3ce682bc17e
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
node lts 7be6a8478f5f 2 weeks ago 908MB
Copyright ©1993-2024 Joey E Whelan, All rights reserved.