1、安装docker
1.1、卸载系统之前的 docker
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
1.2、安装yum utils包,并设置存储库
sudo yum install -y yum-utils
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
1.3、安装 docker,以及 docker-cli
sudo yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin
1.4、启动docker
sudo systemctl start docker
1.5、设置docker开机自动启动
sudo systemctl enable docker
1.6、配置docker阿里镜像加速器
1.6.1:
sudo mkdir -p /etc/docker
1.6.2:
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://9bt3rat6.mirror.aliyuncs.com"]
}
EOF
1.6.3:
sudo systemctl daemon-reload
1.6.4:
sudo systemctl restart docker
1.7、看到docker版本
2、安装jdk1.8 『已切换到root』
2.1、直接拉取镜像
docker pull java:8
2.2、查看镜像
docker images
3、安装msql5.7
3.1、拉取镜像
docker pull mysql:5.7
3.2、查看镜像
docker images
3.3、创建mysql容器并启动
docker run -p 3306:3306 --name mysql \
-v /mydata/mysql/log:/var/log/mysql \
-v /mydata/mysql/data:/var/lib/mysql \
-v /mydata/mysql/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7
参数说明
-p 3306:3306:将容器的 3306 端口映射到主机的 3306 端口
-v /mydata/mysql/conf:/etc/mysql:将配置文件夹挂载到主机
-v /mydata/mysql/log:/var/log/mysql:将日志文件夹挂载到主机
-v /mydata/mysql/data:/var/lib/mysql/:将配置文件夹挂载到主机
-e MYSQL_ROOT_PASSWORD=root:初始化 root 用户的密码
-d 后台运行容器,并返回容器ID
3.4、查看运行容器
docker ps
3.5、mysql配置 vi /mydata/mysql/conf/my.cnf
[client]
default_character_set=utf8
[mysql]
default-character-set=utf8
[mysqld]
collation_server = utf8_general_ci
character_set_server = utf8
skip-character-set-client-handshake
skip-name-resolve
3.6、设置mysql开机自启动
docker update mysql --restart=always
4、安装redis6
4.1、拉取镜像
docker pull redis:6
4.2、查看镜像
docker images
4.3、创建挂载目录及文件
4.3.1:
mkdir -p /mydata/redis/conf
4.3.2:
touch /mydata/redis/conf/redis.conf
4.4、创建redis容器并启动
docker run -p 6379:6379 --name redis \
-v /mydata/redis/data:/data \
-v /mydata/redis/conf/redis.conf:/etc/redis/redis.conf \
-d redis:6 redis-server /etc/redis/redis.conf
4.5、查看运行容器
docker ps
4.6、设置redis开机自启动
docker update redis --restart=always
5、安装nginx1.24.0
5.1、拉取镜像
docker pull nginx:1.24.0
5.2、查看镜像
docker images
5.3、将容器的文件复制到宿主机
# 5.3.1:随便创建一个nginx容器
docker run --name nginx -p 80:80 -d nginx:1.24.0
# 5.3.2:复制容器文件
mkdir -p /mydata/nginx/conf
docker cp nginx:/etc/nginx/nginx.conf /mydata/nginx/conf
docker cp nginx:/etc/nginx/conf.d /mydata/nginx/conf
docker cp nginx:/usr/share/nginx/html /mydata/nginx/
# 5.3.3:关闭此容器
docker stop nginx
# 5.3.4:删除此容器
docker rm nginx
# 5.3.5:删除正在运行的nginx容器
docker rm -f nginx
5.4、创建nginx容器并启动
docker run -p 80:80 --name nginx \
-v /mydata/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /mydata/nginx/conf/conf.d:/etc/nginx/conf.d \
-v /mydata/nginx/log:/var/log/nginx \
-v /mydata/nginx/html:/usr/share/nginx/html \
-d nginx:1.24.0
5.5、查看运行容器
docker ps
5.6、设置nginx开机自启动
docker update nginx --restart=always
6、创建springboot项目
6.1、项目构建
6.2、pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.7.RELEASE</version>
</parent>
<groupId>com.baihua</groupId>
<artifactId>arrange-study</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>arrange-study</name>
<description>部署学习</description>
<dependencies>
<!-- web工程 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.6.13</version>
</plugin>
</plugins>
</build>
</project>
6.2、application.yml
server:
port: 8080
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.56.103:3306/arrange?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: root
6.3、项目结构
6.3.1、controller
package com.baihua.controller;
import com.baihua.entity.UserEntity;
import com.baihua.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("user")
@CrossOrigin // 解决跨域问题
public class UserController {
@Autowired
private UserService userService;
// 根据id获取用户
@GetMapping("get/{id}")
public UserEntity getUserById(@PathVariable("id") Integer id) {
UserEntity user = userService.getById(id);
return user;
}
}
6.3.2、service
package com.baihua.service;
import com.baihua.entity.UserEntity;
import com.baomidou.mybatisplus.extension.service.IService;
public interface UserService extends IService<UserEntity> {
}
package com.baihua.service;
import com.baihua.dao.UserDao;
import com.baihua.entity.UserEntity;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService{
}
6.3.3、dao
package com.baihua.dao;
import com.baihua.entity.UserEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserDao extends BaseMapper<UserEntity> {
}
6.3.4、entity
package com.baihua.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("user")
public class UserEntity {
private Integer id;
private String name;
private Integer age;
}
6.3.5、启动类
6.4、创建user表并插入一条数据
6.5、启动项目本地测试
7、项目打为jar包
7.1、执行命令
mvn clean package
7.2、jar包位置
8、jar通过xftp包上传到服务器
8.1、上传位置 /usr/local/java (自己定义)
8.2、Dockerfile (位置跟jar包一致)
# 设置本镜像需要使用的基础镜像
FROM java:8
# 把jar包添加到镜像中
ADD arrange-study-0.0.1-SNAPSHOT.jar /arrange-study.jar
# 镜像暴露的端口
EXPOSE 8080
# 容器启动命令
ENTRYPOINT ["java","-jar","/arrange-study.jar"]
9、构建docker镜像
9.1、执行构建命令
# arrange-study-server 为镜像名称
# 1.0 为镜像版本
# 不要漏了最后的那个.
docker build -t arrange-study-server:1.0 .
9.2、查看镜像
docker images
9.3、启动容器
docker run -p 8080:8080 --name arrange-study-server \
-v /mydata/arrange-study-server/logs:/var/log/arrange-study-server \
-d arrange-study-server:1.0
9.4、查看运行容器
docker ps
9.5、测试
10、创建vue2项目
10.1、项目构建
vue create arrange-study-vue
10.2、启动vue项目
npm run serve
10.3、安装axios
npm install axios
10.4、编写User.vue组件
<template>
<div class="user">
<button @click="getUser">我是谁呢?点我呀!</button>
<button @click="userHide">我要隐藏!点我呀!</button>
<div v-if="user.id">id:{{user.id}}</div>
<div v-if="user.name">name:{{user.name}}</div>
<div v-if="user.age">age:{{user.age}}</div>
</div>
</template>
<script>
import axios from 'axios'
export default {
name: 'User',
data() {
return {
user: {}
}
},
methods: {
// 获取用户
getUser() {
axios.get(`http://localhost:8080/user/get/1`).then(res => {
console.log('res', res);
this.user = res.data;
})
},
// 隐藏
userHide() {
this.user = {};
}
}
}
</script>
<style>
</style>
10.5、在App.vue中引入User.vue
10.6、浏览器再次查看(本地测试成功)
11、vue项目打包
11.1、修改User.vue中axios请求的ip地址为服务器地址
// 获取用户
getUser() {
axios.get(`http://192.168.56.103:8080/user/get/1`).then(res => {
console.log('res', res);
this.user = res.data;
})
},
11.2、打包命令
npm run build
11.3、打包文件位置
11.4、将dist文件夹下的都传到服务器/mydata/nginx/html 下
11.5、浏览器 http://192.168.56.103 (访问成功)
12、碰到的问题
12.1、Linux可以ping通Windows,反之没ping通
环境:使用vagrant + virtualbox 构建的虚拟机
解决方案:
- 找到window中以太网适配器virtualbox的ip地址
- 将Vagrantfile文件的config.vm.network "private_network", ip: "“,改为刚查到的那个ip
12.2、文件上传到服务器时发生错误
标签:Vue,SpringBoot,mydata,Linux,nginx,conf,mysql,import,docker From: https://www.cnblogs.com/xiaonuanxin/p/17608025.html原因:使用vagrant账号登录的xftp,权限不足,需要root权限
解决方案:
1. 使用chmod -R 777 放开权限【如:chmod -R 777 /mydata 代表mydata文件夹下所有人都可以读写操作】