Docker帮助文档
1 HTTP与服务器进行交互方法
常见的有四种:GET、POST、PUT、DELETE。其中,GET和POST最常用。
**GET:**用来获取资源,它只是获取、查询数据,不会修改服务器的数据,从这点来讲,它是安全的(后面还会从另一方面分析它的不安全性)。由于它是读取的,因此可以对GET请求的数据进行缓存。
**POST:**则是可以向服务器发送修改请求,进行数据的修改的。举个例子:比如说我们要在知乎、或者论坛下面评论,这个时候就需要用到POST请求。但是它不能缓存,为什么呢?设想如果我们将“评论成功”的页面缓存在本地,那么当我发送一个请求的时候,直接返回本地的“评论成功”页面,而服务器端则什么也没有做,根本没有进行评论的更新,岂不是难以想象。
2 获取docker的镜像以及server的地址
@GetMapping("/container")
public String toAddPage(Model model){
Collection<String> images = dockerUtils.getImages();
model.addAttribute("Images",images);
List<String> servers = hostMapper.queryServerIp();
model.addAttribute("servers",servers);
return "container/add";
}
3 增加容器 controller
public String addEmp(ContainerCreate containerCreate,Model model){
HttpSession session = httpUtil.getSession();
containerCreate.setUsername((String) session.getAttribute("loginUser"));
containerCreate.setStatus("Exit");//新建时容器处于关闭状态
dockerUtils.createContainer(containerCreate);
containerCreateMapper.addContainer(containerCreate);
return "redirect:/containers";
}
4 通过容器名称开启、关闭、删除容器
@GetMapping("/start/{serverIp}/{name}")
public String startContainerByName(@PathVariable("serverIp") String serverIp,@PathVariable("name") String name){
int status = dockerUtils.startContainer(serverIp, name);
if(status==204 || status==304){
ContainerCreate tempContainer = new ContainerCreate();
tempContainer.setStatus("UP");
tempContainer.setName(name);
containerCreateMapper.setStatus(tempContainer);
}
return "redirect:/containers";
}
@GetMapping("/stop/{serverIp}/{name}")
public String stopContainerByName(@PathVariable("serverIp") String serverIp,@PathVariable("name") String name){
int status = dockerUtils.stopContainer(serverIp,name);
if(status==204 || status==304){
ContainerCreate tempContainer = new ContainerCreate();
tempContainer.setStatus("Exit");
tempContainer.setName(name);
containerCreateMapper.setStatus(tempContainer);
}
return "redirect:/containers";
}
@GetMapping("/delete/{serverIp}/{name}")
public String deleteContainerByName(@PathVariable("serverIp") String serverIp,@PathVariable("name") String name){
int status = dockerUtils.deleteContainer(serverIp,name);
if(status==204){
ContainerCreate tempContainer = new ContainerCreate();
tempContainer.setName(name);
containerCreateMapper.deleteContainer(tempContainer);
}
return "redirect:/containers";
}
5 判断重复
5.1 name
@RequestMapping("/valName")
public void valName(String name, HttpServletResponse response) throws IOException {
boolean flag = true;
List<String> res = containerCreateMapper.queryContainerNameAll();
for (String tempName: res) {
if(name.equals(tempName)){
flag=false;
break;
}
}
if(!flag){
response.setCharacterEncoding("UTF-8");
response.getWriter().print(name+" 已经存在了!!");
}else {
response.getWriter().print("");
}
}
5.2 IP
@RequestMapping("/valIp")
public void valNIp(String ip, HttpServletResponse response) throws IOException {
boolean flag = true;
List<String> res = containerCreateMapper.queryContainerIpAll();
for (String tempIP: res) {
if(ip.equals(tempIP)){
flag=false;
break;
}
}
if(!flag){
response.setCharacterEncoding("UTF-8");
response.getWriter().print(ip+" 已经存在了!!");
}else {
response.getWriter().print("");
}
}
6 获取容器状态
@PostMapping( "/getStat")
public void getStat(String serverIp,String name,HttpServletResponse response) throws IOException {
Status status = dockerUtils.getStatus(serverIp, name);
response.setCharacterEncoding("UTF-8");
response.getWriter().println(status);
}
7 登录 controller
public class LoginController {
@Autowired
private UserMapper userMapper;
@RequestMapping("/user/login")
public String login(
@RequestParam("username") String username,
@RequestParam("password" ) String password,
Model model, HttpSession session){
if("admin".equals(username)&&"123456".equals(password)){
session.setAttribute("loginUser",username);
return "redirect:/main.html";
}else {
if(username.equals(userMapper.queryUsername(username))){
//验证登录
if(userMapper.queryPassword(username).equals(password)){
session.setAttribute("loginUser",username);
return "redirect:/containers";
}else {
model.addAttribute("msg","用户名或者密码错误!");
return "index";
}
}else {
//如果不存在用户名,直接注册
userMapper.addUser(new User(username=username,password=password));
session.setAttribute("loginUser",username);
return "redirect:/containers";
}
}
}
@RequestMapping("/user/logout")
public String userLogout(HttpSession session){
session.invalidate();
return "redirect:/index.html";
}
}