[root@docker ~]# systemctl start docker
[root@docker ~]# docker pull mysql
一、线上考试系统 虚拟化技术部署
1、部署前端服务器
project_exam_system.sql数据库文件
dist网络资源
1.将资源上传到服务器
C:\Users\89765>scp -r "D:\青岛实训\project_exam_system\dist" root@192.168.8.196:~/
2.创建基础容器 在服务器上
[root@docker ~]# systemctl start docker
[root@docker ~]# docker pull centos
[root@docker ~]# docker run -it --name c2 centos:latest /bin/bash
3.在容器中修改yum源
[root@a87ca8be2791 /]# rm -rf /etc/yum.repos.d/*
[root@a87ca8be2791 /]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@a87ca8be2791 /]# yum clean all
[root@a87ca8be2791 /]# yum makecache
4.在容器中安装epel
[root@a87ca8be2791 /]# yum -y install epel-release
5.在容器中安装nginx
[root@a87ca8be2791 /]# yum -y install nginx
6.CTRL+P+Q
7.导出tar包
[root@docker ~]# docker export -o centos_nginx.tar c2
8.引入tar包,称为镜像
[root@docker ~]# docker import -m "想回家" centos_nginx.tar centos:nginx
9.停用c2容器,删除c2容器
[root@docker ~]# docker stop c2
c2
[root@docker ~]# docker rm c2
c2
10.创建新的centos nginx容器,同时指定端口,以及挂载目录
[root@docker ~]# docker run -it --name c2 -p80:80/tcp -v /opt/:/usr/share/nginx/html/ centos:nginx /bin/bash
[root@e6addfb6bf6d /]# nginx
[root@b565af57ecb3 /]# ls /usr/share/nginx/html/
assets containerd favicon.ico index.html
11.在容器内部启动nginx CTRL+P+Q
[root@docker ~]# cp -r dist/* /opt/
12.在宿主机上把dist目录中的文件复制到opt目录中
[root@docker ~]# cp -r dist/* /opt/
[root@docker ~]# docker run -it --name c2 -p80:80/tcp -v /opt/:/usr/share/nginx/html/ centos:nginx nginx
[root@docker ~]# docker rm c2
c2
[root@docker ~]# docker run -it --name c2 -p80:80/tcp -v /opt/:/usr/share/nginx/html/ centos:nginx /bin/bash
[root@9eaf9ce6aa1e /]# nginx
2、数据库
[root@docker ~]# docker pull mysql
[root@docker ~]# docker run -d --name m0 -e MYSQL_ROOT_PASSWORD=root -p3306:3306 mysql:latest
[root@docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3db02812f30d mysql:latest "docker-entrypoint.s…" 26 seconds ago Up 25 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp m0
[root@docker ~]# docker exec -it m0 mysql -uroot -proot
用navicat连接宿主机,然后上传sql文件
mysql> show databases; +---------------------+ | Database | +---------------------+ | information_schema | | mysql | | performance_schema | | project_exam_system | | sys | +---------------------+ mysql> use project_exam_system; 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 | +-------------------------------+
3、让容器时间和宿主机时间同步
[root@docker ~]# docker run -it --name c3 centos:latest /bin/bash
[root@9ed7ae58448f /]# date
Mon Aug 26 06:38:07 UTC 2024
[root@9ed7ae58448f /]# [root@docker ~]#
1.让容器共享宿主的/etc/localtime
[root@docker ~]# docker run -it --name c4 -v /etc/localtime:/etc/localtime centos:latest /bin/bash
[root@98ffe8dffbeb /]# date
Mon Aug 26 14:39:54 CST 2024
4、exec命令
[root@docker ~]# docker exec c4 ls /
5、使用link docker容器IP地址不固定,如果要作为服务器使用,就必须能够直接访问服务,不能直接使用IP,为了容器设置域名,做端口映射也可以保证,但是一旦端口占用,就无法启动了
语法: docker run -link 容器名称/id:自定义域名
被link的容器必须处于启动状态,否则当前容器无法启动
[root@docker ~]# docker run -d --name m0 -e MYSQL_ROOT_PASSWORD=root mysql:latest
[root@docker ~]# docker run -it --name c0 --link m0:mysqldb centos:latest /bin/bash
[root@f6a428495b41 /]# ping mysqldbPING 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.083 ms 64 bytes from mysqldb (172.17.0.2): icmp_seq=2 ttl=64 time=0.052 ms
[root@f6a428495b41 /]# cat /etc/hosts
172.17.0.2 mysqldb d0d9d505fd15 m0 172.17.0.3 f6a428495b41
[root@docker ~]# docker stop m0
[root@docker ~]# docker run -it --name c1 centos:latest /bin/bash
[root@docker ~]# docker start m0
[root@docker ~]# docker attach c0
[root@f6a428495b41 /]# 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.091 ms
64 bytes from mysqldb (172.17.0.4): icmp_seq=2 ttl=64 time=0.053 ms
[root@f6a428495b41 /]# cat /etc/hosts
172.17.0.4 mysqldb d0d9d505fd15 m0
172.17.0.3 f6a428495b41[root@docker ~]# docker stop c0 c1 m0
c0
c1
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
[root@docker ~]#
二、docker镜像容器
1. docker client向dcoker daemon 发送创建容器的请求docker run
2. docker daemon查找有无客户端需要的镜像
3. 如果没有,就到容器的镜像仓库下载需要的镜像
4. 拿到容器镜像后,启动容器
1.在docker daemo初始状态(没有镜像的时候)/var/lib/docker不存在
2.当docker daemon服务器启动,会自动创建dock目录
3.在没有镜像的情况下。查看overlay2这个目录是只有两个文件,管道文件,I目录(管理镜像和容器的软连接的目录)
4.当我们pull一个镜像之后/va r/lib/docker/overlay2下就有一个新的目录,这个目录就是cenonos的基础目录,这个新的目录就是镜像,就是不可读的层
[root@docker ~]# systemctl stop docker
[root@docker ~]# systemctl stop docker
Warning: Stopping docker.service, but it can still be activated by:
docker.socket
[root@docker ~]# cd /var/lib/docker/
[root@docker docker]# ls
buildkit engine-id network plugins swarm volumes
containers image overlay2 runtimes tmp
[root@docker docker]# cd overlay2/
[root@docker overlay2]# ls
[root@docker overlay2]# cd
[root@docker ~]# rm -rf /var/lib/docker/[root@docker ~]# systemctl start docker
[root@docker ~]# ls /var/lib/docker/
buildkit engine-id network plugins swarm volumes
containers image overlay2 runtimes tmp
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@docker ~]# docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES[root@docker ~]# docker pull centos
[root@docker ~]# ls /var/lib/docker/overlay2/[root@docker ~]# docker run -it --name c0 centos:latest /bin/bash
[root@docker ~]# ls /var/lib/docker/overlay2/3b75c5c682b0f40acd84fc2ecb42c6db8f2de904610b728bafb08dff880d01bc
[root@docker ~]# docker exec c0 dd if=/dev/zero of=/zhangsanlisi.txt bs=10M count=100[root@docker ~]# ls /var/lib/docker/overlay2/3b75c5c682b0f40acd84fc2ecb42c6db8f2de904610b728bafb08dff880d01bc/merged/
[root@docker ~]# tar --numeric-owner --exclude=/proc --exclude=/sys -cvf lxe.tar /
[root@docker ~]# rm -rf lxe.tar