The goal of this project is training you in how to get a Node.js application running into a Docker container.
This guide assumes that you have a working Docker installation and a basic understanding of how a Node.js application is structured.
We need to create a directory to save all files of our project. So:
$ mkdir study-dockerizing-a-node-api
$ cd study-dockerizing-a-node-apiAfter create the directory, we need a package.json to create a Node.js API:
$ npm initAfter that install the express dependency:
$ npm i express --saveEdit and create a new script to start the node server in the package.json:
"start": "node index.js",Now we have a package.json:
{
"name": "study-dockerizing-a-node-api",
"version": "0.0.1",
"description": "A repository containing an article and example to start my docker studies",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/mmalaquiasdev/study-dockerizing-a-node-api.git"
},
"keywords": [
"Docker",
"node",
"study",
"api"
],
"author": "Mateus Malaquias",
"license": "MIT",
"bugs": {
"url": "https://github.com/mmalaquiasdev/study-dockerizing-a-node-api/issues"
},
"homepage": "https://github.com/mmalaquiasdev/study-dockerizing-a-node-api#readme",
"dependencies": {
"express": "^4.16.4"
}
}Now create a index.js file that defines a web app using the Express.js framework:
const express = require('express');
const PORT = 8080;
const HOST = '0.0.0.0';
const app = express();
app.get('/', (req, res) => {
res.send('Hello world\n');
});
app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);A Dockerfile is a text document that contains all the commands that a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.
Create an empty file called Dockerfile:
$ touch DockerfileOpen this file with your favorite text editor. And lets go!
$ vim DockerfileYour Dockerfile should look like this:
# Node runtime version
FROM node:8
# The api directory inside of the docker image
WORKDIR /app
# A wildcard is used to ensure both package.json AND package-lock.json are copied to the docker image
COPY package*.json ./
# Copy the project to the docker image
COPY . .
# Install the API dependencies with NPM
RUN npm install
# Informe to the docker, the door let's use
EXPOSE 8080
CMD ["npm", "start"]Go to the directory that has your Dockerfile and execute the following command to build the Docker image.
$ docker build -t <your username>/node-hello-world .Running your image with -d runs the container in detached mode, leaving the container running in the background. The -p flag redirects a public port to a private port inside the container.
$ docker run -p 8080:8080 -d <your username>/node-web-appOpen your web browser or postman application and make a request to the URL above:
http://localhost:8080
Or use the curl application inside the terminal.
$ curl -i localhost:49160You need to see a simple Hello world message.
I hope this tutorial helped you get up and running a simple Node.js application on Docker.
Before creating a docker image it's intresting to make a file named .dockerignore in the root directory. When this file exists the CLI modifies the context to exclude files and directories that match patterns in it. This will help us avoid unnecessarily sending large or sensitive files and directories to the image when using ADD or COPY command.
So create the file and edit it:
$ touch .dockerignore
$ vim .dockerignoreWrite the content above:
node_modules
npm-debug.log
This will prevent your local modules and debug logs from being copied into your Docker image.
- Dockerfile reference
- Dockerizing a Node.js web app
- Trying Docker for the First Time 📖 Many Learnings!
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
See also the list of contributors who participated in this project.
This project is licensed under the MIT License - see the LICENSE.md file for details