佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > docker部署crownblog項(xiàng)目到阿里云的方法步驟

docker部署crownblog項(xiàng)目到阿里云的方法步驟

熱門(mén)標(biāo)簽:網(wǎng)站建設(shè) 美圖手機(jī) 智能手機(jī) 使用U盤(pán)裝系統(tǒng) 百度競(jìng)價(jià)點(diǎn)擊價(jià)格的計(jì)算公式 阿里云 硅谷的囚徒呼叫中心 檢查注冊(cè)表項(xiàng)

前端項(xiàng)目打包

  • 找到.env.production 修改為自己的ip或者域名地址
  • 執(zhí)行命令npm run build生成dist文件
  • 把dist文件拷貝到后端項(xiàng)目目錄下(使用go自帶的http服務(wù)來(lái)部署前端項(xiàng)目)

后端項(xiàng)目部署

一、服務(wù)器的配置

  • 購(gòu)買(mǎi)阿里云服務(wù)器
  • 打開(kāi)服務(wù)器的8085和3306端口
  • 使用Xshell登陸服務(wù)器

二、安裝docker

官方文檔: docs.docker.com/get-docker/

選擇對(duì)應(yīng)的系統(tǒng)進(jìn)行查看,以u(píng)buntu 18.04 LTS為例

卸載舊版本

sudo apt-get remove docker docker-engine docker.io containerd runc

Reading package lists... Done
Building dependency tree    
Reading state information... Done
Package 'docker-engine' is not installed, so not removed
Package 'docker' is not installed, so not removed
Package 'containerd' is not installed, so not removed
Package 'docker.io' is not installed, so not removed
Package 'runc' is not installed, so not removed
0 upgraded, 0 newly installed, 0 to remove and 3 not upgraded.

添加新版本倉(cāng)庫(kù)

sudo apt-get update

udo apt-get install \

  apt-transport-https \

  ca-certificates \

  curl \

  gnupg-agent \

  software-properties-common

獲取官方GPG key

# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

驗(yàn)證key,如果輸出的是下列內(nèi)容,則說(shuō)明正確

# apt-key fingerprint 0EBFCD88

pub  rsa4096 2017-02-22 [SCEA]
   9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88
uid      [ unknown] Docker Release (CE deb) <docker@docker.com>
sub  rsa4096 2017-02-22 [S]

添加倉(cāng)庫(kù)地址(用國(guó)內(nèi)的倉(cāng)庫(kù)下載,速度較快)

$ sudo add-apt-repository \

  "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/ \

  $(lsb_release -cs) \

  stable"

更新倉(cāng)庫(kù)和安裝

 $ sudo apt-get update

 $ sudo apt-get install docker-ce docker-ce-cli containerd.io

進(jìn)行驗(yàn)證,運(yùn)行hello-world

$ docker pull hello-world
$ docker run hello-world
#出現(xiàn)以下信息,表示docker安裝成功,已經(jīng)可以正常運(yùn)行
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:

  1. The Docker client contacted the Docker daemon.
  2. The Docker daemon pulled the "hello-world" image from the Docker Hub.(amd64)
  3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading.
  4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.
 To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash
 Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/
 For more examples and ideas, visit:
 https://docs.docker.com/get-started/

使用阿里鏡像站來(lái)加速

地址:mirrors.aliyun.com/

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://XXX你的id.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

三、拉取鏡像和創(chuàng)建鏡像和容器編排

Mysql服務(wù)器的鏡像

首先,個(gè)人非常不建議mysql用docker來(lái)部署,有幾個(gè)原因:

  • 必須做數(shù)據(jù)卷的映射,千萬(wàn)不能 將數(shù)據(jù)庫(kù)數(shù)據(jù)放在docker容器中運(yùn)行,否則一但刪除容器數(shù)據(jù)將全部清空,所以一定要做數(shù)據(jù)持久化?。?;
  • 不利于io,數(shù)據(jù)讀寫(xiě)在容器中讀寫(xiě)一次,在綁定的卷中還要讀寫(xiě)一次,兩倍讀寫(xiě)壓力,性能上要打折扣。

如果非要在docker上部署mysql,可以這么做

#首先確定mysql是否能被搜素到,這步可以跳過(guò),也可以在dockerhub.com中搜索
docker search mysql

#拉取鏡像
docker pull mysql  #這里默認(rèn)是拉取的最新版本,如果需要特定版本可以在鏡像后面添加tag,具體版本信息可以在dockerhub.com查詢(xún)

#特定版本拉取,比如要拉取8.0.22(版本號(hào)一定要是官方放出的版本號(hào),否則是查找不到的)
docker pull mysql:8.0.22

#這時(shí)可以查看下拉取的鏡像
docker images

#運(yùn)行鏡像
docker run -d -p 3306:3306 -v /crownBlog/datadir:/var/lib/mysql --name crownBlog-mysql -e MYSQL_ROOT_PASSWORD=123456  mysql

# -d 表示后臺(tái)運(yùn)行,并返回容器id
# -p 3006:3306 表示端口映射,具體為 -p 主機(jī)端口:容器端口
# --name 給容器取個(gè)名字
# -e MYSQL_ROOT_PASSWORD=password 給mysql root管理員設(shè)置密碼
# -v /crownBlog/datadir:/var/lib/mysql 添加數(shù)據(jù)卷
/crownBlog/datadir是主機(jī)的數(shù)據(jù)庫(kù)路徑
/var/lib/mysql是容器中的數(shù)據(jù)庫(kù)路徑,這一步非常重要

#進(jìn)入容器配置
docker exec -it crownBlog-mysql bash

root@ed9345077e02:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.22 MySQL Community Server - GPL
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

# 之后就和一般情況下mysql的操作一樣了。

四、創(chuàng)建數(shù)據(jù)庫(kù)并導(dǎo)入數(shù)據(jù)文件

  • 使用Xftp連接到服務(wù)器
  • 把本地的sql文件上傳到服務(wù)器
  • 使用docker cp命令把sql文件復(fù)制到容器
docker cp crownBlog.sql crownBlog-mysql:/home 
(docker cp 第一個(gè)參數(shù)指定本地文件或者文件夾,第二個(gè)參數(shù)指定容器及容器內(nèi)的目標(biāo)文件夾)

登入容器并登錄mysql: docker exec -it crownBlog-mysql mysql -uroot -p123456

執(zhí)行sql文件 :source /home/crownBlog.sql

五、制作crownblog項(xiàng)目鏡像

使用Xftp把后端代碼上傳到服務(wù)器
進(jìn)入代碼編寫(xiě)Dockerfile文件

FROM golang:latest
RUN go env -w GO111MODULE=on
RUN go env -w GOPROXY=https://goproxy.cn,https://goproxy.io,direct

WORKDIR $GOPATH/src/crownBlog
COPY . $GOPATH/src/crownBlog

RUN go build .

EXPOSE 8085

ENTRYPOINT ["./blog"]   

配置crownblog的config文件
mod改為release

srv改為服務(wù)器ip 數(shù)據(jù)庫(kù)host改為剛才映射的數(shù)據(jù)庫(kù)ip

六、生成鏡像

在Dockerfile這個(gè)目錄下

$ docker build -t crownblog .
$ docker run -d -p 8085:8085--name crownblog crownblog

 
#這樣訪(fǎng)問(wèn)服務(wù)器IP:8085就可以訪(fǎng)問(wèn)網(wǎng)站了

到此這篇關(guān)于docker部署crownblog項(xiàng)目到阿里云的方法步驟的文章就介紹到這了,更多相關(guān)docker部署crownblog到阿里云內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

標(biāo)簽:湘潭 湖北 懷化 通遼 黃山 煙臺(tái) 山南 賀州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《docker部署crownblog項(xiàng)目到阿里云的方法步驟》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話(huà)咨詢(xún)

    • 400-1100-266
    蕲春县| 兰溪市| 铜山县| 监利县| 长岛县| 盖州市| 揭西县| 东平县| 南阳市| 宁强县| 兴安县| 红安县| 商洛市| 合肥市| 武义县| 田林县| 宾川县| 吴桥县| 北海市| 璧山县| 奈曼旗| 康马县| 龙岩市| 合水县| 安阳市| 汕头市| 霍城县| 丽水市| 抚远县| 乳源| 娄烦县| 泗水县| 宜春市| 突泉县| 旬邑县| 东安县| 集贤县| 淳安县| 黄骅市| 武定县| 福泉市|