preface

This article explains how to quickly and easily deploy a Node API project. Can be used as an introduction to Docker.

Node projects are based on the Express + Sequelize framework.

The database uses mysql.

Docker installation

Docker can be downloaded from docs.docker.com/get-docker

Check the Docker installation version: $Docker –version

Dockerfile

A Dockerfile is a text file used to build an image. The text content contains the instructions and instructions required to build the image. Dockerfile learning address: www.runoob.com/docker/dock…

Write the Dockerfile file in the project root directory:

# Custom mirror based on Node :12.1FROM the node: 12.1# Mirror author
LABEL maintainer="[email protected]"  

Specify a path to file to container
COPY . /home/funnyService  

RUN/CMD to RUN in the working directory
WORKDIR /home/funnyService  

Specifies the environment variable NODE_ENV as production
ENV NODE_ENV=production  

# installation of yarn
RUN npm install yarn -g  

Initialize the project
RUN yarn install    

# declare port
EXPOSE 3000    

Run the node project '$node SRC /app.js'
CMD [ "node"."src/app.js" ] 

CMD runs when docker runs. RUN is in the Docker build.
Copy the code

docker-compose

Compose is a tool for defining and running multi-container Docker applications. With Compose, you can use YML files to configure all the services your application needs. Then, with a single command, all services can be created and started from the YML file configuration. Docker – compose learning address: www.runoob.com/docker/dock…

Docker-comemess. yml: docker-comemess. yml: docker-comemess. yml

# funny-app specifies the container name
container_name: 'funny-app'  

/Dockerfile file./Dockerfile
build: .   

# specify the image to run by the container with the name set to funny-node:2.0
image: 'be hilarious - node: 2.0'  

Host port: Container port
ports:           
  - '3000:3000'
Copy the code

Local debugging

$docker-compose up -d is executed in the project root directory

$docker images = $docker images = $Docker images = $Docker images

$docker ps check if there is a funny-app container

Debugging interface http://127.0.0.1:3000/test/demo success:

Server deployment running

Git pull the project on the server

Enter $docker-compose up -d

Use $Docker images $docker ps to check if the build and startup were successful

Debugging interface http://server IP address :3000/test/demo

Thank you

Thanks to the help of Technology guru Benny, I have a better understanding of the use of Docker.

Benny tech blog full of dry goods: www.benny.wiki

Reprint statement: please indicate the author, mark the original link, if you have questions, send to [email protected]