码迷,mamicode.com
首页 > 其他好文 > 详细

Docker 入门

时间:2014-10-17 18:58:53      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:docker

Docker是什么

Docker is an open platform for developers and sysadmins to build, ship, and run distributed applications. Consisting of Docker Engine, a portable, lightweight runtime and packaging tool, and Docker Hub, a cloud service for sharing applications and automating workflows, Docker enables apps to be quickly assembled from components and eliminates the friction between development, QA, and production environments. As a result, IT can ship faster and run the same app, unchanged, on laptops, data center VMs, and any cloud.(官方给出的定义)


Docker和Virtual Machine区别

bubuko.com,布布扣bubuko.com,布布扣

            (virtual machine)                             (docker)

有图有真相,少了hypervisor这层,难怪性能好多了!

Docker组成

先来看下docker构成图

bubuko.com,布布扣


Docker主要由container(容器)和image(映像)组成。

Docker使用了一种称为aufs(需要kernel支持)的文件系统,这种文件文件系统可以层层叠加修改文件,最底下的文件系统是只读的,如果需要修改文件,aufs会增加一个可写的层,这个层就是container。不同的container可以共享底层的只读文件系统。

container是可写的层,通过dock commit 就可以将你的container变成一个新的只读的image。


Docker安装

环境:centos 6.5 x86_64 (centos7默认源自带docker软件包)

[root@compute1 ~]# rpm -Uvh http://mirrors.ustc.edu.cn/fedora/epel/6/x86_64/epel  # 安装epel源
[root@compute1 ~]# yum install docker-io    # 安装docker软件包
[root@compute1 ~]# service docker start      # 启动docker服务
[root@compute1 ~]# chkconfig docker on    # docker服务开机自启动


制作Docker映像

制作docker映像有好多种,可以看官方介绍 https://docs.docker.com/articles/baseimages/

这里我使用febootstrap(在epel源)来制作一个可以ssh的centos 6.5的docker映像

yum install febootstrap # 安装febootstrap软件包
febootstrap -i bash -i wget -i yum -i iputils -i iproute -i man -i vim-minimal -i openssh-server -i openssh-clients centos6.5 centos6.5-image https://mirrors.ustc.edu.cn/centos/6.5/os/x86_64/  # 安装需要的软件包,centos6.5-image是centos映像文件夹,有玩过chroot的童鞋应该都懂这个
cd centos6.5-image/ &&  tar -c . | docker import - centos6.5-base    # 创建docker 映像
[root@compute1 ~]# docker images  # 查看docker映像
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos6.5-base      latest              3f9c6605aa3b        6 days ago          311.2 MB
# 制作可以ssh的docker映像,推荐安装软件包使用Dockfile来定义
[root@compute1 ~]# vim Dockerfile   # 编写Dockfile
#Dockerfile
FROM centos6.5-base                                                                  # 定义基础映像
MAINTAINER ice <yao.xiabing@99cloud.net>                            # 作者
RUN ssh-keygen -q -N "" -t dsa -f /etc/ssh/ssh_host_dsa_key         # RUN 可以执行shell命令
RUN ssh-keygen -q -N "" -t rsa -f /etc/ssh/ssh_host_rsa_key
RUN sed -ir ‘s/(.*pam_loginuid.so)/#\1/g‘ /etc/pam.d/sshd               
RUN mkdir -p /root/.ssh && chown root.root /root && chmod 700 /root/.ssh  
EXPOSE 22                                                           # 对外暴露22端口
RUN echo ‘root:99cloud‘ | chpasswd                   # 配置root密码
ENV LANG en_US.UTF-8                                        # 配置环境变量
ENV LC_ALL en_US.UTF-8
CMD /usr/sbin/sshd -D                                          # CMD 表示构建映像时不会运行后面跟的命令,在实例化时才运行
#End
[root@compute1 ~]# ls /root/Dockerfile     # 这里看清楚Dockfile的路径,
/root/Dockerfile
[root@compute1 ~]# docker build -t centos6.5-ssh /root/    # 后面跟Dockfile的上一级目录
[root@compute1 ~]# docker images   # 查看docker映像
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos6.5-ssh       latest              5009964eb87d        6 days ago          311.3 MB
centos6.5-base      latest              3f9c6605aa3b        6 days ago          311.2 MB
[root@compute1 ~]# docker run -d -p 127.0.0.1:2222:22 centos6.5-ssh   # -p 映射docker实例的22端口到本地的2222端口,如果不加-p选项,会随机选择一个端口做映射

更详细的Dockerfile介绍 https://docs.docker.com/reference/builder/


参考链接

http://my.oschina.net/feedao/blog/223795

http://failshell.io/docker/building-a-centos-docker-base-image/


本文出自 “the-way-to-cloud” 博客,请务必保留此出处http://iceyao.blog.51cto.com/9426658/1565235

Docker 入门

标签:docker

原文地址:http://iceyao.blog.51cto.com/9426658/1565235

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!