线上考试系统的数据 虚拟化技术部署
1.部署前段服务器
docker run -it --name c0 -p80:80/tcp centos:latest
/bin/bash
docker run -it --name c1 -p80 centos:latest
/bin/bash # 宿主会指定⼀个⼤于等于3278的端⼝给容器
docker ps --all # 查看实际给予的端⼝
docker run -it --name c2 -p10.1.1.51::80
centos:latest /bin/bash
docker run -it --name c4 -v /opt/:/var/www/html/
centos:latest /bin/bash
# 1.将资源上传到服务器
scp -r dist/ root@192.168.71.50:~# 2创建基础容器 在服务器上
systemctl start docker.service
docker pull centos
docker run -it --name c0 centos:latest /bin/bash
# 3 在容器中修改yum源
curl -o /etc/yum.repos.d/CentOS-Base.repo
https://mirrors.aliyun.com/repo/Centos-vault-
8.5.2111.repo
yum clean all && yum makecache
# 4.在容器中安装epel
yum -y install epel-release
# 5.在容器中安装nginx
yum -y install nginx
# 6.ctrl pq
# 7.导出tar包
docker export -o centos_nginx.tar
# 8.引⼊tar包,称为镜像
docker import -m "维护世界和平" centos_nginx.tar
centos:nginx
# 9.查看镜像
docker image ls
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED
SIZE
centos nginx 9afbddb29074 10 seconds
ago 366MBmysql latest a82a8f162e18 4 weeks ago
586MB
centos latest 5d0da3dc9764 2 years ago
231MB
# 10.停⽤c0容器,删除c0容器
# 11.创建新的centos nginx容器,在创建的同时指定端⼝,以及
挂载⽬录
docker run -it --name c0 -p80:80/tcp -v
/opt/:/usr/share/nginx/html centos:nginx /bin/bash
# 12在容器内部启动nginx服务
nginx
#ctrl p q
# 13 在宿主上把dist⽬录中的⽂件复制opt⽬录中
cp -r dist/* /opt
1.启动docker守护进程
[root@docker001 ~]# systemctl start docker.service2.搜索mysql镜像
[root@docker001 ~]# docker search mysql
NAME DESCRIPTION
STARS OFFICIAL
mysql MySQL is a widely used, open
source relation… 15322 [OK]
bitnami/mysql Bitnami container image for
MySQL 114
google/mysql MySQL server for Google
Compute Engine 25
elestio/mysql Mysql, verified and packaged
by Elestio 0
rapidfort/mysql RapidFort optimized, hardened
image for MySQL 25
circleci/mysql MySQL is a widely used, open
source relation… 30
ubuntu/mysql MySQL open source fast,
stable, multi-thread… 64
javanile/mysql MySQL for development
0
eclipse/mysql Mysql 5.7, curl, rsync
1
ilios/mysql Mysql configured for running
Ilios 1
cimg/mysql
3bitnamicharts/mysql
0
mirantis/mysql
0
docksal/mysql MySQL service images for
Docksal - https://d… 0
linuxserver/mysql A Mysql container, brought to
you by LinuxSe… 41
datajoint/mysql MySQL image pre-configured to
work smoothly … 2
ddev/mysql ARM64 base images for ddev
dbserver-mysql-8.… 1
phpmyadmin phpMyAdmin - A web interface
for MySQL and M… 1017 [OK]
vulhub/mysql
0
drud/mysql
1
nasqueron/mysql
1
corpusops/mysql
https://github.com/corpusops/docker-images/ 0
jumpserver/mysql
1
openeuler/mysql
0encoflife/mysql
0
3.拉取mysql镜像
[root@docker001 ~]# 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:86cdfe832c81e39a89cfb63c3fde1683c41cc00ef91e
67653c9c1df0ba80f454
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
[root@docker001 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED
SIZEcentos nginx 12fea1b890de 13 hours
ago 366MB
centos java17 b6bf774de7bb 14 hours
ago 550MB
centos_httpd v0 fffc96d791d5 39 hours
ago 309MB
centos_yum v0 191d9c84f461 39 hours
ago 260MB
mysql latest a82a8f162e18 4 weeks ago
586MB
centos latest 5d0da3dc9764 2 years ago
231MB
4.创建镜像容器
[root@docker001 ~]# docker run -d --name my01 -e MYSQL_ROOT_PASSWORD=root -p 3306:3306 mysql:latest
9338d672d3d819e5eb35fcf9fb5cd8fe88daf5a84b00ab0a4b3
3fc9b9ba87d8a
# -d 后台运⾏,并输出hashcode
# -e MYSQL_ROOT_PASSWORD=root 设置mysql密码
# -p 映射3306端⼝
- 管理mysql
[root@docker001 ~]# docker exec -it my01 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 9
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.
让容器的时间和宿主机一致
让容器共享宿主机的/etc/localtime
[root@docker0 ~]# docker run -it --name c0 centos:latest /bin/bash
[root@2115eaa4dc02 /]# date
Mon Aug 26 06:37:37 UTC 2024
Ctrl p q
[root@docker0 ~]# docker run -it --name c2 -v /etc/localtime:/etc/localtime centos:latest /bin/bash
[root@7aca1ba22cec /]# date
Mon Aug 26 14:40:04 CST 2024
容器的ip地址不稳定link
--link 容器名:别名
1.docker容器的ip地址是不固定的,如果要作为服务器使用,就必须能够直接访问服务,不能直接使用ip,为容器设置域名,做端口映射也能保证,确定是一旦停用之后端口被占用,就无法再次启动了
2.docker run --link 容器的名称或者id:自定义域名
3.被link容器必须处于启动状态,否则当前容器无法启动
[root@docker0 ~]# docker run -d --name m0 -e MYSQL_ROOT_PASSWORD=root mysql:latest
1c889f1939417936af1f147c390b539476447eb1bcf04ce8a676438bcca873fd
[root@docker0 ~]# docker inspect m0
"IPAddress": "172.17.0.2",
[root@docker0 ~]# docker run -it --name c0 --link m0:mysqldb
centos:latest /bin/bash
[root@4bf632f7c6fe /]# 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.184 ms
[root@4bf632f7c6fe /]# 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 1c889f193941 m0
172.17.0.3 4bf632f7c6fe
[root@docker0 ~]# docker stop m0
m0
[root@docker0 ~]# docker run -it --name c1 centos:latest /bin/bash
[root@7fc0f9ada135 /]# [root@docker0 ~]#
[root@docker0 ~]# docker inspect c1
"IPAddress": "172.17.0.2",
[root@docker0 ~]# docker start m0
m0
[root@docker0 ~]# docker inspect m0
"IPAddress": "172.17.0.4",
[root@docker0 ~]# docker attach c0
[root@4bf632f7c6fe /]# 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.141 ms
每次修改都修改了/etc//hosts文件
[root@4bf632f7c6fe /]# 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 1c889f193941 m0
172.17.0.3 4bf632f7c6fe
- 在docker daemo初始状态(没有镜像的时候) /var/lib/docker不存在
- 当docker daemon服务器启动,会自动创建docker目录
- 在没有镜像的情况下,查看overlay2这个目录只有两个文件,管道文件,目录(管理镜像和容器的软连接目录)
- 当我们pull一个镜像后/var/lib/docker/overlay2下就有一个新目录,这个目录就是centos的基础目录,这个新目录就是镜像,就是不可读的层
- 当我们使用这个镜像创建一个容器,在overlay2会添加2个目录,一个是init目录,另外一个目录就是镜像的可写层
[root@docker0 ~]# systemctl stop docker.service
[root@docker0 ~]# rm -rf /var/lib/docker/
[root@docker0 ~]# systemctl start docker
[root@docker0 ~]# ls /var/lib/docker
buildkit engine-id network plugins swarm volumes
containers image overlay2 runtimes tmp
[root@docker0 ~]# ls /var/lib/docker/overlay2/
backingFsBlockDev l
[root@docker0 ~]# 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 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 2 years ago 231MB
[root@docker0 ~]# ls /var/lib/docker/overlay2/
02cd71150a75d00200a702a8790862a723a3bec9ec1e8027c2eb320906a9e6c6
backingFsBlockDev
l
[root@docker0 ~]# ls /var/lib/docker/overlay2/02cd71150a75d00200a702a8790862a723a3bec9ec1e8027c2eb320906a9e6c6
diff link
[root@docker0 ~]# ls /var/lib/docker/overlay2/02cd71150a75d00200a702a8790862a723a3bec9ec1e8027c2eb320906a9e6c6/diff/
bin home lost+found opt run sys var
dev lib media proc sbin tmp
etc lib64 mnt root srv usr
[root@docker0 ~]# docker run -it --name c0 centos:latest /bin/bash
[root@14a5325b2169 /]# [root@docker0 ~]#
[root@docker0 ~]# ls /var/lib/docker/overlay2/
02cd71150a75d00200a702a8790862a723a3bec9ec1e8027c2eb320906a9e6c6
aeffabaf23c400af84a96fa3c037258d5e22c59c371804119f346d2a5392d296
aeffabaf23c400af84a96fa3c037258d5e22c59c371804119f346d2a5392d296-init
backingFsBlockDev
l
[root@docker0 ~]# ls /var/lib/docker/overlay2/aeffabaf23c400af84a96fa3c037258d5e22c59c371804119f346d2a5392d296
diff link lower merged work
[root@docker0 ~]# docker exec c0 dd if=/dev/zero of=/zhangsanlisi.txt bs=10M count=100
100+0 records in
100+0 records out
1048576000 bytes (1.0 GB, 1000 MiB) copied, 2.60088 s, 403 MB/s
[root@docker0 ~]# ls /var/lib/docker/overlay2/aeffabaf23c400af84a96fa3c037258d5e22c59c371804119f346d2a5392d296/merged/
bin home lost+found opt run sys var
dev lib media proc sbin tmp zhangsanlisi.txt
etc lib64 mnt root srv usr
[root@localhost ~]# tar --numeric-owner --exclude=/proc --exclude=/sys -cvf tjj.tar /
[root@localhost ~]# scp tjj.tar root@192.168.2.13:~
[root@docker0 ~]#docker import -m “我要去上天“ tjj.tar jj:v0