Docker Build Method Steps for Deploying Node Project
- 2021-11-02 03:37:10
- OfStack
Client Docker
Docker Basic Operations
Mirror name
Pull mirror image
Other operations
Dockerfile
Docker-compose
Building nginx-node-postgres project
Some time ago, I made an node full stack project, and the server technology stack is nginx + koa + postgresql. Among them, it takes a lot of trouble to build and deploy the environment on centos, deploy the test server, and then deploy the production environment server when it goes online. There are a lot of boring, energy-consuming and thankless "physical work". So I began to think about how to automate this part of the construction and deployment, which led to Docker.
What is Docker
Docker is a lightweight virtualization technology than virtual machines, and the entities it virtualizes are called containers. The container itself is an sandbox with isolated scope, and it only contains the basic library and the services it hosts, which is very concise. After the container runs, it is only a process in the host machine, and the resources occupied are very small, which creates conditions for running container cluster on the operating system, and has excellent operability and flexibility.
What is the relationship between mirroring and containers? Mirrors can be regarded as classes (class) and containers as objects (object). Containers are generated by instantiation of mirrors. Of course, one mirror can generate multiple containers.
Client Docker
If we are not on the server, how can we use Docker on the client? Docker Desktop can be used on Windows and OSX, plus Kitematic, both desktop management tools, which are very convenient in general operation. Docker, Desktop and Kitematic only visualize some operations, and the command line is still necessary, because many operations can only be done on the command line.
Docker Basic Operations
Mirror name
For mirror labels, for example, nginx: 1.19. 0-alpine, 1.19. 0 is the version number of nginx, and alpine is the code name of os.
Jessie: debian 8 Stretch: debian 9 Buster: debian 10 Alpine: Alpine, recommended because it is very smallThe Alpine is the smallest version, and some are even 1/4 of other versions. This means building mirrors faster and more efficiently, because fewer components are loaded, which implies fewer vulnerabilities and more security.
Pull mirror image
docker pull nginx:1.19.0-alpine
Startup container
--name web: Specifies the container name as web -p 8080: 80: Container nginx listening port is 80, mapped to local port 8080 -v xxxx: xxxx: In this case, the local configuration file is mapped to the container nginx configuration file -d: Running in the background nginx: 1.19. 0-alpine: Image used
docker run --name web -p 8080:80 -v /usr/etc/nginx/nginx.conf:/etc/nginx/nginx.conf:ro -d nginx:1.19.0-alpine
Other operations
docker images # Display Image
docker rmi xxx # Remove Mirror
docker ps # Shows running containers
docker rm xxx # Remove Container
Dockerfile
It is convenient to use Dockerfile, which is the configuration file of the mirror. As long as there is Dockerfile, you can build the mirror at any time. Here's how to build a very simple nginx image, and from is the base image used in the build:
FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf
Docker-compose
When our project not only has a single container, but also needs to run multiple containers, and the containers need to communicate with each other, we need more powerful management tools. For example, k8s, but it is enough for our current small projects to use the official Docker-compose.
First you need the docker-compose. yml configuration file, such as the following templates for both containers, image for the image used, ports for the port mapping, and volumes for the data volume to be mapped:
version: "3"
services:
webapp:
image: web
ports:
- "8080:80"
volumes:
- "/data"
redis:
image: "redis:alpine"
You can then use the following command line:
docker-compose build [options] [SERVICE...] # Build (rebuild) the service container in the project
docker-compose up -d # Run compose Project , Background execution
docker-compose up is a powerful command that attempts to automate a series of operations including building the image, (re-) creating the service, starting the service, and associating the service-related container. All linked services will be started automatically unless they are already running. It can be said that most of the time, you can start a project directly through this command.
Building nginx-node-postgres project
With the above foundation, we can build our own project, first of all, dockerfile of node service, mainly doing the following steps
Create a container working directory Copy related configuration files to containers Install npm package in container Run pm2 to start the container
FROM node:14.5.0-alpine3.12
# Working directory
WORKDIR /usr/src/app
# Copy configuration file
COPY package*.json ./
COPY process.yml ./
RUN npm set registry https://registry.npm.taobao.org/ \
&& npm install pm2 -g \
&& npm install
# Use pm2 Management
CMD ["pm2-runtime", "process.yml", "--only", "app", "--env", "production"]
EXPOSE 3010
Then configure docker-compose. yml
db configures the database postgres, where the data volume volumes maps the database directory and initialization steps The app is configured with the node service, where the build is mapped to the directory where the dockerfile is located; depends_on denotes the dependent container, starting sequence, where db is started first and then node; links means mapping the name of db to the app container The nginx container depend_on is in the app container and is configured to forward the node service at the same time
version: '3'
services:
db:
image: postgres:12.3-alpine
container_name: postgres
environment:
- TZ=Asia/Shanghai
- POSTGRES_PASSWORD=xxxx
volumes:
- ./postgres/data:/var/lib/postgresql/data
- ./postgres/init:/docker-entrypoint-initdb.d
ports:
- 5432:5432
restart: always # Always restart , The recommended configuration in the production environment is always
expose:
- 5432
app:
image: koa-pg
container_name: koa
volumes:
- ./dist:/usr/src/app/dist
- ./logs:/usr/src/app/logs
build: ./
environment:
- TZ=Asia/Shanghai
restart: always
depends_on:
- db
links:
- db
expose:
- 3010
nginx:
image: nginx:1.19.0-alpine
container_name: nginx
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
ports:
- 8080:80
environment:
- TZ=Asia/Shanghai
restart: always
depends_on:
- app
links: # host Noun substitution ip Configure nginx Forwarding of
- app
expose:
- 8080
After configuring our project, the next step is to run it
docker-compose up
This is true in our local development machine, and it is also true to deploy to servers. You can deploy several servers if you want to deploy them. As long as you install docker, it can be solved by one command line.
To start several containers, modify the configuration of docker-compose. yml, again docker-ES211up, ES213easy!