首页 > 代码库 > 安装docker

安装docker

                                                             docker

环境:

  centos 6.5 64位

默认装完系统的话centos内核是

[root@localhost ~]# uname -a
Linux localhost.localdomain 2.6.32-431.el6.x86_64

 

 

docker支持的内核是3.xx以上版本 系统必须是64位的

 

 

一、首先先升级自己本身的内核等级2.6-》3.xx

步骤:

[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.mbaRhP: 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镜像

[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。

我们还可以搜索基于 Fedora 和 Ubuntu 操作系统的容器。

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

查看docker镜像

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

运行docker运行shell

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

停止容器

[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@linuxidc ~]# 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_linuxidc-LogVol01
                      49212216  19620096  27069192  43% /etc/resolv.conf
/dev/mapper/vg_linuxidc-LogVol01
                      49212216  19620096  27069192  43% /etc/hostname
/dev/mapper/vg_linuxidc-LogVol01
                      49212216  19620096  27069192  43% /etc/hosts
/dev/mapper/vg_linuxidc-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@linuxidc ~]# 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@linuxidc ~]# docker commit fe943ba99287 web  #提交容器到镜像  web 是为镜像起的名字
49bc0d42b01698a462868f2ecaedfec267fed02c393f1ad06ec17b0d1012eff2
[root@linuxidc ~]# 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@linuxidc ~]# 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

查看镜像的构建历史

 

注意:如果发现docker起不来

[root@localhost ~]# service docker status
docker 已死,但 pid 文件仍存

首先要检查你的内核版本号是否正确,如果没更新过来需要更新

[root@localhost ~]# yum upgrade device-mapper-libs
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
* base: mirror.lzu.edu.cn
* epel: mirror.premi.st
* extras: mirror.bit.edu.cn
* updates: mirror.lzu.edu.cn
Setting up Upgrade Process
Resolving Dependencies
--> Running transaction check
---> Package device-mapper-libs.x86_64 0:1.02.79-8.el6 will be updated
--> Processing Dependency: device-mapper-libs = 1.02.79-8.el6 for package: device-mapper-1.02.79-8.el6.x86_64
---> Package device-mapper-libs.x86_64 0:1.02.117-7.el6_8.1 will be an update
--> Running transaction check
---> Package device-mapper.x86_64 0:1.02.79-8.el6 will be updated
--> Processing Dependency: device-mapper = 1.02.79-8.el6 for package: device-mapper-event-1.02.79-8.el6.x86_64
---> Package device-mapper.x86_64 0:1.02.117-7.el6_8.1 will be an update
--> Running transaction check
---> Package device-mapper-event.x86_64 0:1.02.79-8.el6 will be updated
---> Package device-mapper-event.x86_64 0:1.02.117-7.el6_8.1 will be an update
--> Processing Dependency: device-mapper-event-libs = 1.02.117-7.el6_8.1 for package: device-mapper-event-1.02.117-7.el6_8.1.x86_64
--> Running transaction check
---> Package device-mapper-event-libs.x86_64 0:1.02.79-8.el6 will be updated
---> Package device-mapper-event-libs.x86_64 0:1.02.117-7.el6_8.1 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

===================================================================================================================================================================================
Package Arch Version Repository Size
===================================================================================================================================================================================
Updating:
device-mapper-libs x86_64 1.02.117-7.el6_8.1 updates 256 k
Updating for dependencies:
device-mapper x86_64 1.02.117-7.el6_8.1 updates 211 k
device-mapper-event x86_64 1.02.117-7.el6_8.1 updates 133 k
device-mapper-event-libs x86_64 1.02.117-7.el6_8.1 updates 125 k

Transaction Summary
===================================================================================================================================================================================
Upgrade 4 Package(s)

Total download size: 725 k
Is this ok [y/N]: y
Downloading Packages:
(1/4): device-mapper-1.02.117-7.el6_8.1.x86_64.rpm | 211 kB 00:00
(2/4): device-mapper-event-1.02.117-7.el6_8.1.x86_64.rpm | 133 kB 00:00
(3/4): device-mapper-event-libs-1.02.117-7.el6_8.1.x86_64.rpm | 125 kB 00:00
(4/4): device-mapper-libs-1.02.117-7.el6_8.1.x86_64.rpm | 256 kB 00:00
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 121 kB/s | 725 kB 00:06
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Updating : device-mapper-libs-1.02.117-7.el6_8.1.x86_64 1/8
Updating : device-mapper-1.02.117-7.el6_8.1.x86_64 2/8
Updating : device-mapper-event-libs-1.02.117-7.el6_8.1.x86_64 3/8
Updating : device-mapper-event-1.02.117-7.el6_8.1.x86_64 4/8
Cleanup : device-mapper-event-1.02.79-8.el6.x86_64 5/8
Cleanup : device-mapper-event-libs-1.02.79-8.el6.x86_64 6/8
Cleanup : device-mapper-1.02.79-8.el6.x86_64 7/8
Cleanup : device-mapper-libs-1.02.79-8.el6.x86_64 8/8
Verifying : device-mapper-event-1.02.117-7.el6_8.1.x86_64 1/8
Verifying : device-mapper-event-libs-1.02.117-7.el6_8.1.x86_64 2/8
Verifying : device-mapper-1.02.117-7.el6_8.1.x86_64 3/8
Verifying : device-mapper-libs-1.02.117-7.el6_8.1.x86_64 4/8
Verifying : device-mapper-1.02.79-8.el6.x86_64 5/8
Verifying : device-mapper-event-libs-1.02.79-8.el6.x86_64 6/8
Verifying : device-mapper-event-1.02.79-8.el6.x86_64 7/8
Verifying : device-mapper-libs-1.02.79-8.el6.x86_64 8/8

Updated:
device-mapper-libs.x86_64 0:1.02.117-7.el6_8.1

Dependency Updated:
device-mapper.x86_64 0:1.02.117-7.el6_8.1 device-mapper-event.x86_64 0:1.02.117-7.el6_8.1 device-mapper-event-libs.x86_64 0:1.02.117-7.el6_8.1

Complete!

[root@localhost ~]# service docker restart
停止 docker: [确定]
Starting docker: [确定]
[root@localhost ~]# service docker status
docker (pid 3692) 正在运行...
[root@localhost ~]#

安装docker