一.镜像文件的打包
将镜像打包
查看帮助文件
docker --help
找到save,可以将镜像保存为一个tar包
docker save --hellp
查看现有的镜像
docker images
root@docker0 ~]# docker save -o centos.tar centos:latest
[root@docker0 ~]# ls
anaconda-ks.cfg centos.tar
可以将tar发送给其他用户,也可作为备份
删除镜像
[root@docker0 ~]# docker rmi centos:latest
Error response from daemon: conflict: unable to remove repository reference "centos:latest" (must force) - container 832204f3a881 is using its referenced image 5d0da3dc9764
#如果有这个镜像生成的容器正在使用,那么用rmi就无法删除
#删除容器,退出容器up状态
[root@docker0 ~]# docker rm c0
c0
#使用docker ps --all确定没有镜像
[root@docker0 ~]# docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@docker0 ~]# docker rmi centos:latest
Untagged: centos:latest
Untagged: centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Deleted: sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6
Deleted: sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59
2.从tar中引入镜像
docker --help
docker load --help
[root@docker0 ~]# docker load -i centos.tar
74ddd0ec08fa: Loading layer 238.6MB/238.6MB
Loaded image: centos:latest
[root@docker0 ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 2 years ago 231MB
Sha
3.打开一个新容器;
[root@docker0 ~]# docker run -it --name c0 centos:yum /bin/bash
从容器中导出tar包
docker --help
docker export --help
使用指令
[root@docker0 ~]# docker export -o centos_yum.tar c0
[root@docker0 ~]# ls
anaconda-ks.cfg centos.tar centos_yum.tar
4.从tar包导入镜像
查看帮助
docker --help
docker import --help
使用指令
[root@docker0 ~]# docker import -m yum centos_yum.tar centos:yum
sha256:57439e0dc3974aff8dfd90d6fdad955975ac305efe05f708954955920b996267
[root@docker0 ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
centos yum 57439e0dc397 31 seconds ago 233MB
centos latest 5d0da3dc9764 2 years ago 231MB
4.做一个带httpd服务的镜像:
docker run -it --name c100 centos:yum /bin/bash
yum -y install httpd
echo “aaa” > /var/www/html/index.html
httpd -k start
ctrl p q
docker export -o centos_httpd.tar c100
docker import -m httpd centos_httpd.tar centos:httpd
[root@docker0 ~]# docker import -m httpd centos_httpd.tar centos:httpd
sha256:7c5809f9310ed04687ed9c65076bde1ec9178a81b7ba1b5278ae5fd0526aeba2
二.查看ip
进入容器使用指令
docker attach c100
yum -y install net-tools
yum -y install iproute
ip a s
ifconfig
使用docker指令inspect
docker inspect c100
3.在外部调用指令
docker exec c100 ip a
docker exec c100 ifconfig
虚拟机端口映射
[root@docker0 ~]# docker stop c100
c100
[root@docker0 ~]# docker rm c100
c100
[root@docker0 ~]# docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1.将容器中的80端口映射到宿主机(docker主机)的80端口
[root@docker0 ~]# docker run -it --name c0 -p80:80/tcp centos:httpd /bin/bash //第一个80是宿主机的80端口,第二个80是容器的80端口
[root@74c5b3132c9f /]# httpd -k start
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[root@74c5b3132c9f /]# curl localhost
“aaa”
[root@docker0 ~]# curl 172.17.0.2
“aaa”
[root@docker0 ~]# curl 192.168.2.13
“aaa”
练习:
[root@docker0 ~]# docker run -it --name c1 -p12345:80 centos:httpd /bin/bash
[root@7f6fab58c90b /]# httpd -k start
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
[root@7f6fab58c90b /]# curl localhost
“aaa”
[root@docker0 ~]# curl 172.17.0.2
“aaa”
[root@docker0 ~]# docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7f6fab58c90b centos:httpd "/bin/bash" 2 minutes ago Up 2 minutes 0.0.0.0:12345->80/tcp, :::12345->80/tcp c1
74c5b3132c9f centos:httpd "/bin/bash" 14 minutes ago Up 14 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp c0
2.随机的为容器指定映射端口,映射端口>=32768
[root@docker0 ~]# docker run -it --name c0 -p80 centos:httpd /bin/bash
[root@9f83dc07c22e /]# httpd -k start
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[root@9f83dc07c22e /]# curl localhost
“aaa”
[root@docker0 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9f83dc07c22e centos:httpd "/bin/bash" 41 seconds ago Up 41 seconds 0.0.0.0:32768->80/tcp, :::32768->80/tcp c0
[root@docker0 ~]# curl 192.168.2.13:32768
“aaa”
[root@docker0 ~]# docker run -it --name c1 -p80 centos:httpd /bin/bash
[root@c8f64dea290b /]# httpd -k start
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
[root@c8f64dea290b /]# read escape sequence
[root@docker0 ~]# curl 192.168.2.13:32769
“aaa”
3.通过其他的ip地址的端口映射容器的端口
[root@docker0 ~]# ifconfig ens33:0 192.168.2.11 broadcast 192.168.2.11 netmask 255.255.255.255 up
[root@docker0 ~]# docker stop c0 c1
[root@docker0 ~]# docker rm c0 c1
[root@docker0 ~]# docker run -it --name c0 -p192.168.2.11::80 centos:httpd /bin/bash
[root@61cfbb4e4d53 /]# httpd -k start
[root@61cfbb4e4d53 /]# curl localhost
“aaa”
三.持久化
挂载外部文件
[root@docker0 ~]# mkdir /source
[root@docker0 ~]# docker run -it --name c0 -v source:/data centos:httpd /bin/bash
[root@docker0 ~]# touch /source/abc.txt
[root@docker0 ~]# ls /source/
abc.txt
[root@docker0 ~]# docker exec c0 ls /data
abc.txt
[root@docker0 ~]# docker exec c0 touch /data/def.txt
[root@docker0 ~]# ls /source/
abc.txt def.txt