Docker deployment front-end project [practice, record]

This article only records the basic knowledge and initial deployment practice of the blogger after learning and practicing docker technology. Please correct me if there are any errors.

Getting to know docker

One: docker composition

Insert picture description here

How Docker works

Docker is a system based on the C/S structure. The Docker daemon runs on the Linux server (host). When we enter Docker-related commands on the Linx server (Docker-Client), it will be sent to Doker-Server.

Two: docker host【recognition】

1. Image

A Docker image is equivalent to a file system, which carries all the data required to create a specific type of container. According to a certain type of image, many container instances can be created.

2. Container (container)

The container can be regarded as a simple version of the Linux system, running on the host, but it has its own file system and network.

Three: docker client [interactive]

Note: The following content classifies the various behaviors of each object. According to the process logic, it can be divided into five behaviors: crud action, config action, host action, controlOne action, and manageAll action. The following content only focuses on the basic behavior logic of each object, and the specific commands are searched for by yourself.

1. Manage docker service

crud action
  • Installation (C): linux / window
  • Description (R)
docker version
docker info
  • Uninstall (D)
config action
  • Set up mirror warehouse
  • Set up Alibaba Cloud image acceleration
controllOne action
  • (Boot from) start docker service
  • Shut down the docker service

2. Manage docker images

crud action
  • Pull (C): pull
  • Build (C): build
  • View (R)
docker inspect 镜像id
  • Delete (D)
# deleteAll
docker rmi -f $(docker images -aq) 
# deleteOne
docker rmi -f 镜像id                      
# deleteMany
docker rmi -f 镜像id 镜像id 镜像id 镜像id
manageAll action
  • View all mirrors
# findAll
docker images
# findOne
docker search 镜像名

3. Manage docker containers

crud action
  • Create (C)
docker run 可选参数 镜像名
# 可选参数
--name=”xxx“    # 容器名字
-d              # 后台模式运行
-it             # 使用交互模式运行,进入容器查看内容
-p(小学)         # 指定端口映射 如:-p 8080(宿主机):8080(容器)
-P(大写)         # 随机端口映射
--net           # 网络模式
-v              # 数据卷
  • View (R)
docker inspect 容器id
  • Delete (D)
# deleteAll
docker rm -f $(docker ps -aq)
# deleteOne
docker rm -f 容器id
host action
  • file transfer
docker cp 容器id:文件路径(容器内) 目的路径(宿主机)
[root@MT ~]docker cp 1a9a6785c37c:/home/c.java /home/
  • process
docker top 容器id
  • Shared files, shared network (specified in the form of parameters when running the image to obtain the container instance)
controllOne action
  • Start and stop
docker start 容器id      # 启动容器
docker restart 容器id    # 重启容器
docker stop 容器id       # 停止正在运行的容器
docker kill 容器id       # 强制停止
  • Entry and exit
# 进入
docker exec -it 容器ID /bin/bash
# 退出
exit          # 容器直接退出
Ctrl + P + Q  # 容器不停止退出
  • View log
docker logs -tf 容器id             # 查看实时日志  
docker logs -t --tail 数量 容器id  # 查看指定数量的日志   
manageAll action
  • View all/running/stop containers
docker ps
docker ps -a -q

Four: docker Registry [reference]

Docker Registry is a place where image files are centrally stored, and imageName and imageTag together determine an image.

1.docker Hub

Official website: https://hub.docker.com/, playing a similar role to github, you can search for the required mirror here and read the document.

2. Alibaba Cloud Warehouse

The domestic public warehouse, the mirror download speed is fast.

Practice docker deployment

Step 1: Project development stage [output git repo(project and dockerfile)]

1. Write dockerfile

What is dockerfile

Dockerfile is a text file used to build an image. The text content contains instructions and instructions for building an image.

Dockerfile common configuration items
  • If English is good, see official documents, if English is poor, see the rookie tutorial and other materials.
Front-end project deployment example dockerfile

Note: Since the deployment method adopted is the method of nginx proxying to multiple http services, the following example is just to build a single http service image based on the node environment.

FROM node:12

RUN mkdir -p /home/docs
WORKDIR /home/docs

ADD package.json .
RUN yarn config set registry 'https://registry.npm.taobao.org'
RUN yarn

COPY . /home/docs

RUN yarn docs:build

EXPOSE 3386
# 以下server.js即是一个在node环境下,通过express框架搭建http服务的脚本。
CMD ["node", "server.js"]

2. Prepare data and script (involved in dockerfile)

project code(data)
server.js(script)
// express依赖
const http = require('http')
const path = require('path')
const bodyParser = require('body-parser')
const express = require('express')
// ecstatic依赖
const ecstatic = require('ecstatic')
// connect-history-api-fallback依赖
const history = require('connect-history-api-fallback')

const app = express()
app.use(bodyParser.json())

app.use(history())

app.use(ecstatic({
    
     root: path.join(__dirname, './dist') }))

http.createServer(app).listen(process.argv[2] || 3386)

Step 2: Front-end packaging stage [input git repo, output image (after test)]

1.Clone

# 在打包机上
mkdir codePath
cd codePath
git clone https://github.com/xx/xx.git

2.Build

cd projectPath
docker build -t xx/node:tagName .

3.Run

docker run -d -p 3386:3386--name containerName xx/node:tagName
curl 127.0.0.1:7788 # test

4.Share

No practice, please refer to https://www.runoob.com/docker/docker-repository.html

Step 3: The server runs the container stage [input image, running]

Note: The following project deployment method is nginx container + multiple http server containers

1.nginx service

Requirement: nginx first level routing controls access to different projects, such as 127.0.0.1/project1 to access project1, and 127.0.0.1/project2 to access project2.

first: pull nginx service, create nginx container, start nginx container
  • Pull, create and start: Develop the habit of looking through the document after looking up the mirror from Docker Hub (it is better to teach it to fish than to teach it, haha).
  • Note: For cross-container forwarding, since the host and each container have their own network and port, network and port mapping needs to be done. You can let the nginx container share the network with the host by letting the nginx container –net=host , And then realize the request forwarding requirements of nginx container -> host -> project container (note that in practice, unexpected errors occur in the docker for window environment, and Linux is normal).
second: Add a request forwarding configuration for a specific route
  • Add proxy configuration
docker ps
# 进入容器
docker exec -it nginxId /bin/bash
# 修改配置
vi /etc/nginx/conf.d # vi编辑器问题,1.command not find:执行 apt-get install vim 2.找不到包:apt-get update
--- conf.d
# 对应server下添加如下配置
	location /project1{
    
    
		proxy_pass http://127.0.0.1:3386;
	}
---

2.project service

Run the http service of the test project
docker run -d -p 3386:3386 --name containerName xx/node:tagName
curl 127.0.0.1:3386# test

Guess you like

Origin blog.csdn.net/jw2268136570/article/details/108218613