Docker is a tool that simplifies inter-team application development environments. Applications are packaged in a container that provides a better experience for developers during development, testing, and deployment. This container is created by the Docker image in which all dependencies and runtime information runs.

In recent years, developers have embraced these approaches. Because they don’t have to worry about installing dependencies anymore.

In this chapter, we’ll discuss some specific steps to Dokcer our Node application. We’re not going to explain what Docker is, we’re going to explain why we need it.

Preliminary knowledge

  • Familiar with Node and NPM
  • Docker basics
  • Docker has been installed and has a Docker Hub account

If you don’t have Dockler installed, you can go to the Docker website and follow the steps. Some concepts you may not understand, you can baidu, not complex.

Step 1 – Install a Node(Express) service

We will use Express, where we use scaffolding to quickly generate an application structure. Installation of the generator

$ npm install -g express-generator
Copy the code

Generate an application structure

$ express --no-view nodeDockerApp
Copy the code

Go to the file directory and you can rename your application. In this case, we’ll call it ‘nodeDockerApp’ and the file directory will look something like this:

. ├ ─ ─ app. Js ├ ─ ─ bin │ └ ─ ─ WWW ├ ─ ─ package. The json ├ ─ ─ public │ ├ ─ ─ images │ ├ ─ ─ javascripts │ └ ─ ─ stylesheets │ └ ─ ─ Style. CSS ├─ routes │ ├─ index.js │ ├─ ├─ users.jsCopy the code

Then remove the public and bin directories and focus on our server-side content:

$ rm -r public bin
Copy the code

Then, create the service in app.js:

const express = require('express'); const logger = require('morgan'); const indexRouter = require('./routes/index'); const usersRouter = require('./routes/users'); const port = 5000; const app = express(); app.use(logger('dev')); app.use(express.json()); app.use(express.urlencoded({ extended: false })); app.use('/', indexRouter); app.use('/users', usersRouter); app.listen(port, function () { console.log('App listening on port 5000! '); }); module.exports = app;Copy the code

Add these dependencies and scripts to the package.json file:

{" name ":" nodedockerapp ", "version" : "0.0.0", "private" : true, "scripts" : {" start ":" node app. Js ", "dev" : "Nodemon app. Js"}, "dependencies" : {" debug ":" ~ 2.6.9 ", "express" : "~ 4.16.1", "Morgan" : "~ 1.9.1}", "devDependencies" : {"nodemon": "^2.0.7"}}Copy the code

Then define the routing file and paste the following code in the routes/users.js file:

const express = require('express');
const router = express.Router();

/* GET users listing. */
router.get('/', function (req, res, next) {
  const data = {
    name: 'Victor Jonah',
    job: 'Technical Author',
  };
  res.status(200).send(data);
});

/* Add user to the list. */
router.post('/', function (req, res, next) {
  const { name, job } = req.post();
  const data = {
    name,
    job
  }
  res.status(200).send(data);
});

module.exports = router;
Copy the code

All dependencies and server files are in place, and we’re up and running

Start the app:

npm run dev
Copy the code

We’ll see these:

Running in a terminal:

curl http://localhost:5000/users
Copy the code

You should see the following:

Step 2 — Docker-ify your app

Now that we have the application running, we create a Dockerfile to build our Docker image.

Before we can begin, we have to perform some steps. The first step, again, is to create a Dockerfile, which is a blueprint that provides the Docker image in turn as we execute.

Execute in terminal:

touch Dockerfile
Copy the code

Then, add the following structure to the Dockerfile:

FROM node:14 # Create app directory RUN mkdir -p /usr/src/nodedockerapp WORKDIR /usr/src/nodedockerapp # Install dependencies COPY package.json /usr/src/nodedockerapp RUN npm install # Push in app source COPY . /usr/src/nodedockerapp  CMD [ "/bin/bash" ]Copy the code

That’s enough. Let’s explain it line by line:

FROM node:14
Copy the code

Here, we tell Docker to pull the image from the Dockerhub, which contains all the application code and tools. After the image runs, it becomes a container. The first line creates a file container directory, and the second line makes it our working directory. This means that the container only works in this directory.

COPY package.json  /usr/src/nodedockerapp

RUN npm install

Json to our working directory, and then install all dependencies

# Push in app source

COPY  .   /usr/src/nodedockerapp

Copy the home directory (all files) into the container directory. Step 3 – Ignore the files in the container

Some of the files in the container are not necessary, so we can ignore them and use.dockerignore

.git node_modules

It looks like a.gitignore file

Step 4 – Build the image

The final step is to build and run the image, and you have the container.

In the home directory of the Dockerfile file, we execute:

docker build -t nodedockerapp .
Copy the code

You can list all the dokcer images you’ve built that are available:docker images:

We run the image to generate the container. At the same time, we also need to map the service port of the local machine and the port of DOcker, so that we can run the server in DOcker and also connect.

Run:

docker run -it -p 5000:5000 nodedockerapp
Copy the code

So our service runs in a container with port 5000, which connects to our local machine

Test our port:

So you can see that the application works well in the container and sometimes we have multiple containers running in the application and we need to docker-composeIt helps us manage all of our containers so that it behaves like there is only one container.