服务器之家:专注于服务器技术及软件下载分享
分类导航

云服务器|WEB服务器|FTP服务器|邮件服务器|虚拟主机|服务器安全|DNS服务器|服务器知识|Nginx|IIS|Tomcat|

服务器之家 - 服务器技术 - 服务器知识 - docker部署crownblog项目到阿里云的方法步骤

docker部署crownblog项目到阿里云的方法步骤

2021-07-10 21:01crown 服务器知识

这篇文章主要介绍了docker部署crownblog项目到阿里云的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

前端项目打包

 

  • 找到.env.production 修改为自己的ip或者域名地址
  • 执行命令npm run build生成dist文件
  • 把dist文件拷贝到后端项目目录下(使用go自带的http服务来部署前端项目)

docker部署crownblog项目到阿里云的方法步骤

后端项目部署

 

一、服务器的配置

  • 购买阿里云服务器
  • 打开服务器的8085和3306端口
  • 使用Xshell登陆服务器

二、安装docker

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

选择对应的系统进行查看,以ubuntu 18.04 LTS为例

卸载旧版本

?
1
2
3
4
5
6
7
8
9
10
11
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.

添加新版本仓库

?
1
2
3
4
5
6
7
8
sudo apt-get update
 
udo apt-get install \
  apt-transport-https \
  ca-certificates \
  curl \
  gnupg-agent \
  software-properties-common

获取官方GPG key

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

验证key,如果输出的是下列内容,则说明正确

?
1
2
3
4
5
6
# 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]

添加仓库地址(用国内的仓库下载,速度较快)

?
1
2
3
4
$ sudo add-apt-repository \
  "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/ \
  $(lsb_release -cs) \
  stable"

更新仓库和安装

?
1
2
3
$ sudo apt-get update
 
$ sudo apt-get install docker-ce docker-ce-cli containerd.io

进行验证,运行hello-world

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ docker pull hello-world
$ docker run hello-world
#出现以下信息,表示docker安装成功,已经可以正常运行
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/

使用阿里镜像站来加速

地址:mirrors.aliyun.com/

?
1
2
3
4
5
6
7
8
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

三、拉取镜像和创建镜像和容器编排

Mysql服务器的镜像

首先,个人非常不建议mysql用docker来部署,有几个原因:

  • 必须做数据卷的映射,千万不能 将数据库数据放在docker容器中运行,否则一但删除容器数据将全部清空,所以一定要做数据持久化!!;
  • 不利于io,数据读写在容器中读写一次,在绑定的卷中还要读写一次,两倍读写压力,性能上要打折扣。

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

?
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
#首先确定mysql是否能被搜素到,这步可以跳过,也可以在dockerhub.com中搜索
docker search mysql
 
#拉取镜像
docker pull mysql  #这里默认是拉取的最新版本,如果需要特定版本可以在镜像后面添加tag,具体版本信息可以在dockerhub.com查询
 
#特定版本拉取,比如要拉取8.0.22(版本号一定要是官方放出的版本号,否则是查找不到的)
docker pull mysql:8.0.22
 
#这时可以查看下拉取的镜像
docker images
 
#运行镜像
docker run -d -p 3306:3306 -v /crownBlog/datadir:/var/lib/mysql --name crownBlog-mysql -e MYSQL_ROOT_PASSWORD=123456  mysql
 
# -d 表示后台运行,并返回容器id
# -p 3006:3306 表示端口映射,具体为 -p 主机端口:容器端口
# --name 给容器取个名字
# -e MYSQL_ROOT_PASSWORD=password 给mysql root管理员设置密码
# -v /crownBlog/datadir:/var/lib/mysql 添加数据卷
/crownBlog/datadir是主机的数据库路径
/var/lib/mysql是容器中的数据库路径,这一步非常重要
 
#进入容器配置
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的操作一样了。

四、创建数据库并导入数据文件

  • 使用Xftp连接到服务器
  • 把本地的sql文件上传到服务器
  • 使用docker cp命令把sql文件复制到容器
?
1
2
docker cp crownBlog.sql crownBlog-mysql:/home
(docker cp 第一个参数指定本地文件或者文件夹,第二个参数指定容器及容器内的目标文件夹)

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

执行sql文件 :source /home/crownBlog.sql

五、制作crownblog项目镜像

使用Xftp把后端代码上传到服务器
进入代码编写Dockerfile文件

?
1
2
3
4
5
6
7
8
9
10
11
12
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改为服务器ip 数据库host改为刚才映射的数据库ip

六、生成镜像

在Dockerfile这个目录下

?
1
2
3
4
5
$ docker build -t crownblog .
$ docker run -d -p 8085:8085--name crownblog crownblog
 
 
#这样访问服务器IP:8085就可以访问网站了

到此这篇关于docker部署crownblog项目到阿里云的方法步骤的文章就介绍到这了,更多相关docker部署crownblog到阿里云内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://juejin.cn/post/6960260130185150500

延伸 · 阅读

精彩推荐