opo 发表于 2018-5-29 11:37:00

Docker安装与镜像管理(一)

  一、安装docker

  yum install epel-resase
  yum install docker-io(RHEL6)
  yum install docker (RHEL7)
  

  系统:
  # uname -a
  Linux kvm.huangming.org 2.6.32-573.el6.x86_64 #1 SMP Thu Jul 23 15:44:03 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
  

  # cat /etc/issue
  CentOS release 6.7 (Final)
  Kernel \r on an \m
  

  二、启动docker
# /etc/init.d/docker start
Starting docker:                                 
# ps -ef |grep docker
root       3900      11 19:28 pts/0    00:00:00 /usr/bin/docker -d
root       3927   3900 34 19:28 pts/0    00:00:04 mkfs.ext4 -E nodiscard,lazy_itable_init=0 /dev/mapper/docker-8:3-1573967-base
root       3938   36060 19:28 pts/0    00:00:00 grep docker  

  三、镜像管理

[*]  docker pull centos
  #从docker.com获取centos镜像
[*]  docker images
  #查看本地镜像
[*]  docker tag centos test-img
  #为centos镜像设置标签为test-img,在使用docker image查看时会多出一行,更改的image id和centos的一样
[*]  docker search
  #搜索docker仓库里的docker镜像
[*]  docker run -t -i centos /bin/bash
  #用下载到的镜像开启容器,-i表示让容器的标准输入打开,-t表示分配一个伪终端,要把-i -t放到镜像名字的前面。当镜像发生修改后,我们可以把该镜像提交重新生成一个新版本运行在本地
[*]  docker ps
  #查看运行的容器,加-a选项可以查看没有运行的容器
[*]  docker rmi centos
  #用来删除指定的镜像,其中后面的参数可以是tag,如果是tag时,实际上是删除该tag,只要该镜像还有其他的tag,就不会删除该镜像。当后面的参数为ID时,则会彻底删除整个镜像,连同所有的标签。
  

  1、下载centos镜像
# docker pull centos
latest: Pulling from centos
47d44cb6f252: Already exists
6fdebd7b0eb5: Already exists
a63aae4d216f: Already exists
Digest: sha256:381f21e4c7b3724c6f420b2bcfa6e13e47ed155192869a2a04fa10f944c78476
Status: Image is up to date for centos:latest  2、查看下载到本地镜像
# docker images
REPOSITORY          TAG               IMAGE ID            CREATED             VIRTUAL SIZE
centos            latest            bb3d629a7cbc      3 weeks ago         196.6 MB  3、查看docker仓库镜像
# docker search centos
NAME                            DESCRIPTION                                     STARS   OFFICIAL   AUTOMATED
centos                        The official build of CentOS.                   2069            
jdeathe/centos-ssh            CentOS-6 6.7 x86_64 / CentOS-7 7.2.1511 x8...   18                  
jdeathe/centos-ssh-apache-php   CentOS-6 6.7 x86_64 / Apache / PHP / PHP M...   14                  
million12/centos-supervisor   Base CentOS-7 with supervisord launcher, h...   10                  
blalor/centos                   Bare-bones base CentOS 6.5 image                8                  
nimmis/java-centos            This is docker images of CentOS 7 with dif...   8                  
torusware/speedus-centos      Always updated official CentOS docker imag...   7                  
centos/mariadb55-centos7                                                      3                  
nathonfowlie/centos-jre         Latest CentOS image with the JRE pre-insta...   3                  
nickistre/centos-lamp         LAMP on centos setup                            3                  
consol/sakuli-centos-xfce       Sakuli end-2-end testing and monitoring co...   2                  
softvisio/centos                Centos                                          1                  
layerworx/centos                CentOS container with etcd, etcdctl, confd...   1                  
yajo/centos-epel                CentOS with EPEL and fully updated            1                  
lighthopper/orientdb-centos   A Dockerfile for creating an OrientDB imag...   1                  
darksheer/centos                Base Centos Image -- Updated hourly             1                  
timhughes/centos                Centos with systemd installed and running       1                  
januswel/centos               yum update-ed CentOS image                      0                  
drokar/centos-s6                centOS combined with s6, a small process s...   0                  
lighthopper/openjdk-centos      A Dockerfile for creating an OpenJDK image...   0                  
ustclug/centos                   USTC centos                                    0                  
blacklabelops/centos            CentOS Base Image! Built and Updates Daily!   0                  
grayzone/centos               auto build for centos.                        0                  
jsmigel/centos-epel             Docker base image of CentOS w/ EPEL installed   0                  
grossws/centos                  CentOS 6 and 7 base images with gosu and l...   0                    

  4、开启一个容器docker run -it centos /bin/bash
# docker run -it centos /bin/bash
#  

# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
# uname -a               
Linux 8c1fd812d079 2.6.32-573.el6.x86_64 #1 SMP Thu Jul 23 15:44:03 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
# w
11:55:12 up 42 min,0 users,load average: 0.00, 0.03, 0.01
USER   TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
# ps
   PID TTY          TIME CMD
   1 ?      00:00:00 bash
    16 ?      00:00:00 ps
# uptime
11:55:21 up 42 min,0 users,load average: 0.00, 0.02, 0.01
# df -Th
Filesystem                                                                                    Type   SizeUsed Avail Use% Mounted on
/dev/mapper/docker-8:3-1573967-8c1fd812d0790d1868bcec389a50c6f7a08c6c099167141393af0961e7b850be ext4   9.8G230M9.0G   3% /
tmpfs                                                                                           tmpfs1.9G   01.9G   0% /dev
shm                                                                                             tmpfs   64M   0   64M   0% /dev/shm
/dev/sda3                                                                                       ext4    43G4.6G   36G12% /etc/hosts
# exit  

# docker ps -a
CONTAINER ID      IMAGE               COMMAND             CREATED             STATUS                        PORTS               NAMES
f732106147eb      centos            "/bin/bash"         17 seconds ago      Up 15 seconds                                       high_heisenberg   
8c1fd812d079      centos            "/bin/bash"         3 minutes ago       Exited (0) About a minute ago                     grave_mcclintock  

  5、修改镜像名称
# docker images
REPOSITORY          TAG               IMAGE ID            CREATED             VIRTUAL SIZE
centos            latest            bb3d629a7cbc      3 weeks ago         196.6 MB
# docker tag centos test-img
# docker images
REPOSITORY          TAG               IMAGE ID            CREATED             VIRTUAL SIZE
centos            latest            bb3d629a7cbc      3 weeks ago         196.6 MB
test-img            latest            bb3d629a7cbc      3 weeks ago         196.6 MB
# docker tag test-img centos:test-img2
# docker images
REPOSITORY          TAG               IMAGE ID            CREATED             VIRTUAL SIZE
test-img            latest            bb3d629a7cbc      3 weeks ago         196.6 MB
centos            latest            bb3d629a7cbc      3 weeks ago         196.6 MB
centos            test-img2         bb3d629a7cbc      3 weeks ago         196.6 MB  

  6、删除镜像
# docker rmi centos:test-img2
Untagged: centos:test-img2
# docker images
REPOSITORY          TAG               IMAGE ID            CREATED             VIRTUAL SIZE
centos            latest            bb3d629a7cbc      3 weeks ago         196.6 MB
test-img            latest            bb3d629a7cbc      3 weeks ago         196.6 MB  

  四、docker镜像使用容器生成新的镜像
  1、启动镜像

# docker start 8c1fd
8c1fd
# docker ps -a
CONTAINER ID      IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
f732106147eb      centos            "/bin/bash"         12 minutes ago      Exited (0) 43 seconds ago                     high_heisenberg   
8c1fd812d079      centos            "/bin/bash"         15 minutes ago      Up 3 seconds  2、进入已启动的容器docker exec -it 【镜像ID,可以简写】 /bin/bash

# docker exec -it 8c1fd /bin/bash
#  3、在容器中安装软件(安装网卡包net-tools)

  
  # yum install net-tools wget -y
# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>mtu 1500
      inet 172.17.0.3netmask 255.255.0.0broadcast 0.0.0.0
      inet6 fe80::42:acff:fe11:3prefixlen 64scopeid 0x20<link>
      ether 02:42:ac:11:00:03txqueuelen 0(Ethernet)
      RX packets 5419bytes 10411538 (9.9 MiB)
      RX errors 0dropped 0overruns 0frame 0
      TX packets 4127bytes 268377 (262.0 KiB)
      TX errors 0dropped 0 overruns 0carrier 0collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING>mtu 65536
      inet 127.0.0.1netmask 255.0.0.0
      inet6 ::1prefixlen 128scopeid 0x10<host>
      looptxqueuelen 0(Local Loopback)
      RX packets 0bytes 0 (0.0 B)
      RX errors 0dropped 0overruns 0frame 0
      TX packets 0bytes 0 (0.0 B)
      TX errors 0dropped 0 overruns 0carrier 0collisions 0  4、生成新的镜像

[*]  docker commit -m "change somth" -a "somebody info" container_id new-image
  例如:docker commit -m "install httpd" -a "test-image1" 8c1fd812d079 test/centos
  

# docker commit -m "centos_with_nettools_and_wget" -a "author" 8c1fd812d079 centos_new1
3e4be15719e03e909f39a7c03f3c7cd4fa6f2a8e282b2b88204d0224104b20e6
# docker images
REPOSITORY          TAG               IMAGE ID            CREATED             VIRTUAL SIZE
centos_new1         latest            3e4be15719e0      16 seconds ago      268 MB
centos            latest            bb3d629a7cbc      3 weeks ago         196.6 MB
test-img            latest            bb3d629a7cbc      3 weeks ago         196.6 MB  

  五、docker基于本地模版导入创建镜像
  1、下载地址https://download.openvz.org/template/precreated/

  2、下载一个镜像模版

  
  


[*]  导入镜像

      #cat centos-6-x86_64-minimal.tar.gz | docker import - centos-6-x86_64

[*]  导出镜像
      #docker save -o new-centos.tar new-image/centos

[*]  恢复本地镜像
      #docker load --input new-centos.tar 或者 docker load < new-centos.tar

[*]  上传镜像到dockerhub官网,前提需要注册一个用户
      #docker push image-name
  

  
  3、导入镜像
# cat centos-6-x86_64-minimal.tar.gz | docker import - centos-6-x86_64
1014358cbf3b643cae13e830afc539cbf0e02aa40f6c77583898a0cbd33a9561

[*]  查看镜像
# docker images
REPOSITORY          TAG               IMAGE ID            CREATED            VIRTUAL SIZE
centos-6-x86_64   latest            1014358cbf3b      About a minute ago   326.4 MB
centos_new1         latest            3e4be15719e0      24 hours ago         268 MB
test-img            latest            bb3d629a7cbc      3 weeks ago          196.6 MB
centos            latest            bb3d629a7cbc      3 weeks ago          196.6 MB  4、导出镜像

# docker save -o centos_net_01.tar bb3d629a7cbc
# du -sh centos_net_01.tar
195Mcentos_net_01.tar  5、强制删除镜像
# docker rmi -f bb3d629a7cbc
Untagged: centos:latest
Untagged: test-img:latest  6、恢复镜像
# docker load < centos_net_img2.tar
# docker images
REPOSITORY          TAG               IMAGE ID            CREATED             VIRTUAL SIZE
centos-6-x86_64   latest            1014358cbf3b      18 minutes ago      326.4 MB
<none>            <none>            3e4be15719e0      24 hours ago      268 MB
# docker tag 3e4be15719e0 centos_net_img:latest
# docker images
REPOSITORY          TAG               IMAGE ID            CREATED             VIRTUAL SIZE
centos-6-x86_64   latest            1014358cbf3b      21 minutes ago      326.4 MB
centos_net_img      latest            3e4be15719e0      24 hours ago      268 MB  
页: [1]
查看完整版本: Docker安装与镜像管理(一)