唐抉的个人博客

Docker应用部署实战

字数统计: 3.5k阅读时长: 18 min
2022/12/15

Centos部署应用步骤

配置代码运行环境

nginx下载及配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#centos下载apache
[root@localhost admin]# yum install httpd

#centos下载nginx
[root@localhost admin]# yum install epel-release
[root@localhost httpd]# yum install -y update
[root@localhost httpd]# yum install -y nginx

#打开防火墙端口80和443
[root@localhost httpd]# firewall-cmd --permanent --zone=public --add-service=http
[root@localhost httpd]# firewall-cmd --permanent --zone=public --add-service=https
[root@localhost httpd]# firewall-cmd --reload

#启动nignx服务
[root@localhost httpd]# systemctl start nginx

#查看nignx进程的状态
[root@localhost admin]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since 日 2022-12-11 18:32:09 PST; 1min 10s ago
Process: 5678 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 5675 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 5670 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 5680 (nginx)
Tasks: 2
CGroup: /system.slice/nginx.service
├─5680 nginx: master process /usr/sbin/nginx
└─5683 nginx: worker process

12月 11 18:32:09 localhost.localdomain systemd[1]: Starting The nginx HTTP a...
12月 11 18:32:09 localhost.localdomain nginx[5675]: nginx: the configuration...
12月 11 18:32:09 localhost.localdomain nginx[5675]: nginx: configuration fil...
12月 11 18:32:09 localhost.localdomain systemd[1]: Started The nginx HTTP an...
Hint: Some lines were ellipsized, use -l to show in full.

#设置nignx服务开机自启动
[root@localhost httpd]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

#进入conf.d目录新建文件并填入以下内容
[root@localhost httpd]# cd /etc/nginx/conf.d
[root@localhost conf.d]# ls
[root@localhost conf.d]# touch flask_test.conf
[root@localhost conf.d]# vi flask_test.conf
server {
listen 80;
server_name localhost;

# api代理转发
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://127.0.0.1:8000;
}
}

#重启nginx
[root@localhost conf.d]# systemctl reload nginx

pgsql下载及配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#centos下载pgsql
[root@localhost httpd]# yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
[root@localhost httpd]# yum install postgresql12-server

#启动pgsql服务
[root@localhost httpd]# postgresql-12-setup initdb
Initializing database ... OK
[root@localhost httpd]# systemctl start postgresql-12

#设置pgsql服务开机自启动
[root@localhost httpd]# systemctl enable postgresql-12
Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-12.service to /usr/lib/systemd/system/postgresql-12.service.

#配置pgsql
[root@localhost bin]# su - postgres
-bash-4.2$ psql
psql (12.13)
Type "help" for help.
postgres=# alter user postgres with password '123456'
postgres-# \q
-bash-4.2$ exit
logout
[root@localhost bin]# find / -name postgresql.conf
find: ‘/run/user/1000/gvfs’: 权限不够
/var/lib/pgsql/12/data/postgresql.conf

#配置远程访问
[root@localhost bin]# vi /var/lib/pgsql/12/data/postgresql.conf
listen_addressses="*"
port = 5432

node.js安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#安装node.js
[root@localhost admin]# wget https://nodejs.org/dist/v16.16.0/node-v16.16.0-linux-x64.tar.xz
[root@localhost admin]# tar -xvf node-v16.16.0-linux-x64.tar.xz
[root@localhost admin]# mv node-v16.16.0-linux-x64 node.js

#查找node.js路径
[root@localhost admin]# find / -name node.js
find: ‘/run/user/1000/gvfs’: 权限不够
/home/admin/node.js
/home/admin/node.js/lib/node_modules/npm/node_modules/debug/src/node.js
/home/admin/node.js/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/node.js
/home/admin/node.js/lib/node_modules/npm/node_modules/@npmcli/fs/lib/common/node.js
/home/admin/node.js/lib/node_modules/npm/node_modules/util-deprecate/node.js
#检查node版本
[root@localhost admin]# cd /home/admin/node.js/bin
[root@localhost bin]# ./node -v
v16.16.0

#添加node.js的环境变量:
[root@localhost bin]# vi /etc/profile
#在文件末尾加上两句代码如下:
#NODE_HOME为实际中node的安装位置目录
export NODE_HOME=/home/admin/node.js
export PATH=$NODE_HOME/bin:$PATH
#按ESC+:+wq退出vim模式,输入以下命令使配置生效
[root@localhost bin]# source /etc/profile

#配置软连接将环境配置放到开机自启动的脚本中
[root@localhost bin]# ln -s /usr/local/node.js/bin/node /usr/local/bin/node
[root@localhost bin]# ln -s /usr/local/node.js/bin/npm /usr/local/bin/npm
#查看虚拟机ip
[root@localhost admin]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.60.129 netmask 255.255.255.0 broadcast 192.168.60.255
inet6 fe80::b659:fb9c:695f:5eff prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:b0:6f:2a txqueuelen 1000 (Ethernet)
RX packets 491682 bytes 725762513 (692.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 49960 bytes 3119454 (2.9 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 1411 bytes 485777 (474.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1411 bytes 485777 (474.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:42:aa:9d txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

部署应用

应用代码传输

在window系统中下载xftp并安装,官网地址如下:家庭/学校免费 - NetSarang Website (xshell.com),安装好后打开,点击新建,在主机一栏填上虚拟机的ip地址:192.168.60.129,用户名填root,密码填虚拟机的密码,其他默认,点击连接如下所示:

左侧窗口为本地目录,右侧窗口为虚拟机目录。选中左侧文件后右键,点击传输便可将文件从本地的windows系统传输到Linux虚拟机中。

应用前端部署

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#切换到前端代码目录
[root@localhost bin]# cd /root/WeChat_exercise/front-end

#下载vue及vue cli、加载项目所需的配置文件
[root@localhost front-end]# npm install vue
[root@localhost front-end]# npm install --global @vue/cli
[root@localhost front-end]# npm install yarn
[root@localhost front-end]# npm install

#运行前端项目
[root@localhost front-end]# npm run serve
DONE Compiled successfully in 51145ms 01:47:08
App running at:
- Local: http://localhost:8080
- Network: http://192.168.60.129:8080

Note that the development build is not optimized.
To create a production build, run yarn build.

后端环境部署

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#编译安装python3
[root@localhost admin]# wget https://www.Python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz
[root@localhost admin]# tar xJf Python-3.6.1.tar.xz
[root@localhost admin]# cd Python-3.6.1
[root@localhost Python-3.6.1]# ./configure --prefix=/usr/local/python3 && make && make install

#创建软链接
[root@localhost Python-3.6.1]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3
[root@localhost Python-3.6.1]# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

#更换pip源
[root@localhost ~]# mkdir ~/.pip
[root@localhost ~]# vi ~/.pip/pip.conf

[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com

#切换到后端项目目录,安装virtualenv
[root@localhost ~]# cd /root/WeChat_exercise/back-end
ot@localhost back-end]# yum install python-virtualenv

#创建虚拟环境venv并激活
[root@localhost back-end]# python3 -m venv venv
[root@localhost back-end]# . venv/bin/activate
[root@localhost back-end]# yum install postgresql-devel

#安装Flask和项目所需环境
(venv) [root@localhost back-end]# pip3 install --upgrade pip setuptools wheel
(venv) [root@localhost back-end]# pip install Flask
(venv) [root@localhost back-end]# pip install -r requirements.txt
(venv) [root@localhost back-end]# pip install psycopg2
#运行Flask项目
(venv) [root@localhost back-end]#flask run

docker容器部署应用

这一部分先手动启动Pgsql、Python、Nginx等容器,然后再使用Docker Compose来编排容器。

Docker安装

1
2
3
4
5
#安装并启动docker
[root@localhost admin]# curl -sSL https://get.daocloud.io/docker | sh
[root@localhost admin]# systemctl start docker
[root@localhost admin]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

构建PgSQL容器

  • 安装镜像

    1
    [root@localhost admin]# docker pull postgres

  • 创建配置文件夹,可以看到postgres镜像创建的容器是将数据保存在/var/lib/postgresql/data位置下的:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    [root@localhost admin]# cd /home
    [root@localhost home]# mkdir -p /home/docker
    [root@localhost home]# mkdir -p /home/docker/postgresql
    [root@localhost home]# mkdir -p /home/docker/postgresql/data
    [root@localhost home]# docker image inspect postgres
    [
    {
    "Id": "sha256:a26eb6069868e4bfd0095788e541bb40711861bdfb2a8252103dea85cc0758aa",
    "RepoTags": [
    "postgres:latest"
    ],
    "RepoDigests": [
    "postgres@sha256:f4cd32e7a418d9c9ba043e7d561243388202b654c740bcc85ca40b41d9fb4f1e"
    ],
    "Parent": "",
    "Comment": "",
    "Created": "2022-12-22T23:19:59.856808957Z",
    "Container": "938c729969fbf6312f6563590395ed0bb4cbc982b039598c38ffa539c86a5df5",
    "ContainerConfig": {
    "Hostname": "938c729969fb",
    "Domainname": "",
    "User": "",
    "AttachStdin": false,
    "AttachStdout": false,
    "AttachStderr": false,
    "ExposedPorts": {
    "5432/tcp": {}
    },
    "Tty": false,
    "OpenStdin": false,
    "StdinOnce": false,
    "Env": [
    "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/postgresql/15/bin",
    "GOSU_VERSION=1.14",
    "LANG=en_US.utf8",
    "PG_MAJOR=15",
    "PG_VERSION=15.1-1.pgdg110+1",
    "PGDATA=/var/lib/postgresql/data"
    ],

  • 执行以下命令创建并启动PgSQL容器,自定义命名为my-postgres

    1
    2
    [root@localhost home]# docker run -p 5432:5432 -v /home/docker/postgresql/data:/var/lib/postgresql/data -e POSTGRES_PASSWORD=123456 -e TZ=PRC -d --name=my-postgres postgres
    165c63929bf341497816b85f28a060a2c6f75b7d9144627a560547b9ca92f9f4

  • 现在便可以用docker ps -a查看容器是否已经运行:

    1
    2
    3
    4
    [root@localhost home]# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    165c63929bf3 postgres "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:5432->5432/tcp, :::5432->5432/tcp my-postgres
    4c1699cdc854 flask-test:latest "/bin/sh -c 'gunicor…" 9 days ago Restarting (1) 45 seconds ago flask_test

构建Flask API镜像

  • 由于有多条命令,不方便直接写到Dockerfile中的CMD子命令中,因此在后端项目目录下新建boot.sh

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    #切换到后端项目目录
    [root@localhost home]# cd /root/WeChat_exercise/back-end
    [root@localhost back-end]# ls
    app config.py invoke migrations package.json __pycache__ Python.gitignore requirements.txt venv weixin.py
    [root@localhost back-end]# touch boot.sh
    [root@localhost back-end]# vi boot.sh
    #内容如下
    #!/bin/sh

    while true; do
    flask db upgrade
    if [[ "$?" == "0" ]]; then
    break
    fi
    echo Failed to apply the migration to the database, retrying in 3 secs...
    sleep 3
    done
    flask deploy
    exec gunicorn -w 3 -b 0.0.0.0:5000 --access-logfile - --error-logfile - weixin:app
    #授予可执行权限
    [root@localhost back-end]# chmod +x boot.sh

  • 新建一个Dockerfile

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    [root@localhost back-end]# touch Dockerfile
    [root@localhost back-end]# vi Dockerfile

    [root@CentOS www]# vim back-end/Dockerfile
    内容如下:

    FROM python:3.6-alpine

    COPY ./back-end /usr/src/app
    WORKDIR /usr/src/app
    RUN pip --no-cache-dir install -i https://mirrors.aliyun.com/pypi/simple/ --upgrade pip
    RUN pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/ \
    && pip install gunicorn
    ENV FLASK_APP weixin.py
    EXPOSE 5000
    ENTRYPOINT ["./boot.sh"]

  • 开始构建后端Flask API镜像

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    [root@localhost back-end]# cd ../
    [root@localhost WeChat_exercise]# docker build -f back-end/Dockerfile -t weixin-api:0.0.1 .
    Sending build context to Docker daemon 573MB.2MB
    Step 1/8 : FROM python:3.6-alpine
    ---> 3a9e80fa4606
    Step 2/8 : COPY ./back-end /usr/src/app
    ---> f5a2a7ecb4cf
    ...
    Step 6/8 : ENV FLASK_APP weixin.py
    ---> Running in e9f4ebef3d13
    Removing intermediate container e9f4ebef3d13
    ---> 7317d7be0b13
    Step 7/8 : EXPOSE 5000
    ---> Running in c544718548e6
    Removing intermediate container c544718548e6
    ---> c6e683ab97ff
    Step 8/8 : ENTRYPOINT ["./boot.sh"]
    ---> Running in c8e11997efb0
    Removing intermediate container c8e11997efb0
    ---> 64f072fbbbe5
    Successfully built 64f072fbbbe5
    Successfully tagged weixin-api:0.0.1

  • 启动Flask API容器

    1
    2
    [root@localhost WeChat_exercise]# docker run -d --name weixin-api --link my-postgres:postgres -e DATABASE_URL=postgresql://postgres:123456@localhost:5432/postgres -p 5000:5000 --rm weixin-api:0.0.1
    2b58b6f9a4984649df15eda6c7b5b849697b3fc6f9cdfd906009e52814763f19

构建Nginx容器

  • 修改WeChat_exercise/front-end/src/main.js文件,增加以下代码,其中192.168.60:131为虚拟机ip

    1
    2
    3
    4
    5
    6
    Vue.use(ElementUI);
    if (process.env.NODE_ENV==='production'){
    axios.defaults.baseURL='http://192.168.60:131:5000'
    }else {
    axios.defaults.baseURL = 'http://127.0.0.1:5000';
    }

  • 将打包后的静态文件,拷贝到 ../docker/nginx/data 目录中,后续映射到 Nginx 容器中去

    1
    2
    3
    4
    5
    6
    7
    [root@localhost home]# cd /root/WeChat_exercise/front-end
    [root@localhost front-end]# npm install
    [root@localhost front-end]# npm run build
    [root@localhost front-end]# mkdir -p ../docker/nginx/data
    [root@localhost front-end]# cp -a dist/* ../docker/nginx/data
    [root@localhost front-end]# cd ..
    [root@localhost WeChat_exercise]#

  • 启动Nginx容器

    1
    2
    [root@localhost WeChat_exercise]# docker run -d --name nginx -p 18080:80 --rm -v $PWD/docker/nginx/data:/usr/share/nginx/html nginx
    50f66f6ad75ec19220e37fe510724b70448b462a7e7c6b335f42de4893f44944

  • 浏览器访问http://192.168.60.131:18080/#/ping,前端应用能够正常访问到后端API接口:

使用Docker Compose编排容器

  • 安装docker-compose

    1
    2
    3
    4
    5
    6
    [root@localhost WeChat_exercise]# cd back-end
    [root@localhost back-end]# curl -L https://get.daocloud.io/docker/compose/releases/download/v2.4.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
    [root@localhost back-end]# chmod +x /usr/local/bin/docker-compose
    [root@localhost back-end]# ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
    [root@localhost back-end]# docker-compose version
    Docker Compose version v2.4.1

  • 创建并编写docker-compose.yaml文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    [root@localhost back-end]# touch docker-compose.yaml
    [root@localhost back-end]# vi docker-compose.yaml
    #内容如下:

    version: "3.7"
    services:
    my-postgres:
    image: postgres
    privileged: true
    # 权限要加,不然启动会告错
    environment:
    POSTGRES_PASSWORD: 123456
    POSTGRES_USER: postgres
    POSTGRES_DB: postgres
    TZ: Asia/Shanghai
    ports:
    - 5432:5432
    volumes:
    - /home/docker/postgresql/data:/var/lib/postgresql/data
    restart: always

    weixin-api:
    image: weixin-api:0.0.1
    build:
    context: .
    dockerfile: Dockerfile
    restart: always
    ports:
    - "5000:5000"
    depends_on:
    - my-postgres

    nginx:
    image: nginx
    ports:
    - "18080:80"
    volumes:
    - "$PWD/docker/nginx/data:/usr/share/nginx/html"

  • 查看正在运行的容器并使它们全部停止运行

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    #查看正在运行的容器
    [root@localhost back-end]# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    2b58b6f9a498 weixin-api:0.0.1 "./boot.sh" 47 minutes ago Up 47 minutes 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp weixin-api
    50f66f6ad75e nginx "/docker-entrypoint.…" 2 hours ago Up 2 hours 0.0.0.0:18080->80/tcp, :::18080->80/tcp nginx
    165c63929bf3 postgres "docker-entrypoint.s…" 2 hours ago Up 2 hours 0.0.0.0:5432->5432/tcp, :::5432->5432/tcp my-postgres
    4c1699cdc854 flask-test:latest "/bin/sh -c 'gunicor…" 9 days ago Restarting (1) 14 seconds ago flask_test

    #停止所有正在运行的容器
    [root@localhost back-end]# docker stop $(docker ps -a -q)
    27ddc44b2109
    a0704302049b
    d5e616940191
    2b58b6f9a498
    2d86579808e5
    bb1794163d8e
    be6acf36f0d6
    50f66f6ad75e
    165c63929bf3
    4a78c412395d
    80b4b9b47652
    4c1699cdc854
    d140e8d466d0

    #检查是否还有容器正在运行
    [root@localhost back-end]# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

  • 使用Docker Compose启动应用所需的所有容器,'-d' 选项表示在后台运行 compose,否则在前台输出日志

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    #启动所有容器
    [root@localhost back-end]# docker-compose up -d

    [+] Running 3/3
    ⠿ Container back-end-nginx-1 Started 6.0s
    ⠿ Container back-end-my-postgres-1 Started 6.1s
    ⠿ Container back-end-weixin-api-1 Started 11.0s

    #查看容器状态
    [root@localhost back-end]# docker-compose ps
    NAME COMMAND SERVICE STATUS PORTS
    back-end-my-postgres-1 "docker-entrypoint.s…" my-postgres running 0.0.0.0:5432->5432/tcp, :::5432->5432/tcp
    back-end-nginx-1 "/docker-entrypoint.…" nginx running 0.0.0.0:18080->80/tcp, :::18080->80/tcp
    back-end-weixin-api-1 "./boot.sh" weixin-api running 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp

    #查看各容器的运行日志,若含有'-f' 选项则持续输出
    [root@localhost back-end]# docker-compose logs
    #停止全部容器
    [root@localhost back-end]# docker-compose stop
    [+] Running 3/3
    ⠿ Container back-end-weixin-api-1 Stopped 16.3s
    ⠿ Container back-end-nginx-1 Stopped 4.0s
    ⠿ Container back-end-my-postgres-1 Stopped

端口被占用解决方法

在docker部署的过程中,若端口被占用,如80端口,可使用以下命令解除端口占用:

1
2
3
4
5
6
7
8
9
10
11
12
#查看占用80端口的进程
[root@localhost back-end]# netstat -lnp | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1159/nginx: master
tcp6 0 0 :::80 :::* LISTEN 1159/nginx: master
#杀死占用端口的进程
[root@localhost back-end]# kill -9 1159
#查看是否还有进程占用端口,若有,则继续杀死进程,若无,则端口占用已解除
[root@localhost back-end]# netstat -lnp | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1160/nginx: worker
tcp6 0 0 :::80 :::* LISTEN 1160/nginx: worker
[root@localhost back-end]# kill -9 1160
[root@localhost back-end]# netstat -lnp | grep 80
CATALOG
  1. 1. Centos部署应用步骤
    1. 1.1. 配置代码运行环境
      1. 1.1.1. nginx下载及配置
      2. 1.1.2. pgsql下载及配置
      3. 1.1.3. node.js安装
    2. 1.2. 部署应用
      1. 1.2.1. 应用代码传输
      2. 1.2.2. 应用前端部署
      3. 1.2.3. 后端环境部署
  2. 2. docker容器部署应用
    1. 2.1. Docker安装
    2. 2.2. 构建PgSQL容器
    3. 2.3. 构建Flask API镜像
    4. 2.4. 构建Nginx容器
    5. 2.5. 使用Docker Compose编排容器
  3. 3. 端口被占用解决方法