centos系統(tǒng)環(huán)境搭建專欄??點擊跳轉(zhuǎn)
關(guān)于Docker-compose安裝請看CentOS系統(tǒng)環(huán)境搭建(三)——Centos7安裝Docker&Docker Compose,該文章同樣收錄于centos系統(tǒng)環(huán)境搭建專欄。
Centos7部署項目
- 采用前后端分離的形式部署。
- 使用Docker運行項目。
- 使用Docker Compose創(chuàng)建項目容器。
- 使用git管理項目的更新。
Centos7安裝git
安裝
yum install git
驗證
git --version
Centos7從github拉取代碼
配置git
配置用于提交代碼的用戶名
git config --global user.name "Your Name"
配置用戶郵箱
git config --global user.email "Your email"
生成公鑰
ssh-keygen -t rsa -C "Your email"
效果如下
ssh-keygen -t rsa -C "你的email"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:一串密碼 email
The key's randomart image is:
密碼圖
可知,Your public key has been saved in /root/.ssh/id_rsa.pub.
這里就是你密鑰的位置。
[root@VM-4-17-centos srv]# cd /root/.ssh/
[root@VM-4-17-centos .ssh]# ll
total 8
-rw------- 1 root root 1679 Jun 9 14:57 id_rsa
-rw-r--r-- 1 root root 400 Jun 9 14:57 id_rsa.pub
github添加SSH key
打開你的GitHub
獲取ssh key公鑰內(nèi)容
id_rsa.pub就是你的公鑰,參考上面步驟,進入對應(yīng)文件夾下查看公鑰。
cat /root/.ssh/id_rsa.pub
將它復制到github。
驗證
ssh -T git@github.com
成功后如下所示。
[root@VM-4-17-centos .ssh]# ssh -T git@github.com
Hi PerCheung! You've successfully authenticated, but GitHub does not provide shell access.
拉取項目
新建空白文件夾
mkdir /srv/tencent
mkdir /srv/tencent/server
我將會把github代碼拉取到/srv/tencent/server
拉取項目
復制ssh地址
進入/srv/tencent/server
執(zhí)行下面的命令
git clone git@github.com:PerCheung/tencent.git
效果如下
[root@VM-4-17-centos server]# git clone git@github.com:PerCheung/tencent.git
Cloning into 'tencent'...
remote: Enumerating objects: 352, done.
remote: Counting objects: 100% (352/352), done.
remote: Compressing objects: 100% (210/210), done.
remote: Total 352 (delta 136), reused 289 (delta 77), pack-reused 0
Receiving objects: 100% (352/352), 111.94 KiB | 0 bytes/s, done.
Resolving deltas: 100% (136/136), done.
[root@VM-4-17-centos server]# ll
total 4
drwxr-xr-x 5 root root 4096 Jun 9 15:31 tencent
使用Docker Compose創(chuàng)建項目容器
創(chuàng)建docker-compose.yaml
/srv/tencent/server下創(chuàng)建docker-compose.yaml
# 指定 Docker Compose 文件版本
version: '3'
# 定義服務(wù)
services:
# 定義名為 tencent 的服務(wù)
tencent:
# 自定義容器名
container_name: tencent_server
# 始終重啟該容器
restart: always
# 使用當前目錄下的 Dockerfile 構(gòu)建容器鏡像
build: ./tencent
# 賦予容器更高的權(quán)限,以便容器內(nèi)的進程可以擁有更高的權(quán)限
privileged: true
# 容器內(nèi)的工作目錄
working_dir: /tencent
# 將主機的 ~/.m2 目錄映射到容器內(nèi)的 /root/.m2 目錄,以便容器內(nèi)的應(yīng)用程序可以訪問主機上的 Maven 倉庫
volumes:
- ./tencent:/tencent
- ~/.m2:/root/.m2
# 將容器的網(wǎng)絡(luò)模式設(shè)置為主機模式,使得容器內(nèi)的應(yīng)用程序可以直接使用主機的網(wǎng)絡(luò)資源,提高應(yīng)用程序的網(wǎng)絡(luò)性能
network_mode: host
# 容器啟動時執(zhí)行的命令,清除 Maven 緩存并啟動 Spring Boot 應(yīng)用程序
command: mvn clean spring-boot:run
啟動項目
docker-compose up
之后將會下載大量jar包到你的本地maven倉庫。
成功后有如下信息。
這證明項目可以順利啟動,使用ctrl+C
結(jié)束,換這個命令運行我們的項目。
docker-compose up -d
可以看到項目已經(jīng)正在運行。
通過網(wǎng)頁即可訪問。
配置前端
同理,以上的步驟就是在配置后端,配置前端模仿即可。
新建空白文件夾
mkdir /srv/tencent
mkdir /srv/tencent/page
拉取前端代碼
/srv/tencent/page下執(zhí)行
git clone git@github.com:PerCheung/mytencentpage.git
創(chuàng)建nginx.conf
/srv/tencent/page下創(chuàng)建nginx.conf如下
server {
# 監(jiān)聽端口號為 8080
listen 8080;
# 服務(wù)器名為 localhost
server_name localhost;
# 設(shè)置字符集為 utf-8
charset utf-8;
# 設(shè)置根目錄為 /usr/share/nginx/files
root /usr/share/nginx/files;
}
創(chuàng)建docker-compose.yaml
/srv/tencent/page下創(chuàng)建docker-compose.yaml如下文章來源:http://www.zghlxwxcb.cn/news/detail-656271.html
version: '3'
services:
tencent_page:
# 使用 nginx:1.21.1 鏡像作為容器
image: nginx:1.21.1
# 設(shè)置容器名稱為 tencent_page
container_name: tencent_page
# 啟用特權(quán)模式,以便容器內(nèi)部可以執(zhí)行一些特殊操作
privileged: true
# 掛載本地 dist 目錄到容器內(nèi)的 /usr/share/nginx/files 目錄
volumes:
- ./mytencentpage/dist:/usr/share/nginx/files
# 掛載本地 nginx.conf 文件到容器內(nèi)的 /etc/nginx/conf.d/default.conf 文件
- ./nginx.conf:/etc/nginx/conf.d/default.conf
# 將容器內(nèi)的 8080 端口映射到宿主機的 80 端口
ports:
- 80:8080
# 啟動容器時執(zhí)行的命令,創(chuàng)建 /usr/share/nginx/files 目錄并啟動 nginx 服務(wù)
command: /bin/bash -c "mkdir -p /usr/share/nginx/files && nginx -g 'daemon off;'"
啟動服務(wù)
docker-compose up -d
查看是否在運行
文章來源地址http://www.zghlxwxcb.cn/news/detail-656271.html
到了這里,關(guān)于CentOS系統(tǒng)環(huán)境搭建(九)——centos系統(tǒng)下使用docker部署項目的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!