软件资源:
链接: https://pan.baidu.com/s/1iYMP7p7v_HUMc9vakIQfbQ?pwd=6fes 提取码: 6fes
docker-compose容器
部署nmt---nginx+compose+tomcat项目
使用基础的docker指令来创建镜像,实现项目的发布。
Dockerfile--->docker-compose
背景:在一台主机内实现容器的编排,发布考试系统
前端部署
1.安装docker
[root@docker0 ~]# sh docker.sh
2.编辑daemon.json文件---设置仓库镜像
vim /etc/docker/daemon.json
{
"registry-mirrors":[
"https://do.nark.eu.org",
"https://dc.j8.work",
"GitHub - DaoCloud/public-image-mirror: 很多镜像都在国外。比如 gcr 。国内下载很慢,需要加速。致力于提供连接全世界的稳定可靠安全的容器镜像服务。",
"https://dockerproxy.com",
"https://docker.mirrors.ustc.edu.cn",
"https://docker.nju.edu.cn"
]
}
3.开启docker服务
[root@docker0 ~]# systemctl daemon-reload
[root@docker0 ~]# systemctl start docker
4.拉取nginx镜像
[root@docker0 ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
e4fff0779e6d: Pull complete
2a0cb278fd9f: Pull complete
7045d6c32ae2: Pull complete
03de31afb035: Pull complete
0f17be8dcff2: Pull complete
14b7e5e8f394: Pull complete
23fa5a7b99a6: Pull complete
Digest: sha256:447a8665cc1dab95b1ca778e162215839ccbb9189104c79d7ec3a81e14577add
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
查看镜像
[root@docker0 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 5ef79149e0ec 2 weeks ago 188MB
5.创建nginx容器
[root@docker0 ~]# docker run -itd -p80:80 nginx:latest
c9fa7a0cbb13a3dd627b94c28f76ce1802fa391652832ad875782c8d073e6ff9
[root@docker0 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c9fa7a0cbb13 nginx:latest "/docker-entrypoint.…" 17 seconds ago Up 15 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp affectionate_vaughan
[root@docker0 ~]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
在容器启动之后,nginx服务酒店发布了
6.
[root@docker0 ~]# docker exec -it c9fa ls /usr/share/nginx/html
50x.html index.htm
7.创建一个目录,将前端和后端的所以资料放在这里
[root@docker0 ~]# mkdir project_exam_system/
[root@docker0 ~]# cd project_exam_system/
[root@docker0 project_exam_system]# mkdir web
[root@docker0 project_exam_system]# rz -E
rz waiting to receive.
[root@docker0 project_exam_system]# ls web/
dist
[root@docker0 project_exam_system]# ls web/dist/
assets favicon.ico index.html
8.创建容器,映射端口,挂载目录
删除容器c9fa
[root@docker0 project_exam_system]# docker stop c9fa
c9fa
[root@docker0 project_exam_system]# docker rm c9fa
c9fa
创建新容器
[root@docker0 project_exam_system]# docker run -itd -p80:80 -v /root/project_exam_system/web/dist/:/usr/share/nginx/html/ nginx:latest
b46e530213e011ca30c84549aabc42db21cef193ef8d426ed1c2a6f5fe818d44
9.刷新浏览器
后端部署
1.拉取centos镜像
[root@docker0 project_exam_system]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
[root@docker0 project_exam_system]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 5ef79149e0ec 2 weeks ago 188MB
centos latest 5d0da3dc9764 2 years ago 231MB
2.创建容器启动镜像
[root@docker0 project_exam_system]# docker run -it centos:latest /bin/bash
[root@73e60487e331 /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@73e60487e331 /]# [root@docker0 project_exam_system]#
3.jdk17的版本、项目资源war
[root@docker0 ~]# ls
anaconda-ks.cfg jdk-17_linux-x64_bin.tar.gz
application.properties project_exam_system
docker.sh Project_ExamSystem-V1.0.0.war
initserver.sh
4.将外部资源复制到容器内
[root@docker0 ~]# docker cp --help
Usage: docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
Copy files/folders between a container and the local filesystem
Use '-' as the source to read a tar archive from stdin
and extract it to a directory destination in a container.
Use '-' as the destination to stream a tar archive of a
container source to stdout.
Aliases:
docker container cp, docker cp
Options:
-a, --archive Archive mode (copy all uid/gid information)
-L, --follow-link Always follow symbol link in SRC_PATH
-q, --quiet Suppress progress output during copy.
Progress output is automatically suppressed
if no terminal is attached
[root@docker0 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
73e60487e331 centos:latest "/bin/bash" 2 minutes ago Up 2 minutes elastic_robinson
b46e530213e0 nginx:latest "/docker-entrypoint.…" 33 minutes ago Up 33 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp sleepy_vaughan
[root@docker0 ~]# docker cp jdk-17_linux-x64_bin.tar.gz 73e6:/
Successfully copied 183MB to 73e6:/
[root@docker0 ~]# docker cp application.properties 73e6:/
Successfully copied 4.1kB to 73e6:/
[root@docker0 ~]# docker cp Project_ExamSystem-V1.0.0.war 73e6:/
Successfully copied 41MB to 73e6:/
5.连接centos镜像的容器
对已传到容器的文件进行部署
[root@docker0 ~]# docker attach 73e6
[root@73e60487e331 /]# ls
Project_ExamSystem-V1.0.0.war jdk-17_linux-x64_bin.tar.gz opt sys
application.properties lib proc tmp
bin lib64 root usr
dev lost+found run var
etc media sbin
home mnt srv
6.创建解压目录
[root@73e60487e331 /]# mkdir /usr/local/jdk
7.解压jdk17到指定目录
[root@73e60487e331 /]# tar -zxvf jdk-17_linux-x64_bin.tar.gz -C /usr/local/jdk/ --strip-components=1
[root@73e60487e331 /]# ls /usr/local/jdk/
LICENSE README bin conf include jmods legal lib man release
8.修改application.properties
设置了java程序连接数据库的操作
[root@73e60487e331 /]# vi application.properties
9.启动java
[root@73e60487e331 /]# /usr/local/jdk/bin/java -jar Project_ExamSystem-V1.0.0.war
10.访问服务
Ctrl+p+q退出访问,出现以下情况表示正常启动,但是没有给数据和账号。
[root@docker0 ~]# curl 172.17.0.3:8080
{"code":20002,"msg":"账号不存在或密码错误"}[root@docker0 ~]#
[root@docker0 ~]# docker commit b885 java:v0
sha256:c4a7ac6df09c0e5ed30399b145f53a8dcbda6cc32690a84c06527f6e619e6885
[root@docker0 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
java v0 c4a7ac6df09c 22 seconds ago 774MB
nginx latest 5ef79149e0ec 2 weeks ago 188MB
centos latest 5d0da3dc9764 2 years ago 231MB
[root@docker0 ~]# docker commit b885 java:v0
sha256:c4a7ac6df09c0e5ed30399b145f53a8dcbda6cc32690a84c06527f6e619e6885
[root@docker0 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
java v0 c4a7ac6df09c 22 seconds ago 774MB
nginx latest 5ef79149e0ec 2 weeks ago 188MB
centos latest 5d0da3dc9764 2 years ago 231MB
[root@docker0 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b8854febcad4 centos:latest "/bin/bash" 8 minutes ago Up 8 minutes inspiring_kare
b46e530213e0 nginx:latest "/docker-entrypoint.…" About an hour ago Up About an hour 0.0.0.0:80->80/tcp, :::80->80/tcp sleepy_vaughan
[root@docker0 ~]# docker stop b885
b885
[root@docker0 ~]# docker rm b885
b885
[root@docker0 ~]# docker run -itd -p8080:8080 java:v0 /usr/local/jdk/bin/java -jar Project_ExamSystem-V1.0.0.war
7daf96b493fec05e23262a04003b70ea818ef6abfbdaa753e1bce911b4ad9b73
数据持久化
1.拉取MySQL镜像
[root@docker0 ~]# docker pull mysql:5.7.44
5.7.44: Pulling from library/mysql
20e4dcae4c69: Pull complete
1c56c3d4ce74: Pull complete
e9f03a1c24ce: Pull complete
68c3898c2015: Pull complete
6b95a940e7b6: Pull complete
90986bb8de6e: Pull complete
ae71319cb779: Pull complete
ffc89e9dfd88: Pull complete
43d05e938198: Pull complete
064b2d298fba: Pull complete
df9a4d85569b: Pull complete
Digest: sha256:4bc6bc963e6d8443453676cae56536f4b8156d78bae03c0145cbe47c2aad73bb
Status: Downloaded newer image for mysql:5.7.44
docker.io/library/mysql:5.7.44
[root@docker0 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
java v0 c4a7ac6df09c 3 hours ago 774MB
nginx latest 5ef79149e0ec 2 weeks ago 188MB
mysql 5.7.44 5107333e08a8 8 months ago 501MB
centos latest 5d0da3dc9764 2 years ago 231MB
2.创建MySQL容器
[root@docker0 ~]# docker run -itd -e MYSQL_ROOT_PASSWORD=root -p 3306:3306 mysql:5.7.44
a63c59858426f275d608225ce8a297f559e2fca3ff76c2a061eec616db672d13
[root@docker0 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a63c59858426 mysql:5.7.44 "docker-entrypoint.s…" 5 seconds ago Up 4 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp hopeful_bohr
d85664513d17 java:v0 "/usr/local/jdk/bin/…" 9 minutes ago Up 7 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp sweet_herschel
3.传mysql
[root@docker0 ~]# docker cp a63c:/var/lib/mysql project_exam_system/
Successfully copied 220MB to /root/project_exam_system/
4.mysql里面写数据
将sql文件上传到MySQL容器中
[root@docker0 project_exam_system]# mkdir mysql
[root@docker0 project_exam_system]# ls
mysql web
[root@docker0 project_exam_system]# ls mysql/
project_exam_system.sql
[root@docker0 project_exam_system]# head -10 mysql/project_exam_system.sql
create 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
[root@docker0 project_exam_system]# cd
[root@docker0 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a63c59858426 mysql:5.7.44 "docker-entrypoint.s…" 9 minutes ago Up 9 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp hopeful_bohr
d85664513d17 java:v0 "/usr/local/jdk/bin/…" 18 minutes ago Up 17 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp sweet_herschel
[root@docker0 ~]# docker cp project_exam_system/mysql/project_exam_system.sql a63c:/
Successfully copied 11MB to a63c:/
5.登录MySQL
使用外部数据库连接容器的MySQL,然后运行sql文件
[root@docker0 ~]# docker exec -it a63c mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.44 MySQL Community Server (GPL)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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> show databases;
+---------------------+
| Database |
+---------------------+
| information_schema |
| mysql |
| performance_schema |
| project_exam_system |
| sys |
+---------------------+
5 rows in set (0.00 sec)
mysql>
6.测试
java的Dockerfile内容:
FROM centos:latest
ADD application.properties /application.properties
ADD Project_ExamSystem-V1.0.0.war /Project_ExamSystem-V1.0.0.war
WORKDIR /
EXPOSE 8080
RUN mkdir /usr/local/jdk
CMD [ "/usr/local/jdk/bin/java -jar Project_ExamSystem-V1.0.0.war" ]
nginx的Dockerfile内容:
FROM nginx:latest
ADD dist project_exam_system/web/
WORKDIR project_exam_system/web
EXPOSE 80
WORKDIR project_exam_system/web/
RUN mkdir web
CMD ["nginx","-g","daemon off;"]
mysql的Dockerfile内容:
FROM mysql:5.7.44
WORKDIR /project_exam_system/mysql
ENV MYSQL_PASSWORD=root
EXPOSE 3306
WORKDIR project_exam_system/
RUN mkdir mysql
ADD projet_exam_system.sql project_exam_system/mysql/
CMD [ "exec -it mysql -uroot -proot" ]