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

In-depth analysis of nginx+php-fpm service HTTP status code 502

One of our web projects has seen an increase in t...

CentOS 8 officially released based on Red Hat Enterprise Linux 8

The CentOS Project, a 100% compatible rebuild of ...

A must-read career plan for web design practitioners

Original article, please indicate the author and ...

How to view the status of remote server files in Linux

As shown below: The test command determines wheth...

A detailed introduction to HTML page loading and parsing process

The order in which the browser loads and renders H...

61 Things Every Web Developer Should Know

Normally, you'll need to read everyone's s...

How to Dockerize a Python Django Application

Docker is an open source project that provides an...

An example of implementing a simple infinite loop scrolling animation in Vue

This article mainly introduces an example of Vue ...

A method of making carousel images with CSS3

Slideshows are often seen on web pages. They have...

Solution to define the minimum height of span has no effect

The span tag is often used when making HTML web pa...

Solution to the failure of loading dynamic library when Linux program is running

Unable to load dynamic library under Linux When t...

Solution to the problem "Table mysql.plugin doesn't exist" when deploying MySQL

Today I deployed the free-installation version of...

Solve the MySQL login 1045 problem under centos

Since the entire application needs to be deployed...

Detailed explanation of keepAlive usage in Vue front-end development

Table of contents Preface keep-avlive hook functi...