2024.8.23(docker)

一、打包传输

1、打包

[root@docker ~]# docker save -o centos.tar centos:latest 

[root@docker ~]# ls

2、删除镜像

[root@docker ~]# docker ps -all
CONTAINER ID   IMAGE           COMMAND       CREATED        STATUS                    PORTS     NAMES
372f7f49e6df   centos:latest   "/bin/bash"   20 hours ago   Exited (0) 18 hours ago             c0
[root@docker ~]# docker rmi centos:latest       如果有这个镜像生成的容器正在使用,那么无法使用rmi移除镜像
Error response from daemon: conflict: unable to remove repository reference "centos:latest" (must force) - container 372f7f49e6df is using its referenced image 5d0da3dc9764

3、删除容器,退出up状态

[root@docker ~]# docker rm c0
c0

4、使用docker ps --all 确定没有镜像

[root@docker ~]# docker ps -all
CONTAINER ID   IMAGE           COMMAND       CREATED        STATUS                    PORTS     NAMES
372f7f49e6df   centos:latest   "/bin/bash"   20 hours ago   Exited (0) 18 hours ago             c0
[root@docker ~]# docker rmi centos:latest    //删除镜像

二、容器导出镜像

1、从tar中引入镜像

[root@docker ~]# docker load -i centos.tar
[root@docker ~]# docker run -i -t --name=c0 centos:latest /bin/bash

[root@docker ~]# docker start c0
c0
[root@docker ~]# docker attach c0
[root@ac4284ebcb00 /]# rm -rf /etc/yum.repos.d/*
[root@ac4284ebcb00 /]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@ac4284ebcb00 /]# yum clean all && yum makecache
[root@ac4284ebcb00 /]# yum -y install epel-release
[root@ac4284ebcb00 /]# read escape sequence       crrl+p+q
[root@docker ~]# docker ps --all
CONTAINER ID   IMAGE           COMMAND       CREATED          STATUS         PORTS     NAMES
ac4284ebcb00   centos:latest   "/bin/bash"   29 minutes ago   Up 8 minutes             c0

2、从容器导出tar包

[root@docker ~]# docker export -o centos_yum.tar c0 
[root@docker ~]# ls

3、从tar包导入镜像

[root@docker ~]# docker import -m yum centos_yum.tar  centos:yum 
[root@docker ~]# docker images         //查看镜像    这个镜像不需要配置yum

[root@docker ~]# docker stop c0
c0
[root@docker ~]# docker ps -all
[root@docker ~]# docker rm c0
c0
[root@docker ~]# docker run -it --name c0 centos:yum /bin/bash
[root@015996a52240 /]# ls /etc/yum.repos.d/

练习:

创建一个镜像,包含httpd服务。名称 centos,版本为http

[root@docker ~]# docker run -it --name c0 centos:yum /bin/bash
[root@015996a52240 /]# ls /etc/yum.repos.d/
CentOS-Base.repo   epel-playground.repo       epel-testing.repo
epel-modular.repo  epel-testing-modular.repo  epel.repo
[root@015996a52240 /]# yum -y install httpd
[root@015996a52240 ~]# yum -y install iproute
[root@015996a52240 ~]# yum -y install net-tools

[root@015996a52240 ~]# echo "docker_httpd_server" > /var/www/html/index.html
[root@015996a52240 ~]# httpd -k start
[root@015996a52240 ~]# curl localhost
docker_httpd_server

[root@docker ~]# docker export -o centos_httpd.tar c0
[root@docker ~]# ls

[root@docker ~]# docker import -m httpd centos_httpd.tar centos:httpd
[root@docker ~]# docker images

[root@docker ~]# docker run -it --name c1 centos:httpd /bin/bash

[root@9191fc30dc14 /]# ls /etc/yum.repos.d/
CentOS-Base.repo   epel-playground.repo       epel-testing.repo
epel-modular.repo  epel-testing-modular.repo  epel.repo
[root@9191fc30dc14 /]# httpd -k start

[root@9191fc30dc14 /]# ifconfig

[root@9191fc30dc14 /]# curl 172.17.0.3
docker_httpd_server

三、查看IP

1、查看容器ip

[root@9191fc30dc14 /]# ip a s
[root@9191fc30dc14 /]# ifconfig

2、inspect

[root@docker ~]# docker inspect c1 

3、在外部调用指令

[root@docker ~]# docker exec c1 ip a s
[root@docker ~]# docker exec c1 ifconfig

4、使用exec创建文件

[root@docker ~]# docker exec c1 touch /opt/test.txt
[root@docker ~]# docker exec c1 ls /opt/
test.txt

四、端口映射

1、指定端口映射

docker run -it -name c0 -p70:80 centos:httpd /bin/bash

2、随机端口映射

docker run -it -- name c1 -p80 centos:httpd /bin/bash

3.指定其他ip的端口映射

ifconfig ens33:0 192.168.71.51 broadcast192.168.71.51 netmask 255.255.255.255 up

docker run -it -- name c2 -p192.168.71.51 :: 80 centos:httpd /bin/bash

五、持久化

1、挂载

docker run -it -- name c3 -v /abc:/def centos:httpd/bin/bash

touch /abc/aaa. txt
docker exec c3 ls /def

 

  • 16
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值