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

CSS pseudo-class: empty makes me shine (example code)

Anyone who has read my articles recently knows th...

JavaScript Html to implement the mobile red envelope rain function page

This article example shares the specific code of ...

Sample code for installing Jenkins using Docker

Two problems that are easy to encounter when inst...

Detailed explanation of redundant and duplicate indexes in MySQL

MySQL allows you to create multiple indexes on th...

Detailed explanation of the usage of DECIMAL in MySQL data type

Detailed explanation of the usage of DECIMAL in M...

Three examples of blur background effects using CSS3

Let’s not start with the introduction and get str...

MySQL data duplicate checking and deduplication implementation statements

There is a table user, and the fields are id, nic...

How to deploy Solidity smart contracts using ethers.js

If you have developed DApps on Ethereum, you may ...

Vue ElementUI Form form validation

Form validation is one of the most commonly used ...

Detailed tutorial for installing mysql5.7.21 under Windows system

MySQL Installer provides an easy-to-use, wizard-b...

Steps for Docker to build a private warehouse Harbor

Harbor Harbor is an open source solution for buil...

MySQL uses custom sequences to implement row_number functions (detailed steps)

After reading some articles, I finally figured ou...