[root@localhost ~]# ls //导入dist项目
anaconda-ks.cfg centos_httpd.tar centos_nginx centos.tar centos_yum.tar dist
[root@localhost ~]# docker pull mysql //下载mysql
[root@localhost ~]# docker images //查看镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
centos httpd 55b1539d5cd6 2 days ago 309MB
centos yum 9d43e5c9d7e4 2 days ago 260MB
mysql latest a82a8f162e18 4 weeks ago 586MB
centos latest 5d0da3dc9764 2 years ago 231MB
[root@localhost ~]# docker run -it --name c0 centos:yum /bin/bash 创建并启动容器
[root@0719e2c449f5 /]# yum -y install nginx
ctrl+p+q退出
[root@localhost ~]# docker export -o centos_nginx c0 //导出为tar
[root@localhost ~]# docker import -m "维护世界" centos_nginx centos:nginx //导入为镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos nginx 7ecd5a5a376f 15 seconds ago 366MB
centos yum 589cbc1074de 4 minutes ago 366MB
centos httpd 55b1539d5cd6 2 days ago 309MB
mysql latest a82a8f162e18 4 weeks ago 586MB
centos latest 5d0da3dc9764 2 years ago 231MB
[root@localhost ~]# docker ps -all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0719e2c449f5 9d43e5c9d7e4 "/bin/bash" 23 minutes ago Up 23 minutes c0
[root@localhost ~]# docker stop c0
c0
[root@localhost ~]# docker rm c0
c0
[root@localhost ~]# docker run -it --name c0 -p80:80/tcp -v /opt/:/usr/share/nginx/html/ centos:nginx /bin/bash //创建,指定端口,挂载并启动c0容器
[root@f66e2e996233 /]# nginx
ctrl+p+q退出
[root@localhost ~]# cp -r dist/* /opt //把项目传到挂载目录下
浏览器访问本机IP
[root@localhost ~]# docker run -d --name m0 -e MYSQL_ROOT_PASSWORD=root -p3306:3306 mysql:latest //-d,隐藏启动 -e,设置mysql密码 -p映射端口
87d71e02c8eaa448eddc2c181622ae0affbc45cf69900735c157026837ed337c
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
87d71e02c8ea mysql:latest "docker-entrypoint.s…" 18 seconds ago Up 17 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp m0
[root@localhost ~]# docker exec -it m0 mysql -uroot -proot //登陆mysql
mysql> exit
去navicat导入sql文件
[root@localhost ~]# docker run -it --name c1 -v /etc/localtime:/etc/localtime centos:nginx /bin/bash //让容器共享宿主的/etc/localtime
[root@cd7ae2b2e21c /]# date
Mon Aug 26 15:07:18 CST 2024
ctrl+p+q退出
--link 容器名:别名
[root@localhost ~]# docker run -it --link c0:mysqldb --name c1 centos:nginx /bin/bash
[root@6c310c115194 /]# ping mysqldb //能ping通
64 bytes from mysqldb (172.17.0.2): icmp_seq=1 ttl=64 time=0.339 ms
[root@6c310c115194 /]# cat /etc/hosts
172.17.0.3 mysqldb 87d71e02c8ea c0
172.17.0.5 6c310c115194
ctrl+p+q
测试:
# 暂停服务
[root@docker001 ~]# docker stop c0 c1
c0
c1
[root@docker001 ~]# docker start c1 //先启动主动依赖容器
# ⽆法启动,提示被link的容器没有启动
Error response from daemon: Cannot link to a non
running container: /c0 AS /c1/mysqldb
Error: failed to start containers: c1
[root@docker001 ~]# docker start c0 // 启动被依赖的容器
c0
[root@docker001 ~]# docker start c1 // 再启动主动依赖的容器
c1
测试:使⽤⼀个容器占⽤172.17.0.2,测试
[root@docker001 ~]# docker stop c0 c1 # 停⽤容器
c0
c1
[root@docker001 ~]# docker run -it --name c2 centos:nginx /bin/bash # 创建新容器 占⽤172.17.0.2
[root@2114e0e97441 /]#
ctrl+p+q退出
[root@docker001 ~]# docker start c0 c1 # 启动 c0 c1
c0
c1
[root@docker001 ~]# docker exec c1 ping mysqldb # 在c1容器ping别名
PING mysqldb (172.17.0.3) 56(84) bytes of data.
64 bytes from mysqldb (172.17.0.3): icmp_seq=1 ttl=64 time=0.157 ms
64 bytes from mysqldb (172.17.0.3): icmp_seq=2 ttl=64 time=0.037 ms
^C
[root@docker001 ~]# docker exec c1 cat /etc/hosts #每次启动都修改了/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.3 mysqldb 20f05670b7df c0
172.17.0.4 ba46c0e4055d
1.docker容器的ip地址是不固定,如果要作为服务器使⽤,就必须能够直接访问服务,不能直接使⽤ip,为容器设置域名,做端⼝映射也能保证,确定是⼀旦停⽤之后端⼝被占⽤,就⽆法再次启动了
2.docker run --link. 容器的名称或者是id:⾃定义域名
3.被link容器必须处于启动状态,否则当前容器⽆法启动
1. docker client向dcoker daemon 发送创建容器的请求 docker run
2. docker daemon查找有⽆客户端需要的镜像
3. 如果没有,就到容器的镜像仓库下载需要的镜像 pull
4. 拿到容器镜像后,启动容器
1.在docker daemo初始状态(没有镜像的时候) /var/lib/docker不存在
2.当docker daemon服务器启动,会⾃动创建dock⽬录
3.在没有镜像的情况下。查看overlay2这个⽬录是只有两个⽂件,管道⽂件 ,l⽬录(管理镜像和容器的软连接的⽬录)
4.当我们pull⼀个镜像之后 /va r/lib/docker/overlay2下就有⼀个新的⽬录,这个⽬录就是cenonos的基础⽬录,这个新的⽬录就是镜像,就是不可读的层
5.当我们使⽤这个镜像创建⼀个容器,在overlay2会添加2个⽬录,⼀个是init⽬录,另外⼀个⽬录就是容器的可写层,⽤户对系统的修改都在可写层中进⾏
一台需要被做成镜像的主机
[root@localhost ~]# ls /proc
1 16393 275 295 314 334 353 489 664 842 execdomains meminfo sysrq-trigger
10 16788 276 296 315 335 383 49 667 9 fb misc sysvipc
11 16954 277 298 316 336 384 490 670 9251 filesystems modules timer_list
1143 17113 278 299 317 337 43 491 676 94 fs mounts timer_stats
1146 17150 281 3 318 34 45 492 7 9454 interrupts mpt tty
1147 18 282 300 319 340 457 493 727 acpi iomem mtrr uptime
12 19 283 301 32 341 458 5 749 asound ioports net version
1297 2 284 302 320 342 46 5418 750 buddyinfo irq pagetypeinfo vmallocinfo
13 20 285 303 322 343 468 562 751 bus kallsyms partitions vmstat
1304 21 286 304 324 344 469 5823 754 cgroups kcore sched_debug zoneinfo
14 22 287 306 325 346 47 584 757 cmdline keys schedstat
1406 23 288 307 326 347 482 597 760 consoles key-users scsi
1410 24 289 308 329 348 483 616 772 cpuinfo kmsg self
14242 25 290 309 33 349 484 63 776 crypto kpagecount slabinfo
1530 26 291 310 330 35 485 654 783 devices kpageflags softirqs
16 2683 292 311 331 350 486 656 786 diskstats loadavg stat
16092 2687 293 312 332 351 487 658 8 dma locks swaps
16369 274 294 313 333 352 488 660 800 driver mdstat sys
[root@localhost ~]# ls /sys
block bus class dev devices firmware fs hypervisor kernel module power
[root@localhost ~]# mkdir /sys/abc
mkdir: 无法创建目录"/sys/abc": 不允许的操作
[root@localhost ~]# tar --numeric-owner --exclude=/proc --exclude=/sys -cvf centos7.tar /
scp(从本机上)
[root@localhost ~]# ls
anaconda-ks.cfg centos7.tar
[root@localhost ~]# docker import -m "说明0" centos7.tar me:me00
[root@localhost ~]# docker images
[root@localhost ~]# docker run -it --name c00 me:me00 /bin/bash