一、回顾
1.使用harbao仓库
1. Python -- version
2. yum -y update
3. yum -y install python2-pip
4. pip install -- upgrade pip == 20.3 -i https://mirrors.aliyun.com/pypi/simple
5. pip install docker-compose -i https://mirrors.aliyun.com/pypi/simple
6. source docker.sh
7.解压harbar
8. vim harbor.yml
1. hostname
2. https注释
9. ./prepare
10. ./installed
1. 使用nginx发布web服务
2. 使用registry管理
2.docker-compose的编排
1. 在学习kubernetes之前的热身
2.通过运行一个yum管理docker容器
3. docker-compose.yml
vim docker-compose version: "2" service: nginx: contanner_name: name0 images: nginx:latest ports: - "80:80" volumns: - /opt/web/:/usr/share/nginx/html/
二、项目:使用基础的docker指令来创建镜像,实现项目的发布
[root@docker ~]# vim /etc/docker/daemon.json
{ "registry-mirrors": [ "https://do.nark.eu.org", "https://dc.j8.work", "https://docker.m.daocloud.io", "https://dockerproxy.com", "https://docker.mirrors.ustc.edu.cn", "https://docker.nju.edu.cn" ] }
[root@docker ~]# systemctl start docker
拉取nginx
[root@docker ~]# docker pull nginx
三、docker部署project-exam-system系统
1、背景:在一台主机之内实现容器的编排,发布考试系统
2、环境准备:
1. docker
vim /etc/docker/daemon.json
{ "registry-mirrors": [ "https://do.nark.eu.org", "https://dc.j8.work", "https://docker.m.daocloud.io", "https://dockerproxy.com", "https://docker.mirrors.ustc.edu.cn", "https://docker.nju.edu.cn" ] }
2. docker-compose
3、普通的部署
1. 镜像
前端:nginx latest服务
拉取nginx:latest镜像
测试
上传前端项目dist
后端的业务: spring boot不能单独部署tomcat,已经在spring boot中内置tomcat
1. 准备拉取基础centos镜像
2. jdk17版本的文件
3. 项目资源.war .. perpreties
4. 将在服务器上的Java的资源文件上传到容器
5. 对已经上传到容器的文件进行部署 docker attach 57
6. tar -xzvf jdk-17_linux-x64_bin.tar.gz -C /usr/local/jdk/ --strip-components=17. 尝试启动
数据持久化业务: MySQL5.7.44
前端
(1)创建nginx容器
[root@docker ~]# docker run -itd -p80:80 nginx:latest
[root@docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 56c77a78a1c1 nginx:latest "/docker-entrypoint.…" 40 seconds ago Up 39 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp admiring_banach
[root@docker ~]# docker exec -it 56 ls /usr/share/nginx/html
50x.html index.html
(2)创建一个目录,不单独放置前端的资料,把后端的资料也都放到这里
[root@docker ~]# mkdir project_exam_system
[root@docker ~]# cd project_exam_system/
[root@docker project_exam_system]# mkdir web
(3)上传文件
C:\Users\89765>scp -r "D:\青岛实训\project_exam_system\dist" root@192.168.8.196:~/project_exam_system/web/
[root@docker project_exam_system]# ls
web
[root@docker project_exam_system]# ls web/
dist
[root@docker project_exam_system]# ls web/dist/
assets favicon.ico index.html
(4)删除之前创建的nginx容器
[root@docker project_exam_system]# docker stop 56
56
[root@docker project_exam_system]# docker rm 56
56
(5)再创建容器,不止映射端口,还挂载目录
[root@docker project_exam_system]# docker run -itd -p80:80 -v /root/project_exam_system/web/dist/:/usr/share/nginx/html/ nginx:latest
881bf9bd662746918396fc79254e0fde7fd382ab6c14973304f6069bea644619
[root@docker project_exam_system]# docker exec -it 88 ls /usr/share/nginx/html
assets favicon.ico index.html
(6)测试成功
出现问题,在实现登录的时候,都实现后段异常,原因后段还没后部署
登录的时候请求的域名bu.yuanyu.zhangmin,我们是hosts域名劫持来实现
vi /etc/hosts 192.168.8.196 bu.yuanyu.zhangmin sudo echo "192.168.8.196 bu.yuanyu.zhangmin" >> /etc/hosts
后端
(1)上传包
(2)创建容器
[root@docker ~]# docker run -it centos:latest /bin/bash
[root@573bb9975f03 /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@573bb9975f03 /]# [root@docker ~]#
(3)将在服务器上的Java的资源文件上传到容器
[root@docker ~]# docker cp application.properties 57:/
Successfully copied 4.1kB to 57:/
[root@docker ~]# docker cp Project_ExamSystem-V1.0.0.war 57:/
Successfully copied 41MB to 57:/
[root@docker ~]# docker cp jdk-17_linux-x64_bin.tar.gz 57:/
Successfully copied 183MB to 57:/
(4)进入容器
[root@docker ~]# docker attach 57
[root@573bb9975f03 /]# ls Project_ExamSystem-V1.0.0.war lib64 sbin application.properties lost+found srv bin media sys dev mnt tmp etc opt usr home proc var jdk-17_linux-x64_bin.tar.gz root lib run
(5)创建jdk解压目录
[root@573bb9975f03 /]# mkdir /usr/local/jdk
(6)解压jdk包
[root@573bb9975f03 /]# tar -xzvf jdk-17_linux-x64_bin.tar.gz -C /usr/local/jdk/ --strip-components=1
(7)修改application.properties 文件
[root@573bb9975f03 /]# vi application.properties
spring.datasource.url=jdbc:mysql://192.168.8.196:3306/project_exam_system #本机IP地址 spring.datasource.username=lxe spring.datasource.password=lxe spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
(8)尝试启动
[root@573bb9975f03 /]# /usr/local/jdk/bin/java -jar Project_ExamSystem-V1.0.0.war
CTRL+P+Q
[root@docker ~]# curl 172.17.0.3:8080
{"code":20002,"msg":"账号不存在或密码错误"}[root@docker ~]#
(9)根据容器57创建镜像
[root@docker ~]# docker commit 57 java:v0
[root@docker ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE java v0 b27ef2926644 6 seconds ago 774MB
(10)删除掉上面的57容器
[root@docker ~]# docker stop 57
57
[root@docker ~]# docker rm 57
57
(11)根据新建的镜像创建镜像
[root@docker ~]# docker run -itd -p8080:8080 java:v0 /usr/local/jdk/bin/java -jar Project_ExamSystem-V1.0.0.war
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7f42f4f5381a java:v0 "/usr/local/jdk/bin/…" 6 seconds ago Up 5 seconds 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp happy_hodgkin
[root@docker ~]# docker stop 7f
7f
[root@docker ~]# docker rm 7f
7f
(12)重新创建容器
[root@docker ~]# docker run -itd -p8080:8080 java:v0
2cd9c025556983e6710ff6cd508a94d6df9217ba05f79c673bab1beadc35e5f2
[root@docker ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2cd9c0255569 java:v0 "/bin/bash" 12 seconds ago Up 12 seconds 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp beautiful_haslett 881bf9bd6627 nginx:latest "/docker-entrypoint.…" About an hour ago Up About an hour 0.0.0.0:80->80/tcp, :::80->80/tcp goofy_agnesi
[root@docker ~]# docker attach 2c //将java容器的终端附加到当前终端
[root@2cd9c0255569 /]# /usr/local/jdk/bin/java -jar Project_ExamSystem-V1.0.0.war //启动spring boot微服务
MySQL
(1)拉取MySQL镜像
[root@docker ~]# docker pull mysql:5.7.44
[root@docker ~]# docker images
mysql 5.7.44 5107333e08a8 8 months ago 501MB
(2)创建容器并映射端口
[root@docker ~]# docker run -itd -e MYSQL_ROOT_PASSWORD=root -p3306:3306 mysql:5.7.44
[root@docker ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 408f1e6cec5b mysql:5.7.44 "docker-entrypoint.s…" 4 seconds ago Up 4 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp pedantic_solomon
(3)向容器中上传文件
[root@docker ~]# docker cp 408:/var/lib/mysql project_exam_system/
Successfully copied 220MB to /root/project_exam_system/
(4)将mysql文件上传
C:\Users\89765>scp "D:\青岛实训\project_exam_system\project_exam_system.sql" root@192.168.8.196:~/project_exam_system
[root@docker ~]# cd project_exam_system/
[root@docker project_exam_system]# ls
mysql project_exam_system.sql web
[root@docker project_exam_system]# rm -rf mysql/
[root@docker project_exam_system]# mkdir mysql
[root@docker project_exam_system]# mv project_exam_system.sql mysql/
[root@docker project_exam_system]# head mysql/project_exam_system.sqlcreate database if not exists project_exam_system charset utf8mb4; use project_exam_system; /* Navicat MySQL Data Transfer Source Server : MySQL84 Source Server Type : MySQL Source Server Version : 80400 Source Host : localhost:3308 Source Schema : project_exam_system
(5)将sql文件上传到mysql容器中
[root@docker ~]# docker cp project_exam_system/mysql/project_exam_system.sql 40:/
Successfully copied 11MB to 40:/
[root@docker ~]# docker exec 40 ls
project_exam_system.sql
(6)使用exec指令访问数据库管理界面
#由于在java的配置文件中,访问数据库的账号和密码不是root,而是lxe
[root@docker ~]# docker exec -it 40 mysql -uroot -prootmysql> show databases; +---------------------+ | Database | +---------------------+ | information_schema | | mysql | | performance_schema | | project_exam_system | | sys | +---------------------+ 5 rows in set (0.00 sec) mysql> create user 'lxe'@'%' identified by 'lxe'; Query OK, 0 rows affected (0.00 sec) mysql> grant all on *.* to 'lxe'@'%'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
4、Dockerfile
创建java资源存放目录,并将所有资源文件移动到java目录中
[root@docker ~]# mkdir project_exam_system/java
[root@docker ~]# mv jdk-17_linux-x64_bin.tar.gz project_exam_system/java/
[root@docker ~]# mv application.properties project_exam_system/java/[root@docker ~]# mv Project_ExamSystem-V1.0.0.war project_exam_system/java/
[root@docker ~]# tree -L 3 project_exam_system/
project_exam_system/ ├── java │ ├── application.properties │ └── jdk-17_linux-x64_bin.tar.gz ├── mysql │ └── project_exam_system.sql └── web └── dist ├── assets ├── favicon.ico └── index.html 5 directories, 5 files
编写Dockerfile文件
[root@docker ~]# vim project_exam_system/java/DockerfileFROM centos:latest ADD application.properties /application.properties ADD jdk/ /usr/local/jdk ADD Project_ExamSystem-V1.0.0.war /Project_ExamSystem-V1.0.0.war ADD java.sh /java.sh RUN chmod -v +x /java.sh WORKDIR / EXPOSE 8080 CMD [ "java.sh","/bin/bash" ]
进入到java资源目录中并解压jdk,并改名
[root@docker ~]# cd project_exam_system/java/
[root@docker java]# tar -xzvf jdk-17_linux-x64_bin.tar.gz[root@docker java]# mv jdk-17.0.12/ jdk
使用当前目录下的Dockerfile来构建一个名为java:v1的镜像。
[root@docker java]# docker build -t java:v1 .
[root@docker java]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE java v1 541d15b51e02 58 seconds ago 591MB
[root@docker java]# vim java.sh
#!/bib/bash /usr/local/jdk/bin/java -jar Project_ExamSystem-v1.0.0.war
根据java:v1镜像创建容器
[root@docker java]# docker run -it -p8081:8080 java:v1 /bin/bash
[root@1ef5404323e8 /]# lsProject_ExamSystem-V1.0.0.war home media run usr application.properties java.sh mnt sbin var bin lib opt srv dev lib64 proc sys etc lost+found root tmp
[root@1ef5404323e8 /]# /usr/local/jdk/bin/java -jar Project_ExamSystem-V1.0.0.war
[root@docker java]# docker exec 1ef ss -anut| grep 8080tcp LISTEN 0 128 *:8080 *:*