In the previous post we saw how to build a HelloWorld Docker image, in this post we will create a Docker image for a Node.js application.
First let us create a basic Node server using Express which will respond with a Greeting message in the root path (/).
server.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 Node from Docker !');
});
app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);
Notice that we are using host as 0.0.0.0 instead of localhost, this is needed when configuring Node for Docker image. Next we will create the Dockerfile for the Node App image. This time we will need Node as the base image so we will start the Docker file using FROM node:12
Dockerfile
# use Node base image
FROM node:12
# Create app directory
WORKDIR /usr/src/app
# Copy app files into to Docker image folder
COPY . .
# Expose Port 8080, lets Docker know that the container listens on the network port 8080 at runtime
EXPOSE 8080
# Start the Node server
CMD [ "node", "server.js" ]
We are using COPY .. Command to copy all the Node app files including
the node_modules folder in this example. Alternatively we can choose to copy
only the package.json file and use npm install to download the npm packages
when building the Docker image.
COPY package*.json ./
RUN npm install
$ docker build -t hellonode .
Sending build context to Docker daemon
2.011MB
Step 1/5 : FROM node:12
current-slim: Pulling from library/node
75cb2ebf3b3c: Pull complete
1c6bd282486b: Pull complete
82de372909de: Pull complete
f1c229111278: Pull complete
2d6a826aec8f: Pull complete
Digest:
sha256:bca7816a397f236e1b03e84a2ca5d775139c1a9746fa793e552c4c8a5b7e5298
Status: Downloaded newer image for node:current-slim
---> 84bcb5d06aea
Step 2/5 : WORKDIR /usr/src/app
---> Using cache
---> 1371d5d82734
Step 3/5 : COPY . .
---> 85901319d9fa
Step 4/5 : EXPOSE 8080
---> Running in 4e7c877a4773
Removing intermediate container 4e7c877a4773
---> 09d57d8db618
Step 5/5 : CMD [ "node", "server.js" ]
---> Running in 2bed0b8d0cdd
Removing intermediate container 2bed0b8d0cdd
---> 11b082c6d789
Successfully built 11b082c6d789
Successfully tagged hello-node:latest
SECURITY WARNING: You are building a Docker image from Windows against a
non-Windows Docker host. All files and directories added to build context will
have '-rwxr-xr-x' permissions. It is recommended to double check and reset
permissions for sensitive files and directories.
Check that the Docker image is created by running the image ls command
>docker image ls
REPOSITORY TAG
IMAGE ID
CREATED SIZE
hello-node
latest
11b082c6d789 4 seconds ago
919MB
hello-world latest
bf756fb1ae65
7 months ago 13.3kB
Next run the Docker image to create the Docker Container
>docker run -p 8000:8080 -d
hello-node
8ead946984e54b9159fb2c31fddf3fad8f56f687fcd790749a04ae66a4c47202
The -p option maps port 8080 of the host to port 8080 of the container. The -d
option causes the container to be run in the background and to output a long
identifier that is the unique identifier of the container.
To make sure that the Node App is up and running execute the command docker ps to get the container_id
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
40959f9c052b
hellonode
"docker-entrypoint.s"
34 seconds ago Up 32 seconds 0.0.0.0:8000->8080/tcp crazy_bose
Then execute the command docker logs <container_id> to see the
logs from the Node server as follows
$ docker logs 40959f9c052b
Node Server running at http://0.0.0.0:8080
Now that we made sure that the Node server is up and running in the Docker
container, we can open a browser and browser the URL http://localhost:8000
You can see the Greetings message from Node, if you are running Docker for Windows then you can see the live running container in the Docker Dashboard as follows.
No comments:
Post a Comment