一、镜像
1.打包
[root@localhost ~]# systemctl start docker
[root@localhost ~]# docker save -o centos.tar centos:latest
[root@localhost ~]# ls
anaconda-ks.cfg centos.tar
2.传输
[root@localhost ~]# scp centos.tar root@192.168.1.100:/root
3.删除镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 2 years ago 231MB
[root@localhost ~]# docker start c0 //启动容器
c0
[root@localhost ~]# docker attach c0 //进入容器
[root@207ca7680ecc /]# exit
exit
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker ps --all //查看全部
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
207ca7680ecc centos:latest "/bin/bash" 20 hours ago Exited (0) 4 seconds ago c0
[root@localhost ~]# docker rmi centos:latest //这里看到没有删掉,因为有容器正在使用
Error response from daemon: conflict: unable to remove repository reference "centos:latest" (must force) - container 207ca7680ecc is using its referenced image 5d0da3dc9764
[root@localhost ~]# docker rm c0 //删除容器
c0
[root@localhost ~]# docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker rmi centos:latest //成功删除
Untagged: centos:latest
Untagged: centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Deleted: sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6
Deleted: sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
4.导入到本地目录
[root@localhost ~]# docker load -i centos.tar
74ddd0ec08fa: Loading layer
[==================================================
>] 238.6MB/238.6MB
Loaded image: centos:latest
[root@localhost ~]# docker image ls //这里看到已经导入成功了
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 2 years ago 231MB
二、容器导出镜像(导出一个有yum源的镜像)
1.有yum源的
[root@localhost ~]# docker run -it --name=c0 centos:latest /bin/bas //创建并启动服务
配置yum源
[root@70f41b63c128 /]# rm -rf /etc/yum.repos.d/*
[root@70f41b63c128 /]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@70f41b63c128 /]# yum clean all && yum makecache
[root@70f41b63c128 /]#yum -y install epel-release
ctrl+p+q退出
[root@localhost ~]# docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
70f41b63c128 centos:latest "/bin/bash" 8 minutes ago Up 8 minutes c0
[root@localhost ~]# docker export -o centos_yum.tar c0 //导出tar
[root@localhost ~]# ls
anaconda-ks.cfg centos.tar centos_yum.tar
[root@localhost ~]# docker import -m yum centos_yum.tar centos:yum //导入为镜像
sha256:9d43e5c9d7e41b8ed7ec287ad1cde2e56297808cab66618e25f25b6962b869fa
[root@localhost ~]# docker image ls //生成了新的镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
centos yum 9d43e5c9d7e4 About a minute ago 260MB
centos latest 5d0da3dc9764 2 years ago 231MB
2.有httpd的
[root@localhost ~]# docker run -it --name c100 centos:yum /bin/bash
[root@1fe0d666d8b9 /]# yum -y install httpd
退出
[root@localhost ~]# docker export -o centos_httpd.tar c100
[root@localhost ~]# ls
anaconda-ks.cfg centos_httpd.tar centos.tar centos_yum.tar
[root@localhost ~]# docker import -m httpd centos_httpd.tar centos:httpd
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos httpd 55b1539d5cd6 About an hour ago 309MB
centos yum 9d43e5c9d7e4 4 hours ago 260MB
centos latest 5d0da3dc9764 2 years ago 231MB
三、IP地址
查看ip
1.进入容器,安装net-tools或iproute,直接使用命令ifconfig
[root@6e9839616992 /]# yum -y install net-tools
[root@6e9839616992 /]# ifconfig
[root@6e9839616992 /]# yum -y install iproute
[root@6e9839616992 /]# ip a s
2.在外面使用docker指令inspect
[root@localhost ~]# docker inspect c100
[root@localhost ~]# docker inspect centos:httpd
3.在外部调用指令
[root@localhost ~]# docker exec c100 ip a s
[root@localhost ~]# docker exec c100 ifconfig
4.使用exec创建一个文件
[root@localhost ~]# docker exec c100 touch /root/tdr.txt
[root@localhost ~]# docker attach c100
[root@6e9839616992 /]# ls /root
anaconda-ks.cfg anaconda-post.log original-ks.cfg tdr.txt
四、虚拟机端口映射
1.将容器中的80端口映射到宿主机的80端口
[root@localhost ~]# docker stop c100 c101 //停用容器
c100
c101
[root@localhost ~]# docker rm c100 c101 //删除容器
c100
c101
[root@localhost ~]# docker run -it --name c0 -p80:80/tcp centos:httpd /bin/bash //创建启动并映射端口,将容器中的80端口映射给宿主机的80端口,前面是宿主机的端口,后面是容器的端口
[root@1fe0d666d8b9 /]# httpd -k start
[root@1fe0d666d8b9 /]# echo "tdr" > /usr/share/httpd/noindex/index.html
ctrl+p+q退出
[root@localhost ~]# curl localhost //宿主机访问,没做端口映射之前就是可以访问的
tdr
去真机浏览器访问虚拟机IP:192.168.1.76 //没做端口映射之前不能访问,现在可以了
2.随机的为容器指定映射端口,映射端口>=32768
[root@localhost ~]# docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1fe0d666d8b9 centos:httpd "/bin/bash" 19 minutes ago Up 19 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp c0
[root@localhost ~]# docker stop c0 //停止
c0
[root@localhost ~]# docker rm c0 //删除
c0
[root@localhost ~]# docker run -it --name c1 -p80 centos:httpd /bin/bash //这里只有80,是容器的80端口
[root@dce0e427f4d6 /]# httpd -k start
[root@dce0e427f4d6 /]# curl localhost
i am tdr
ctrl+p+q退出
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dce0e427f4d6 centos:httpd "/bin/bash" About a minute ago Up About a minute 0.0.0.0:32768->80/tcp, :::32768->80/tcp c1
[root@localhost ~]# curl 192.168.1.76:32768
i am tdr
去真机浏览器访问虚拟机IP:192.168.1.76:32768
3.通过其他的IP地址的端口映射容器的端口
[root@localhost ~]# ifconfig ens33:0 192.168.1.77 broadcast 192.168.1.77 netmask 255.255.255.255 up
[root@localhost ~]# docker run -it --name c0 -p192.168.1.77::80 centos:httpd /bin/bash
[root@1775f9c8e015 /]# httpd -k start
[root@1775f9c8e015 /]# curl localhost
i am tdr
ctrl+p+q退出
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1775f9c8e015 centos:httpd "/bin/bash" About a minute ago Up About a minute 192.168.1.77:32769->80/tcp c0
dce0e427f4d6 centos:httpd "/bin/bash" 17 minutes ago Up 17 minutes 0.0.0.0:32768->80/tcp, :::32768->80/tcp c1
[root@localhost ~]# curl 192.168.1.77:32769
i am tdr
去真机浏览器访问虚拟机IP:192.168.1.77:32769