当前位置: 首页 > news >正文

docke进阶---镜像迁移、容器的ip地址、端口映射和持久化

1.镜像的迁移

1.镜像打包

#查看镜像有一个centos的镜像
[root@docker0 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
centos       latest    5d0da3dc9764   2 years ago   231MB
3查看帮助文件
docker --help
save        Save one or more images to a tar archive (streamed to STDOUT by default)
#找到save,可以将镜像保存为一个tar包
#查看save使用方式
[root@docker0 ~]# docker save --helpUsage:  docker save [OPTIONS] IMAGE [IMAGE...]Save one or more images to a tar archive (streamed to STDOUT by default)Aliases:docker image save, docker saveOptions:-o, --output string   Write to a file, instead of STDOUT
#打包镜像
[root@docker0 ~]# docker save -o centos.tar centos:latest
#查看镜像包
[root@docker0 ~]# ls
anaconda-ks.cfg  centos.tar  initserver.sh
#可以将tar发送给其他用户或者做备份

2.镜像加载

[root@docker0 ~]# docker --help
import      Import the contents from a tarball to create a filesystem image
load        Load an image from a tar archive or STDIN
#查看load的用法
[root@docker0 ~]# docker load --helpUsage:  docker load [OPTIONS]Load an image from a tar archive or STDINAliases:docker image load, docker loadOptions:-i, --input string   Read from tar archive file, instead of STDIN-q, --quiet          Suppress the load output
#加载镜像
[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

 停用关闭容器

docker stop c0 c1
docker rm c0 c1

删除镜像

docker rmi centos:latest

3.容器导出镜像

运行一个容器

[root@docker0 ~]# docker ps --all
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@docker0 ~]# docker run -it --name c0 centos:latest /bin/bash
[root@2d3ce2feba40 /]# 

 配置yum源

[root@2d3ce2feba40 ~]# rm -rf /etc/yum.repos.d/*
[root@2d3ce2feba40 ~]# cat /etc/redhat-release
CentOS Linux release 8.4.2105
[root@2d3ce2feba40 /]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo

 查看yum源并建立缓存

[root@2d3ce2feba40 /]# ls /etc/yum.repos.d/
CentOS-Base.repo
[root@2d3ce2feba40 /]# yum clean all && yum makecache
Failed to set locale, defaulting to C.UTF-8
0 files removed
Failed to set locale, defaulting to C.UTF-8
CentOS-8.5.2111 - Base - mirrors.aliy 446 kB/s | 4.6 MB     00:10    
CentOS-8.5.2111 - Extras - mirrors.al  13 kB/s |  10 kB     00:00    
CentOS-8.5.2111 - AppStream - mirrors 303 kB/s | 8.4 MB     00:28    
Metadata cache created.

 创建一个yum源镜像

[root@docker0 ~]# docker export -o centos_yum.tar c0
[root@docker0 ~]# ls
anaconda-ks.cfg  centos.tar  centos_yum.tar  initserver.sh

下载一个epel源

[root@2d3ce2feba40 /]# yum -y install epel-release.noarch

 查看镜像会出现一个有yum的新镜像

[root@docker0 ~]# docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
centos       yum       06a31e228ee1   39 seconds ago   260MB
centos       latest    5d0da3dc9764   2 years ago      231MB

 使用新镜像

[root@docker0 ~]# docker ps --all
CONTAINER ID   IMAGE           COMMAND       CREATED          STATUS                      PORTS     NAMES
2d3ce2feba40   centos:latest   "/bin/bash"   39 minutes ago   Exited (0) 13 minutes ago             c0
[root@docker0 ~]# docker rm c0
c0
[root@docker0 ~]# docker ps --all
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@docker0 ~]# docker run -it --name c0 centos:yum /bin/bash
[root@53017a519e3c /]# ls /etc/yum.repos.d/
CentOS-Base.repo   epel-playground.repo       epel-testing.repo
epel-modular.repo  epel-testing-modular.repo  epel.repo
[root@53017a519e3c /]# 

练习:制作一个httpd镜像

#查看运行的容器c0
[root@docker0 ~]# docker ps --all
CONTAINER ID   IMAGE        COMMAND       CREATED       STATUS                   PORTS     NAMES
53017a519e3c   centos:yum   "/bin/bash"   3 hours ago   Exited (0) 2 hours ago             c0
#删除c0
[root@docker0 ~]# docker rm c0
c0
[root@docker0 ~]# docker ps --all
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

 运行一个容器并进入容器

[root@docker0 ~]# docker run -it --name c100 centos:yum /bin/bash

 下载httpd

[root@66afe850d388 /]# yum -y install httpd
centos:httpd

 修改index.html

[root@66afe850d388 /]#echo "aaabbbccc" > /var/www/html/index.html

启动服务

[root@66afe850d388 /]# 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@66afe850d388 /]# curl localhost
aaabbbccc

Ctrl p q暂时退出容器,查看容器正在运行

[root@docker0 ~]# docker ps --all
CONTAINER ID   IMAGE        COMMAND       CREATED         STATUS         PORTS     NAMES
66afe850d388   centos:yum   "/bin/bash"   6 minutes ago   Up 6 minutes             c100

导出httpd镜像

[root@docker0 ~]# docker export -o centos_httpd.tar c100
[root@docker0 ~]# ls
anaconda-ks.cfg   centos.tar      initserver.sh
centos_httpd.tar  centos_yum.tar

 引入httpd镜像

[root@docker0 ~]# docker import -m httpd centos_httpd.tar centos:httpdsha256:6e62dbf29499e0f2728ec12c86f18824aee2c2c7ee4e8356c9ad898edc939cb4
[root@docker0 ~]# docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
centos       httpd     6e62dbf29499   16 seconds ago   309MB
centos       yum       06a31e228ee1   3 hours ago      260MB
centos       latest    5d0da3dc9764   2 years ago      231MB

3.IP地址

1.进入容器查看

[root@docker0 ~]# docker attach c100
[root@66afe850d388 /]# yum -y install net-tools
[root@66afe850d388 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)RX packets 10902  bytes 18719264 (17.8 MiB)RX errors 0  dropped 0  overruns 0  frame 0TX packets 6207  bytes 348509 (340.3 KiB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536inet 127.0.0.1  netmask 255.0.0.0inet6 ::1  prefixlen 128  scopeid 0x10<host>loop  txqueuelen 1000  (Local Loopback)RX packets 16  bytes 1240 (1.2 KiB)RX errors 0  dropped 0  overruns 0  frame 0TX packets 16  bytes 1240 (1.2 KiB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0[root@66afe850d388 /]# curl 127.17.0.2
aaabbbccc
[root@66afe850d388 /]# curl 127.17.0.3
aaabbbccc
[root@66afe850d388 /]# read escape sequence

2.使用docker指令inspect

#inspect用法
inspect     Return low-level information on Docker objects
[root@docker0 ~]# docker inspect --helpUsage:  docker inspect [OPTIONS] NAME|ID [NAME|ID...]Return low-level information on Docker objectsOptions:-f, --format string   Format output using a custom template:'json':             Print in JSONformat'TEMPLATE':         Print output usingthe given Go template.Refer tohttps://docs.docker.com/go/formatting/for more information about formattingoutput with templates-s, --size            Display total file sizes if the type iscontainer--type string     Return JSON for specified type[root@docker0 ~]# docker inspect c100

3.外部调用指令

docker exec c100 ip a

docker exec c101 ifconfig

[root@docker0 ~]# docker exec c100 ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope host valid_lft forever preferred_lft forever
4: eth0@if5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0valid_lft forever preferred_lft forever[root@docker0 ~]# docker exec c101 ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet 172.17.0.3  netmask 255.255.0.0  broadcast 172.17.255.255ether 02:42:ac:11:00:03  txqueuelen 0  (Ethernet)RX packets 257  bytes 345445 (337.3 KiB)RX errors 0  dropped 0  overruns 0  frame 0TX packets 163  bytes 9248 (9.0 KiB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536inet 127.0.0.1  netmask 255.0.0.0inet6 ::1  prefixlen 128  scopeid 0x10<host>loop  txqueuelen 1000  (Local Loopback)RX packets 22  bytes 1861 (1.8 KiB)RX errors 0  dropped 0  overruns 0  frame 0TX packets 22  bytes 1861 (1.8 KiB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

4.端口映射

1.将容器中的80端口映射到宿主机(docker主机)80端口

[root@docker0 ~]# docker ps --all
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@docker0 ~]# 
[root@docker0 ~]# docker run -it --name c0 -p80:80/tcp centos:httpd /bin/bash
[root@12c584d32bf9 /]# 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@12c584d32bf9 /]# curl localhost
aaabbbccc
[root@12c584d32bf9 /]# [root@docker0 ~]# 
[root@docker0 ~]# curl 172.17.0.2
aaabbbccc
[root@docker0 ~]# curl localhost
aaabbbccc
[root@docker0 ~]# curl 192.168.1.50
aaabbbccc

 

 2.随机为容器指定映射一个端口,映射端口大于等于32768

[root@docker0 ~]# docker ps --all
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@docker0 ~]# docker run -it --name c0 -p80 centos:httpd /bin/bash
[root@ad4aa342a339 /]# 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@ad4aa342a339 /]# curl localhost
aaabbbccc
[root@ad4aa342a339 /]# [root@docker0 ~]# 
[root@docker0 ~]# docker ps
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS          PORTS                                     NAMES
ad4aa342a339   centos:httpd   "/bin/bash"   29 seconds ago   Up 29 seconds   0.0.0.0:32769->80/tcp, :::32769->80/tcp   c0
[root@docker0 ~]# curl 192.168.1.50:80
curl: (7) Failed connect to 192.168.1.50:80; 拒绝连接
[root@docker0 ~]# curl 192.168.1.50:32769
aaabbbccc

 3.通过其他的IP地址的端口映射容器的端口

 docker run -it --name c0 -p129.168.1.51 centos:httpd /bin/bash

5.持久化

挂载

在创建容器的时候挂载

[root@docker0 ~]# docker ps --all
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
#创建一个挂载目录
[root@docker0 ~]# mkdir /source[root@docker0 ~]# docker run -it --name c0 -v /source:/data centos:httpd /bin/bash
#查看容器就会有一个data目录
[root@76f17e18485d /]# ls
bin   etc   lib64	mnt   root  srv  usr
data  home  lost+found	opt   run   sys  var
dev   lib   media	proc  sbin  tmp
#暂时退出容器
[root@76f17e18485d /]# [root@docker0 ~]# 
#在容器外面创建一个文档
[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


http://www.mrgr.cn/news/9032.html

相关文章:

  • java socket通讯
  • 下载官方llama
  • UE管理内容 —— FBX Scene Import
  • Java面试题--JVM大厂篇之JVM大厂面试题及答案解析(4)
  • 多人协作开发git merge合并功能出现冲突时解决思路
  • 徐州服务器租用:高防服务器的用途有哪些?
  • MySQL 高阶三 (索引性能分析)
  • centos 服务器之间实现免密登录
  • 数学建模学习(122):基于PPF-AHP的多准则决策分析—以城市交通枢纽选址为例
  • 数据结构的顺序表的学习
  • Ai+若依(系统接口--Swagger):04篇
  • 回零及编码器
  • 斗破C++编程入门系列之十六:C++程序设计必知:类的静态成员(九星斗者)彩蛋
  • 哈希环算法(C语言版本)
  • selenium-java实现自动登录跳转页面
  • 【日记】狗尾巴草与暗恋(1519 字)
  • java调用阿里大模型服务平台百炼
  • 【吊打面试官系列-Memcached面试题】什么是二进制协议,我该关注吗?
  • 字符函数内存函数———C语言
  • js 数组使用 map 结构渲染个性字段