您的位置:首页 > 运维架构 > Docker

Docker安装与镜像管理(一)

2016-03-30 22:21 465 查看
一、安装docker

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

系统:
[root@kvm ~]# 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

[root@kvm ~]# cat /etc/issue
CentOS release 6.7 (Final)
Kernel \r on an \m

二、启动docker
[root@kvm ~]# /etc/init.d/docker start
Starting docker:	                                   [  OK  ]
[root@kvm ~]# ps -ef |grep docker
root       3900      1  1 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   3606  0 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 [image-name]
#搜索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镜像
[root@kvm ~]# 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、查看下载到本地镜像
[root@kvm ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              latest              bb3d629a7cbc        3 weeks ago         196.6 MB
3、查看docker仓库镜像
[root@kvm ~]# docker search centos
NAME                            DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
centos                          The official build of CentOS.                   2069      [OK]
jdeathe/centos-ssh              CentOS-6 6.7 x86_64 / CentOS-7 7.2.1511 x8...   18                   [OK]
jdeathe/centos-ssh-apache-php   CentOS-6 6.7 x86_64 / Apache / PHP / PHP M...   14                   [OK]
million12/centos-supervisor     Base CentOS-7 with supervisord launcher, h...   10                   [OK]
blalor/centos                   Bare-bones base CentOS 6.5 image                8                    [OK]
nimmis/java-centos              This is docker images of CentOS 7 with dif...   8                    [OK]
torusware/speedus-centos        Always updated official CentOS docker imag...   7                    [OK]
centos/mariadb55-centos7                                                        3                    [OK]
nathonfowlie/centos-jre         Latest CentOS image with the JRE pre-insta...   3                    [OK]
nickistre/centos-lamp           LAMP on centos setup                            3                    [OK]
consol/sakuli-centos-xfce       Sakuli end-2-end testing and monitoring co...   2                    [OK]
softvisio/centos                Centos                                          1                    [OK]
layerworx/centos                CentOS container with etcd, etcdctl, confd...   1                    [OK]
yajo/centos-epel                CentOS with EPEL and fully updated              1                    [OK]
lighthopper/orientdb-centos     A Dockerfile for creating an OrientDB imag...   1                    [OK]
darksheer/centos                Base Centos Image -- Updated hourly             1                    [OK]
timhughes/centos                Centos with systemd installed and running       1                    [OK]
januswel/centos                 yum update-ed CentOS image                      0                    [OK]
drokar/centos-s6                centOS combined with s6, a small process s...   0                    [OK]
lighthopper/openjdk-centos      A Dockerfile for creating an OpenJDK image...   0                    [OK]
ustclug/centos                   USTC centos                                    0                    [OK]
blacklabelops/centos            CentOS Base Image! Built and Updates Daily!     0                    [OK]
grayzone/centos                 auto build for centos.                          0                    [OK]
jsmigel/centos-epel             Docker base image of CentOS w/ EPEL installed   0                    [OK]
grossws/centos                  CentOS 6 and 7 base images with gosu and l...   0                    [OK]


4、开启一个容器docker run -it centos /bin/bash
[root@kvm ~]# docker run -it centos /bin/bash
[root@8c1fd812d079 /]#


[root@8c1fd812d079 /]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@8c1fd812d079 /]# 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

[root@8c1fd812d079 /]# 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
[root@8c1fd812d079 /]# ps
PID TTY          TIME CMD
1 ?        00:00:00 bash
16 ?        00:00:00 ps
[root@8c1fd812d079 /]# uptime
11:55:21 up 42 min,  0 users,  load average: 0.00, 0.02, 0.01
[root@8c1fd812d079 /]# df -Th
Filesystem                                                                                      Type   Size  Used Avail Use% Mounted on
/dev/mapper/docker-8:3-1573967-8c1fd812d0790d1868bcec389a50c6f7a08c6c099167141393af0961e7b850be ext4   9.8G  230M  9.0G   3% /
tmpfs                                                                                           tmpfs  1.9G     0  1.9G   0% /dev
shm                                                                                             tmpfs   64M     0   64M   0% /dev/shm
/dev/sda3                                                                                       ext4    43G  4.6G   36G  12% /etc/hosts
[root@8c1fd812d079 /]# exit


[root@kvm ~]# 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、修改镜像名称
[root@kvm ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              latest              bb3d629a7cbc        3 weeks ago         196.6 MB
[root@kvm ~]# docker tag centos test-img
[root@kvm ~]# 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
[root@kvm ~]# docker tag test-img centos:test-img2
[root@kvm ~]# 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、删除镜像
[root@kvm ~]# docker rmi centos:test-img2
Untagged: centos:test-img2
[root@kvm ~]# 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、启动镜像

[root@kvm ~]# docker start 8c1fd
8c1fd
[root@kvm ~]# 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

[root@kvm ~]# docker exec -it 8c1fd /bin/bash
[root@8c1fd812d079 /]#
3、在容器中安装软件(安装网卡包net-tools)

[root@8c1fd812d079 /]# yum install net-tools wget -y
[root@8c1fd812d079 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
inet 172.17.0.3  netmask 255.255.0.0  broadcast 0.0.0.0
inet6 fe80::42:acff:fe11:3  prefixlen 64  scopeid 0x20<link>
ether 02:42:ac:11:00:03  txqueuelen 0  (Ethernet)
RX packets 5419  bytes 10411538 (9.9 MiB)
RX errors 0  dropped 0  overruns 0  frame 0
TX packets 4127  bytes 268377 (262.0 KiB)
TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
inet 127.0.0.1  netmask 255.0.0.0
inet6 ::1  prefixlen 128  scopeid 0x10<host>
loop  txqueuelen 0  (Local Loopback)
RX packets 0  bytes 0 (0.0 B)
RX errors 0  dropped 0  overruns 0  frame 0
TX packets 0  bytes 0 (0.0 B)
TX errors 0  dropped 0 overruns 0  carrier 0  collisions 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

[root@kvm ~]# docker commit -m "centos_with_nettools_and_wget" -a "author" 8c1fd812d079 centos_new1
3e4be15719e03e909f39a7c03f3c7cd4fa6f2a8e282b2b88204d0224104b20e6
[root@kvm ~]# 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、导入镜像
[root@kvm src]# cat centos-6-x86_64-minimal.tar.gz | docker import - centos-6-x86_64
1014358cbf3b643cae13e830afc539cbf0e02aa40f6c77583898a0cbd33a9561
查看镜像

[root@kvm src]# 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、导出镜像

[root@kvm src]# docker save -o centos_net_01.tar bb3d629a7cbc
[root@kvm src]# du -sh centos_net_01.tar
195M	centos_net_01.tar
5、强制删除镜像
[root@kvm src]# docker rmi -f bb3d629a7cbc
Untagged: centos:latest
Untagged: test-img:latest
6、恢复镜像
[root@kvm src]# docker load < centos_net_img2.tar
[root@kvm src]# 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

[root@kvm src]# docker tag 3e4be15719e0 centos_net_img:latest
[root@kvm src]# 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  docker