CentOS6.5安裝Docker

筆者這裏是用CnetOS6.5系統html

 開始安裝daoker之旅:linux

[root@localhost ~]# uname -r
2.6.32-431.el6.x86_64
[root@localhost ~]# cat /etc/issue
CentOS release 6.5 (Final)
Kernel \r on an \m

注意其餘的源可能致使你的內核和docker的版本不一致,須要升級內核至3.x。web

安裝:
[root@localhost ~]# rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
Retrieving http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
warning: /var/tmp/rpm-tmp.JN76fI: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing...                ########################################### [100%]
   1:epel-release           ########################################### [100%]
[root@localhost ~]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
[root@localhost ~]# yum -y install docker-io



啓動並設置開機自動啓動docker

[root@localhost ~]# service docker start
Starting cgconfig service:                                 [肯定]
Starting docker:	                                   [肯定]
[root@localhost ~]# chkconfig docker on



獲取cnetos鏡像shell

[root@localhost ~]# docker pull centos:latest
centos:latest: The image you are pulling has been verified
511136ea3c5a: Pull complete 
5b12ef8fd570: Pull complete 
34943839435d: Downloading [===>                                               ] 18.38 MB/232.5 MB 1h7m49s

#官方安裝方式docker pull imagename從docker的索引中心下載,imagename是鏡像名稱,例如docker pull ubuntu就是下載base ubuntu而且tag是latest。 apache

咱們還能夠搜索基於 Fedora 和 Ubuntu 操做系統的容器。 ubuntu

[root@localhost ~]# docker search ubuntu
[root@localhost ~]# docker search fedora

查看docker鏡像centos

[root@localhost ~]# docker images centos
REPOSITORY          TAG                 IMAGE ID            CREATED                  VIRTUAL SIZE
centos              latest              34943839435d        Less than a second ago   224 MB



運行docker運行shellbash

[root@localhost ~]# docker run -i -t centos /bin/bash
[root@2ce733141ece /]# 
[root@2ce733141ece /]# 
[root@2ce733141ece /]# 
[root@2ce733141ece /]#



中止容器app

[root@localhost ~]# docker stop <CONTAINER ID>


刪除全部容器

docker rm $(docker ps -a -q)

查看docker的子命令,直接敲docker 或完整的docker help 就能夠

經常使用命令

總結一下經常使用命令:

其中<>闊起來的參數爲必選,[]闊起來爲可選

  • docker version 查看docker的版本號,包括客戶端、服務端、依賴的Go等

  • docker info 查看系統(docker)層面信息,包括管理的images, containers數等

  • docker search <image> 在docker index中搜索image

  • docker pull <image> 從docker registry server 中下拉image

  • docker push <image|repository> 推送一個image或repository到registry

  • docker push <image|repository>:TAG 同上,指定tag

  • docker inspect <image|container> 查看image或container的底層信息

  • docker images TODO filter out the intermediate image layers (intermediate image layers 是什麼)

  • docker images -a 列出全部的images

  • docker ps 默認顯示正在運行中的container

  • docker ps -l 顯示最後一次建立的container,包括未運行的

  • docker ps -a 顯示全部的container,包括未運行的

  • docker logs <container> 查看container的日誌,也就是執行命令的一些輸出

  • docker rm <container...> 刪除一個或多個container 

  • docker rm `docker ps -a -q` 刪除全部的container

  • docker ps -a -q | xargs docker rm 同上, 刪除全部的container

  • docker rmi <image...> 刪除一個或多個image

  • docker start/stop/restart <container> 開啓/中止/重啓container

  • docker start -i <container> 啓動一個container並進入交互模式

  • docker attach <container> attach一個運行中的container

  • docker run <image> <command> 使用image建立container並執行相應命令,而後中止

  • docker run -i -t <image> /bin/bash 使用image建立container並進入交互模式, login shell是/bin/bash

  • docker run -i -t -p <host_port:contain_port> 將container的端口映射到宿主機的端口

  • docker commit <container> [repo:tag] 將一個container固化爲一個新的image,後面的repo:tag可選

  • docker build <path> 尋找path路徑下名爲的Dockerfile的配置文件,使用此配置生成新的image

  • docker build -t repo[:tag] 同上,能夠指定repo和可選的tag

  • docker build - < <dockerfile> 使用指定的dockerfile配置文件,docker以stdin方式獲取內容,使用此配置生成新的image

  • docker port <container> <container port> 查看本地哪一個端口映射到container的指定端口,其實用docker ps 也能夠看到


docker 把宿主機的目錄掛載到docker容器裏

[root@kcw ~]# docker run -t -i -v /root/convirt-install:/mnt/convirt docker.cn/docker/centos:centos5 /bin/bash  #將宿主機目錄掛載到容器裏 -t 表示分配僞終端 -i表示以交互式方式運行 -v表示本地那個目錄 :後面的是容器目錄
[root@fe943ba99287 /]# df
Filesystem           1K-blocks      Used Available Use% Mounted on
rootfs                10190136    530928   9118536   6% /
/dev/mapper/docker-253:0-1314559-fe943ba9928734269761263a95fc81b2208544b976e79b6b55d397dc35961c38
                      10190136    530928   9118536   6% /
tmpfs                   989592         0    989592   0% /dev
shm                      65536         0     65536   0% /dev/shm
/dev/mapper/vg_kcw-LogVol01
                      49212216  19620096  27069192  43% /etc/resolv.conf
/dev/mapper/vg_kcw-LogVol01
                      49212216  19620096  27069192  43% /etc/hostname
/dev/mapper/vg_kcw-LogVol01
                      49212216  19620096  27069192  43% /etc/hosts
/dev/mapper/vg_kcw-LogVol01
                      49212216  19620096  27069192  43% /mnt/convirt
tmpfs                   989592         0    989592   0% /proc/kcore
[root@fe943ba99287 /]# cd /mnt/convirt/
[root@fe943ba99287 convirt]# ls
common  convirt-install-2.1.1.tar.gz  install

建立容器並提交鏡像

在容器裏安裝程序而且提交到鏡像裏

在docker容器裏安裝一個httpd用來測試,裝完退出

[root@kcw ~]# docker ps -a
CONTAINER ID        IMAGE                       COMMAND             CREATED             STATUS                      PORTS               NAMES
fe943ba99287        docker.cn/docker/centos:5   "/bin/bash"         17 minutes ago      Exited (0) 20 seconds ago                       distracted_archimedes   
記住容器ID

提交容器到鏡像

[root@kcw ~]# docker commit fe943ba99287 web   #提交容器到鏡像  web 是爲鏡像起的名字
49bc0d42b01698a462868f2ecaedfec267fed02c393f1ad06ec17b0d1012eff2
[root@kcw ~]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
web                       latest              49bc0d42b016        54 seconds ago      532.9 MB  #這是提交的鏡像
docker.cn/docker/centos   5                   bac0c97c3010        3 weeks ago         466.9 MB
docker.cn/docker/centos   centos5             bac0c97c3010        3 weeks ago         466.9 MB
centos                    6                   510cf09a7986        3 weeks ago         215.8 MB
centos                    centos6             510cf09a7986        3 weeks ago         215.8 MB
docker.cn/docker/centos   7                   8efe422e6104        3 weeks ago         224 MB
docker.cn/docker/centos   centos7             8efe422e6104        3 weeks ago         224 MB
docker.cn/docker/centos   latest              8efe422e6104        3 weeks ago         224 MB

映射端口啓動機鏡像

[root@kcw ~]# docker run -d -p 80:80 --name apacheWEB web:latest /etc/init.d/httpd start
9eee2939f55a587d8a67b5ce5863318452c16c5254de608bdbc9a912d3ecee46

  • docker images

  • 顯示鏡像列表

  • docker ps

  • 顯示容器列表

  • docker run IMAGE_ID

  • 指定鏡像, 運行一個容器

  • docker start/stop/pause/unpause/kill/restart CONTAINER_ID

  • 操做容器狀態

  • docker tag IMAGE_ID [REGISTRYHOST/][USERNAME/]NAME[:TAG]

  • 給指定鏡像命名

  • docker pull/push NAME:TAG

  • 下載, 推送鏡像到 Docker registry server , NAME 部分包括了服務地址

  • docker rm/rmi CONTAINER_ID/IMAGE_ID

  • 刪除容器, 鏡像

  • docker inspect CONTAINER_ID/IMAGE_ID

  • 查看細節信息

  • docker top CONTAINER_ID

  • 查看指定的運行容器的進程狀況

  • docker info

  • 查看系統配置信息

  • docker save/load

  • 保存, 恢復鏡像信息

  • docker commit CONTAINER_ID

  • 從容器建立鏡像

  • docker export > xxx.tar

  • 保存一個容器

  • docker import - < xxx.tar

  • 恢復一個容器

  • docker cp CONTAINER_ID:PATH HOSTPATH

  • 從鏡像複製文件到實體機

  • docker diff CONTAINER_ID

  • 查看容器相對於鏡像的文件變化

  • docker logs CONTAINER_ID

  • 查看容器日誌

  • docker build

  • 從 Dockerfile 構建鏡像

  • docker history IMAGE_ID

  • 查看鏡像的構建歷史

http://www.linuxidc.com/Linux/2014-07/104595.htm

Centos7安裝docker 點擊這裏  http://linux.cn/article-4340-1.html

docker入門與實戰點擊這裏  http://www.dockerpool.com/static/books/docker_practice/index.html

docker究竟是什麼點擊這裏  http://cloud.51cto.com/art/201411/457407.htm

CentOS6.5內核升級看這裏http://cn.soulmachine.me/blog/page/3/

docker other  http://docker.widuu.com/

本文摘自互聯網若有雷同,純屬正常

相關文章
相關標籤/搜索