打包传输
1.将镜像打包
#查看帮助文件
docker --help
#找到save,可以将镜像保存为一个tar包
docker save --help
#查看save使用方式
#查看现有的镜像
docker images
# docker save --output centos.tar centos:latest
ls
...centos.tar...
可以将tar发送给其他用户,也可以作为备份
删除镜像
[root@docker ~]# docker rmi centos:latest
Error response from daemon: conflict: unable to remove repository reference "centos:latest" (must force) - container 5b068251f855 is using its referenced image 5d0da3dc9764
#如果有这个镜像生成的容器正在使用,那么就无法使用rmi移除镜像#删除容器,退出容器的up状态
docker rm c0
[root@docker ~]# docker rm c0
c0[root@docker ~]# docker ps --all #使用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中引入镜像
docker --help
docker load --help
docker load -i centos.tar centos:latest
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@docker ~]# ls
anaconda-ks.cfg centos.tar
[root@docker ~]# docker load --helpUsage: docker load [OPTIONS]
Load an image from a tar archive or STDIN
Aliases:
docker image load, docker loadOptions:
-i, --input string Read from tar archive file, instead of STDIN
-q, --quiet Suppress the load output
[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
打开一个新的容器
docker run -it --name c0 centos:latest /bin/bash
ctrl ^p^q 退出但不关闭
创建镜像
#查看所有的容器
docker ps --all
#停用并且删除容器
docker stop c1 && docker rm c1
容器导出镜像
1.从容器导出tar包
#查看帮助文档
docker --help
docker export --help
#使用指令
docker export -o centos_yum.tar c0 将容器导出为tar包
#查看新生成的tar包
2.从tar包导入镜像
#查看帮助
docker --help
docker import --help
#使用导入
docker import -m 说明内容 centos_yum.tar centos:yum
#查看镜像
docker images
#这个镜像不需要我们配置yum
练习:
做一个带httpd服务的镜像
docker run -it --name c100 centos:yum /bin/bash
yum -y install httpd
echo "aaabbbccc" > /var/www/html/index.html
httpd -k start
curl ^P^Q
docker export -o centos_httpd.tar c100
docker import -m httpd centos_httpd.tar centos:httpd
实操:
[root@docker httpd]# docker run -it --name c100 centos:yum /bin/bash
[root@f223078b317b /]# yum -y install httpd
[root@f223078b317b /]# echo "aaabbbcccddd" > /var/www/html/index.html
[root@f223078b317b /]# 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@f223078b317b /]# curl localhost
aaabbbcccddd
[root@f223078b317b /]# [root@docker httpd]#
[root@docker httpd]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f223078b317b centos:yum "/bin/bash" 2 minutes ago Up 2 minutes c100
[root@docker httpd]# docker export -o centos_httpd c100
[root@docker httpd]# ls
centos_httpd dockerfile index.html
[root@docker httpd]# docker import -m intrduce centos_httpd centos:httpd
sha256:4bec8f3517a5f9c7934e9b9c61db1b7eec106b5ab4375b905d8dcfbf93707a86
[root@docker httpd]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
centos httpd 4bec8f3517a5 21 seconds ago 309MB
centos yum 3e62c6e9639e 3 hours ago 260MB
centos 7 eeb6ee3f44bd 2 years ago 204MB
centos latest 5d0da3dc9764 2 years ago 231MB
[root@docker httpd]# docker run -it --name c101 centos:httpd /bin/bash 启动一个容器之后,可以启动httpd服务
[root@17f686ad3194 /]# 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@17f686ad3194 /]# curl localhost
aaabbbcccddd[root@docker ~]# docker inspect --help 查看语法
查看ip
1.进入到容器,使用指令
docker attach c100
yum -y install net-tools/iproute
ip a s
ifconfig
2.使用docker指令
docker inspect c100
docker inspect c101
3.在外部调用指令
docker exec c100 ip a s
docker exec c101 ifconfig
4.练习测试实验exec创建(touch)一个文件
[root@docker ~]# docker exec c100 touch /opt/test.txt
[root@docker ~]# docker exec c100 ls /opt/
test.txt
启动容器并端口映射
1.将容器中的80端口映射到宿主机(docker主机)80端口
docker run -it --name c0 -p80:80 centos:httpd /bin/bash
2.随机的为容器指定映射端口,映射端口>=32768
doaker run -it --name c1 -p80 centos:httpd /bin/bash
curl p q
docker ps
3.通过其他的ip地址的端口映射容器的端口
docker run -it --name c0 -p192.168.2.80 centos:httpd /bin/bash
访问192.168.2.80:32768
数据存储
1.为什么
因为docker 容器只是一个工具,不需要保存数据,不需要做持久化,如果要做持久化,那么就需要保存到宿主机上,需要宿主机和容器有一个共享卷
docker run -it --name c0 -v /source:/data