Docker uses dockerfile to start node.js application

Docker uses dockerfile to start node.js application

Writing a Dockerfile

Taking the directory automatically created by express as an example, the directory structure is as follows:

├── /bin
│ └── www
├── /node_modules
├── /public
├── /routes
├── /views
├── package-lock.json
├── package.json
├── ecosystem.config.js
├── app.js
└── Dockerfile

Create a new Dockerfile file in the project directory

FROM node:10.15
MAINTAINER [email protected]
COPY ./app/
WORKDIR /app
RUN npm install pm2 -g
EXPOSE 8003
CMD ["pm2-runtime", "ecosystem.config.js"]
  • FROM specifies that the base image is version 10.15 of node (the official version of node can be viewed here)
  • MAINTAINER describes the maintainer of the image
  • The COPY command copies files from the host machine to the image. The format is COPY [--chown=<user>:<group>] <source path>... <destination path>. Here, all files in the project directory are copied to the /app directory in the image. If the target path does not exist, Docker will automatically create it.
  • WORKDIR is used to specify the working directory, which is the directory where CMD is executed.
  • The RUN command is used to execute shell commands, here it is used to install pm2
  • The EXPOSE command is used to declare the service port provided by the runtime container, but please note that the service of this port will not be enabled at runtime. This command is mainly to help users understand the daemon port of the mirror service to facilitate configuration mapping; in addition, when using random port mapping, it will automatically randomly map the EXPOSE port
  • CMD is the default startup command of the container main process

Build the image

Execute in the project directory

docker build -t express-app:v1 .

If the build is successful, check the mirror list

docker images

The image express-app:v1 should be output

Running the container

docker run -d -p 8003:3000 --name="express-app" express-app:v1

docker run is an abbreviation of the two commands docker create and docker start .

  • -d stands for --detach , which means letting the container run in the background.
  • -p specifies the port mapping between the host and the container. The left side is the port of the host, and the right side is the port of the container. That is to say, accessing port 8003 of the host will be mapped to port 3000 in the container.
  • --name sets the container alias. If not specified, Docker will randomly generate a name, such as tender_swirles .

implement

docker ps

The normal display is as follows

Add the -a parameter to view all started containers.

Entering the container

If you want to enter the container to operate, execute the following command

docker exec -it express-app bash 

-i and -t are usually used together. -i starts the interactive mode, -t specifies that a terminal needs to be allocated. You can try the effect of not passing one of them.

Similar to exec is the attach command, which is docker attach express-app . However, exiting from stdin will stop the container, so it is recommended to use the exec command.

Close Operation

Stop the container

docker stop express-app

Deleting a container

docker rm express-app

If the container is still running when you delete it, you need to add the -f parameter

Deleting an image

docker rmi express-app:v1

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:
  • How to create your own image using Dockerfile
  • How to use Dockerfile to build images in Docker
  • Idea configures maven-tomcat-plugin to implement project deployment
  • Detailed explanation of the role of the spring-boot-maven-plugin plugin
  • Use Maven shade plugin to create executable Jar package in Java
  • dockerfile-maven-plugin minimalist tutorial (recommended)

<<:  Detailed tutorial on MySql installation and uninstallation

>>:  Tips for writing concise React components

Recommend

Alibaba Cloud applies for a free SSL certificate (https) from Cloud Shield

Because the project needs to use https service, I...

Nginx dynamically forwards to upstream according to the path in the URL

In Nginx, there are some advanced scenarios where...

Detailed explanation of Vue slot

1. Function : Allows the parent component to inse...

mysql splits a row of data into multiple rows based on commas

Table of contents Separation effect Command line ...

How to specify parameter variables externally in docker

This article mainly introduces how to specify par...

JavaScript array deduplication solution

Table of contents Method 1: set: It is not a data...

Summary of MySQL injection bypass filtering techniques

First, let’s look at the GIF operation: Case 1: S...

What are the new CSS :where and :is pseudo-class functions?

What are :is and :where? :is() and :where() are p...

Linux kernel device driver memory management notes

/********************** * Linux memory management...

Detailed explanation of concat related functions in MySQL

1. concat() function Function: Concatenate multip...

A detailed introduction to setting up Jenkins on Tencent Cloud Server

Table of contents 1. Connect to Tencent Cloud Ser...

Detailed explanation of Angular structural directive modules and styles

Table of contents 1. Structural instructions Modu...

How to install a virtual machine with Windows services on Mac

1. Download the virtual machine Official download...