Centos部署应用步骤
配置代码运行环境
nginx下载及配置
1 | #centos下载apache |
pgsql下载及配置
1 | #centos下载pgsql |
node.js安装
1 | #安装node.js |
部署应用
应用代码传输
在window系统中下载xftp并安装,官网地址如下:家庭/学校免费 - NetSarang Website (xshell.com),安装好后打开,点击新建,在主机一栏填上虚拟机的ip地址:192.168.60.129,用户名填root,密码填虚拟机的密码,其他默认,点击连接如下所示:
左侧窗口为本地目录,右侧窗口为虚拟机目录。选中左侧文件后右键,点击传输便可将文件从本地的windows系统传输到Linux虚拟机中。
应用前端部署
1 | #切换到前端代码目录 |
后端环境部署
1 | #编译安装python3 |
docker容器部署应用
这一部分先手动启动Pgsql、Python、Nginx等容器,然后再使用Docker Compose来编排容器。
Docker安装
1 | #安装并启动docker |
构建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
6Vue.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 | #查看占用80端口的进程 |