docker
镜像的迁移:导出和导入镜像
查看镜像:
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 2 years ago 231MB
打包
将镜像打包,找到save,可以将镜像保存为一个tar包,可以将tar发送给其他用户,也可以作为备份
[root@docker ~]# docker save -o centos.tar centos:latest
[root@docker ~]# ls
anaconda-ks.cfg centos.tar RHEL7-extras.iso RHEL7OSP-10.iso
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@docker ~]# docker ps -all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b9d46da21fe3 centos:latest "/bin/bash" 20 hours ago Exited (0) 10 minutes ago c0
删除镜像:
删除镜像时,如果有这个镜像生成的容器正在使用(exited时也不能删除),那么就无法使用rmi移除镜像
[root@docker ~]# docker rmi centos:latest
Error response from daemon: conflict: unable to remove repository reference "centos:latest" (must force) - container b9d46da21fe3 is using its referenced image 5d0da3dc9764
删除容器,退出容器up状态
docker stop c0
[root@docker ~]# docker rm c0
c0
使用docker ps -all 确定没有镜像
[root@docker ~]# docker ps -all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@docker ~]# docker rmi centos:latest
Untagged: centos:latest
Untagged: centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Deleted: sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6
Deleted: sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
加载
从tar中引入镜像:将tar文件加载到镜像中
docker --help
docker load --help
[root@docker ~]# ls
anaconda-ks.cfg centos.tar RHEL7-extras.iso RHEL7OSP-10.iso
[root@docker ~]# docker load -i centos.tar
74ddd0ec08fa: Loading layer 238.6MB/238.6MB
Loaded image: centos:latest
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 2 years ago 231MB
创建镜像
1.从容器导出tar包
查看帮助文档
docker --help
docker export --help
使用指令
docker export -o centos_yum.tar c0
查看新生成的tar包
2.从tar包导入镜像
查看帮助文档
docker --help
docker import --help
使用导入
Usage: docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
docker import -m 说明内容 centos_yum.tar centos:yum
查看镜像,这个镜像不需要我们配置yum
创建容器c0,在c0容器中配置了阿里云镜像和epel-release,ctrl+p+q在后台运行容器
容器打包:
[root@docker ~]# docker export -o centos_yum.tar c0
[root@docker ~]# ls
anaconda-ks.cfg centos_yum.tar RHEL7OSP-10.iso
centos.tar RHEL7-extras.iso
引入镜像:
[root@docker ~]# docker import -m yum centos_yum.tar centos:yum
sha256:c03efa37a8dc3ab41a31a56f9a12c80374c8ccb321d57a617bd2b53a07d4f71e
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos yum c03efa37a8dc 22 seconds ago 260MB
centos latest 5d0da3dc9764 2 years ago 231MB
验证:
[root@docker ~]# docker stop c0
c0
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
882870618b17 centos:latest "/bin/bash" 15 minutes ago Exited (0) 6 seconds ago c0
[root@docker ~]# docker rm c0
c0
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@docker ~]# docker run -it --name c0 centos:yum /bin/bash
[root@860c52d03ab9 /]# ls /etc/yum.repos.d
CentOS-Base.repo epel-playground.repo epel-testing.repo
epel-modular.repo epel-testing-modular.repo epel.repo
ip网络:查看ip
1.交互式:进入到容器,使用指令
docker start c0
docker attach c0
yum -y install net-tools或者iproute
ifconfig或ip a s 来查看地址
2.使用docker指令inspect
[root@docker ~]# docker inspect c100
3.在外部调用指令:exec
前提是在容器上下载相关的工具或者
docker exec c100 yum -y install net-tools
[root@docker ~]# docker exec c100 ip a s
[root@docker ~]# docker exec c100 ifconfig
使用exec指令touch一个文件
[root@docker ~]# docker exec c100 touch /opt/test.txt
[root@docker ~]# docker exec c100 ls /opt
test.txt
启动容器并端口映射
不做端口映射的话,在物理机上无法查看docker容器发布的服务
1.指定端口映射
将容器中的80端口映射到宿主机(docker主机)80端口
docker run -it --name c0 -p80(宿主机):80(容器) centos:httpd /bin/bash
2.随机端口映射
随机的为容器指定映射端口,映射端口>=32768
docker run -it --name c1 -p80(容器) centos:httpd /bin/bash
crtl+p+q
docker ps
3.指定其他ip的端口映射
通过其他的ip地址的端口映射容器的端口
通过ifconfig添加一个ip
docker run -it --name c0 -pIP地址::80 centos:httpd /bin/bash
[root@docker ~]# ifconfig ens33:0 192.168.118.56 broadcast 192.168.118.56 netmask 255.255.255.255 up
[root@docker ~]# docker run -it --name c0 -p192.168.118.56::80 centos:httpd /bin/bash
[root@91b858d59851 /]# httpd -k start
[root@91b858d59851 /]# curl localhost
i am httpd
[root@91b858d59851 /]# [root@docker ~]#
[root@docker ~]# docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
91b858d59851 centos:httpd "/bin/bash" 50 seconds ago Up 50 seconds 192.168.118.56:32768->80/tcp c0
[root@docker ~]# curl 192.168.118.56:32768
i am httpd
持久化:共享卷,挂载
将宿主机上的source目录与容器中的data目录共享:
[root@docker ~]# ls /
bin dev home lib64 mnt proc run source sys usr
boot etc lib media opt root sbin srv tmp var
[root@docker ~]# docker run -it --name c0 -v /source:/data centos:httpd /bin/bash
[root@805a80743684 /]# ls
bin dev home lib64 media opt root sbin sys usr
data etc lib lost+found mnt proc run srv tmp var
[root@805a80743684 /]# [root@docker ~]#
[root@docker ~]# touch /source/abc.txt
[root@docker ~]# ls /source
abc.txt
[root@docker ~]# docker exec c0 ls /data
abc.txt
[root@docker ~]# docker exec c0 touch /data/def.txt
[root@docker ~]# ls /source
abc.txt def.txt