How to deploy nodejs service using Dockerfile

How to deploy nodejs service using Dockerfile

Initialize Dockerfile

Assuming our project is named express, create and edit the Dockerfile file in the express project:

$ vim Dockerfile

FROM node:latest

RUN mkdir -p /home/www/express
WORKDIR /home/www/express

COPY . /home/www/express

RUN npm install

EXPOSE 3000

ENTRYPOINT ["npm", "run"]
CMD ["start"]

This file contains the following commands:

  • FROM node:latest - specifies to use the latest version of the node base image
  • RUN mkdir -p /home/www/express – creates the /home/www/express directory inside the container
  • WORKDIR /home/www/express - Set the working directory inside the container to /home/www/express
  • COPY . /home/www/express - Copies the contents of the current directory on the host to the mirror directory /home/www/express
  • RUN npm install - npm install installs the NPM packages required by the application
  • EXPOSE 3000 - Open the container's port 3000 to the outside world
  • ENTRYPOINT ["npm", "run"] - The command to execute after the container is started. Cannot be overridden by parameters provided by docker run
  • CMD ["start"] - The command to execute when the container starts. It can be overridden by the parameters provided by docker run

Build the image

After writing the Dockerfile file, you can build the image through the docker build command:

$ sudo docker build -t test/express .

We use the -t parameter to name the image test/express. The build process is similar to the following:

Sending build context to Docker daemon 29.7 kB
Step 1/8 : FROM registry.src.followme.com:5000/node:v1
 ---> c99c549e8227
Step 2/8 : RUN mkdir -p /home/www/express-app
 ---> Running in 8be9a90629b0
 ---> b9f584851225
Removing intermediate container 8be9a90629b0
Step 3/8 : WORKDIR /home/www/express-app
 ---> 5072c31f9dd9
Removing intermediate container e9dbf4ce3d8b
Step 4/8 : COPY . /home/www/express-app
 ---> a4d1725f15ed
Removing intermediate container 30aa49765015
Step 5/8 : RUN yarn
 ---> Running in f181c243deaa
yarn install v1.3.2
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
Done in 9.46s.
 ---> d390931d73e6
Removing intermediate container f181c243deaa
Step 6/8: EXPOSE 3000
 ---> Running in 94101ab38864
 ---> 43199a8a5a90
Removing intermediate container 94101ab38864
Step 7/8: ENTRYPOINT npm run
 ---> Running in 80b1318962cf
 ---> 6b203c50e855
Removing intermediate container 80b1318962cf
Step 8/8 : CMD start
 ---> Running in a9909e537f59
 ---> d56eae48377c
Removing intermediate container a9909e537f59
Successfully built d56eae48377c

Running the container

After the image is built, you can create/run a container through the built image to realize the Docker deployment of the express application.

Run a container using the tets/express image:

$ sudo docker run -d --name experss-app -p 3000:3000 test/express

In the above operation, we ran the container through the test/express image and named the container express-app. When running the container, we also specify the -d parameter, which makes the container run in the background. The -p parameter maps port 3000 of the host to port 3000 of the container. After running the container, you can use the docker ps command to see the running container. The service can now be accessed via localhost:3000.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of CMD and ENTRYPOINT commands in Dockerfile
  • How to write the best Dockerfile
  • Detailed explanation of COPY and ADD commands in Dockerfile
  • A detailed introduction to the Dockerfile image building file and related commands in Docker
  • Detailed explanation of using Dockerfile to build MySQL image and implement data initialization and permission setting
  • Detailed explanation of the specific use of the ENV instruction in Dockerfile
  • Dockerfile usage examples
  • Dockerfile to create the official Tomcat image and detailed explanation of image usage
  • Summary of common commands in Dockerfile
  • How to deploy SpringBoot project using Dockerfile
  • How to use Dockerfile to create a mirror of the Java runtime environment
  • How to create your own image using Dockerfile
  • Introduction to Dockerfile instructions ADD and COPY
  • Detailed explanation of multi-stage (multi-stage build) in Dockerfile
  • Docker Basics: Detailed Explanation of Dockerfile Commands
  • Dockerfile instructions explained
  • A brief introduction to the Dockerfile instruction VOLUME
  • Dockerfile simple introduction

<<:  How to install and configure mysql 5.7.19 under centos6.5

>>:  Uniapp's experience in developing small programs

Recommend

Detailed introduction to CSS priority knowledge

Before talking about CSS priority, we need to und...

Detailed explanation of BOM and DOM in JavaScript

Table of contents BOM (Browser Object Model) 1. W...

Summary of methods for inserting videos into HTML pages

Now if you want to use the video tag in a page, y...

Two common solutions to html text overflow display ellipsis characters

Method 1: Use CSS overflow omission to solve The ...

React+Antd implements an example of adding, deleting and modifying tables

Table of contents Table/index.js Table/model/inde...

How to install docker using YUM

As shown in the following figure: If the version ...

How to develop Java 8 Spring Boot applications in Docker

In this article, I will show you how to develop a...

The solution record of Vue failing to obtain the element for the first time

Preface The solution to the problem of not being ...

Three ways to share component logic in React

Without further ado, these three methods are: ren...

Use href to simply click on a link to jump to a specified place on the page

After clicking the a tag in the page, you want to ...

Specific usage of CSS compound selectors

Intersection Selector The intersection selector i...