部署一考试项目
# 1、将资源上传到服务器
[root@docker ~]# yum -y install unzip
[root@docker ~]# rz -E
rz waiting to receive.
[root@docker ~]# unzip project_exam_system.zip
# 2、创建基础镜像
[root@docker ~]# systemctl start docker
[root@docker ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Image is up to date for centos:latest
docker.io/library/centos:latest
# 3、在容器中修改yum源
[root@docker ~]# docker run -it --name a0 centos:latest /bin/bash
[root@d1d92bed0d9a /]# rm -rf /etc/yum.repos.d/*
[root@d1d92bed0d9a /]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2495 100 2495 0 0 5898 0 --:--:-- --:--:-- --:--:-- 5898
[root@d1d92bed0d9a /]# yum clean all && yum makecache
# 4、在容器中安装epel
[root@d1d92bed0d9a /]# yum -y install epel-release
# 5、在容器中安装nginx
[root@d1d92bed0d9a /]# yum -y install nginx
# 6、ctrl p q退出容器
[root@d1d92bed0d9a /]# [root@docker ~]#
# 7、导出tar包
[root@docker ~]# docker export -o centos_nginx.tar a0
[root@docker ~]# ls
anaconda-ks.cfg centos.tar project_exam_system.sql yum_httpd.tar
application.properties centos_yum.tar Project_ExamSystem-V1.0.0.war
centos_nginx.tar dist project_exam_system.zip
# 8、引入tar包,成为镜像
[root@docker ~]# docker import -m "nginx" centos_nginx.tar centos:nginx
sha256:a0a4655eb590b16aace6c33d78862992ccdf615f1d517d236d173ff501086ff8
# 9、查看镜像
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos nginx a0a4655eb590 18 seconds ago 366MB
centos httpd 92dfb2054854 3 days ago 310MB
centos yum d79f0f2ff79e 3 days ago 260MB
mysql latest a82a8f162e18 4 weeks ago 586MB
centos latest 5d0da3dc9764 2 years ago 231MB
# 10、删除容器
[root@docker ~]# docker rm -f a0
a0
# 11、创建新的容器,在创建的同时指定端口,以及挂载目录
[root@docker ~]# docker run -it --name a0 -p80:80/tcp -v /opt/:/usr/share/nginx/html centos:nginx /bin/bash
# 12、在容器内部启动nginx服务
[root@8dea218de2f7 /]# nginx
# 13、curl p q退出镜像
[root@8dea218de2f7 /]# [root@docker ~]#
[root@docker ~]# ls /opt
containerd
# 14、在宿主机上把dist目录中的文件复制到opt目录下
[root@docker ~]# cp -r dist/* /opt/
[root@docker ~]# ls /opt/
assets containerd favicon.ico index.html
[root@docker ~]# curl localhost
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="icon" href="/favicon.ico">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vite App</title>
<script type="module" crossorigin src="/assets/index-C4kAShR5.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-CSz7ARPP.css">
</head>
<body>
<div id="app"></div>
</body></html>
mysql镜像管理
# 拉取mysql镜像
[root@docker ~]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
6e839ac3722d: Pull complete
ad912193ad5f: Pull complete
25d13d87fd8d: Pull complete
004d383c75ef: Pull complete
6d9bbc82a0b8: Pull complete
81fec07ea550: Pull complete
83357cb2d3a5: Pull complete
8ffe968b82c1: Pull complete
30dfd9a7ed57: Pull complete
35844ae33cbe: Pull complete
Digest: sha256:86cdfe832c81e39a89cfb63c3fde1683c41cc00ef91e67653c9c1df0ba80f454
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
# 查看镜像
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos httpd 92dfb2054854 3 days ago 310MB
centos yum d79f0f2ff79e 3 days ago 260MB
mysql latest a82a8f162e18 4 weeks ago 586MB
centos latest 5d0da3dc9764 2 years ago 231MB
# 使用mysql镜像创建一个名为mysql01的容器,密码为root,用户为root,端口为3306
[root@docker ~]# docker run -d --name mysql01 -e MYSQL_ROOT_PASSWORD=root -p 3306:3306 mysql:latest
87136ab0b9a66e8d11be350cc11848db7bec4f914e45b889f77ff00aaa5b50fb
# -d 后台运行
# -e MYSQL_ROOT_PASSWRD=root 设置mysql密码
# -p 映射3306端口
# 查看up状态的容器
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
87136ab0b9a6 mysql:latest "docker-entrypoint.s…" 3 hours ago Up 3 hours 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql01
[root@docker ~]# docker exec -it mysql01 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: 9.0.1 MySQL Community Server - GPL
Copyright (c) 2000, 2024, 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> exit
Bye
查看数据库和表
mysql> show databases;
+---------------------+
| Database |
+---------------------+
| information_schema |
| mysql |
| performance_schema |
| project_exam_system |
| sys |
+---------------------+
5 rows in set (0.00 sec)
mysql> use project_exam_system
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+-------------------------------+
| Tables_in_project_exam_system |
+-------------------------------+
| admin |
| category |
| com_city |
| com_nation |
| course |
| dept |
| dept_student |
| exam |
| exam_dept |
| exam_process |
| loginlog |
| major |
| menu |
| paper |
| paper_topic |
| role |
| role_admin |
| role_menu |
| semester |
| student |
| teacher |
| topic |
| user |
+-------------------------------+
23 rows in set (0.00 sec)
mysql> select * from user;
+-----+--------------+----------+--------+---------------------+----------+---------------------+--------+-------+
| id | userName | password | status | createTime | createOr | editTime | editOr | isDel |
+-----+--------------+----------+--------+---------------------+----------+---------------------+--------+-------+
| 1 | 21001189 | 111111 | 0 | 2024-08-21 09:45:05 | NULL | 2024-08-22 22:04:44 | NULL | 0 |
| 2 | 21001190 | 111111 | 0 | 2024-08-21 09:45:05 | NULL | 2024-08-21 09:45:05 | NULL | 0 |
| 3 | 21001191 | 111111 | 0 | 2024-08-21 09:45:05 | NULL | 2024-08-21 09:45:05 | NULL | 0 |
| 4 | 21001192 | 111111 | 0 | 2024-08-21 09:45:05 | NULL | 2024-08-21 09:45:05 | NULL | 0 |
| 5 | 21001193 | 111111 | 0 | 2024-08-21 09:45:05 | NULL | 2024-08-21 09:45:05 | NULL | 0 |
| 6 | 21001194 | 111111 | 0 | 2024-08-21 09:45:05 | NULL | 2024-08-21 09:45:05 | NULL | 0 |
| 7 | 21001195 | 111111 | 0 | 2024-08-21 09:45:05 | NULL | 2024-08-21 09:45:05 | NULL | 0 |
| 8 | 21001196 | 111111 | 0 | 2024-08-21 09:45:05 | NULL
让容器和宿主机时间一致
[root@docker ~]# docker run -it --name c8 -v /etc/localtime:/etc/localtime centos:latest /bin/bash
[root@08f5525f8d55 /]# date
Mon Aug 26 14:39:46 CST 202
exec语句
# 创建名称为c8的容器
[root@docker ~]# docker run -it --name c8 -v /etc/localtime:/etc/localtime centos:latest /bin/bash
# 退出容器不中断-ctrl+p+q
[root@08f5525f8d55 /]# [root@docker ~]#
# 使用exec命令执行ls命令
[root@docker ~]# docker exec c8 ls /
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
# 使用exec命令执行yum clean all && yum makecache这条命令
[root@docker ~]# docker exec c8 yum clean all && yum makecache
0 files removed
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink | 5.1 kB 00:00:00
* base: mirrors.aliyun.com
* epel: repo.jing.rocks
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
a | 3.6 kB 00:00:00
base | 3.6 kB 00:00:00
docker-ce-stable | 3.5 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/13): a/other_db | 2.5 MB 00:00:00
(2/13): a/filelists_db | 6.9 MB 00:00:01
(3/13): docker-ce-stable/7/x86_64/filelists_db | 66 kB 00:00:00
(4/13): docker-ce-stable/7/x86_64/other_db | 145 kB 00:00:01
(5/13): epel/x86_64/prestodelta | 592 B 00:00:01
(6/13): extras/7/x86_64/filelists_db | 305 kB 00:00:01
(7/13): extras/7/x86_64/other_db | 154 kB 00:00:01
(8/13): base/7/x86_64/other_db | 2.6 MB 00:00:08
(9/13): updates/7/x86_64/other_db | 1.6 MB 00:00:09
(10/13): base/7/x86_64/filelists_db | 7.2 MB 00:00:34
(11/13): updates/7/x86_64/filelists_db | 15 MB 00:00:59
epel/x86_64/filelists_db FAILED 07 ETA
(12/13): epel/x86_64/other_db | 4.1 MB 00:08:03
(13/13): epel/x86_64/filelists_db | 15 MB 00:05:49
元数据缓存已建立
容器之间的依赖
# 查看容器
[root@docker ~]# docker ps -all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 创建名为m0容器
[root@docker ~]# docker run -d --name m0 -e MYSQL_ROOT_PASSWORD=root mysql:latest
e6390afa961b4a3d8a3b3ff1e204073c0af4c8fed96198441d58f775e982c1e1
# 查看m0容器的ip
[root@docker ~]# docker inspect m0
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"DriverOpts": null,
"DNSNames": null
# 使用link模块为m0容器创建名为mysqldb依赖的容器
[root@docker ~]# docker run -it --name c0 --link m0:mysqldb centos:latest /bin/bash
# 在这个容器中pingmysqldb,会发现ping到的地址是m0的容器
[root@6c8098650212 /]# ping mysqldb
PING mysqldb (172.17.0.2) 56(84) bytes of data.
64 bytes from mysqldb (172.17.0.2): icmp_seq=1 ttl=64 time=0.063 ms
64 bytes from mysqldb (172.17.0.2): icmp_seq=2 ttl=64 time=0.081 ms
^C
--- mysqldb ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 0.063/0.072/0.081/0.009 ms
# 在容器中查看etc/hosts文件 就看到mysqldb对应的地址是m0的地址
[root@6c8098650212 /]# cat etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 mysqldb e6390afa961b m0
172.17.0.3 6c8098650212
# 退出容器不中断
[root@6c8098650212 /]# [root@docker ~]#
# 查看所有容器
[root@docker ~]# docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6c8098650212 centos:latest "/bin/bash" 3 minutes ago Up 3 minutes c0
e6390afa961b mysql:latest "docker-entrypoint.s…" 5 minutes ago Up 5 minutes 3306/tcp, 33060/tcp m0
# 停止容器m0的运行,172.17.0.2这个ip地址就被让出来了
[root@docker ~]# docker stop m0
m0
# 重新创建c1容器
[root@docker ~]# docker run -it --name c1 centos:latest /bin/bash
# 退出不中断
[root@337a08b8a20a /]# [root@docker ~]#
# 查看容器c1的ip地址,就会看到c1占用了172.17.0.2这个地址
[root@docker ~]# docker inspect c1
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"DriverOpts": null,
"DNSNames": null
# 启动m0容器
[root@docker ~]# docker start m0
m0
# 查看m0的容器的地址,就看发现它的地址变成了172.17.0.4
[root@docker ~]# docker inspect m0
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.4",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"DriverOpts": null,
"DNSNames": null
# 挂载c0容器的终端
[root@docker ~]# docker attach c0
# 继续pingmysqldb,发现地址变成了172.17.0.4(m0现在的地址)
[root@6c8098650212 /]# ping mysqldb
PING mysqldb (172.17.0.4) 56(84) bytes of data.
64 bytes from mysqldb (172.17.0.4): icmp_seq=1 ttl=64 time=0.111 ms
64 bytes from mysqldb (172.17.0.4): icmp_seq=2 ttl=64 time=0.052 ms
64 bytes from mysqldb (172.17.0.4): icmp_seq=3 ttl=64 time=0.101 ms
^C
--- mysqldb ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.052/0.088/0.111/0.025 ms
# 在c0容器中查看/etc/hosts文件,发现里面的地址也变成了172.17.0.4
[root@6c8098650212 /]# cat etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.4 mysqldb e6390afa961b m0
172.17.0.3 6c8098650212
# 退出不中断c0容器
[root@6c8098650212 /]# read escape sequence
# 查看所有容器
[root@docker ~]# docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
337a08b8a20a centos:latest "/bin/bash" 5 minutes ago Up 5 minutes c1
6c8098650212 centos:latest "/bin/bash" 9 minutes ago Up 9 minutes c0
e6390afa961b mysql:latest "docker-entrypoint.s…" 11 minutes ago Up 3 minutes 3306/tcp, 33060/tcp m0
[root@docker ~]# docker stop c0
c0
[root@docker ~]# docker stop c1
c1
[root@docker ~]# docker stop m0
m0
# 因为c0依赖m0,所以先启动被依赖的
[root@docker ~]# docker start c0
Error response from daemon: Cannot link to a non running container: /m0 AS /c0/mysqldb
Error: failed to start containers: c0
# 先启动被依赖的容器
[root@docker ~]# docker start m0
m0
# 再启动主动依赖的容器
[root@docker ~]# docker start c0
c0
容器域名
容器的ip不稳定
加link模块
--link 容器名:别名
小结:docker容器的ip地址不固定,如果要作为服务器使用,就必须能够直接访问服务,不能直接使用ip,为容器设置域名,做端口映射也能保证,确定是一旦停用之后端口被占用,就无法再次启动了
2.doker run --link ,容器的名称或者是id:自定义域名
3.被link容器必须处于启动状态,否则当前容器无法启
docker文件
1.在docker daemon初始状态(没有状态的时候)/var/lib/docker不存在
2.当docker daemon服务器自启动创建docker目录
3.在没有镜像的情况下,查看overlay2这个目录是只有两个文件,管道文件,目录(管理镜像和容器的软链接的目录)
4.当我们pull一个镜像之后 /var/lib/docker/overlay2 下就有一个新的目录,这个目录就是镜像的基础目录,这个新的目录就是镜像,就是不可读的层
基础镜像制作
进程目录
# ls /proc