Docker介绍

官网 www.docker.com github https://github.com/docker/docker.github.io 开源的容器引擎,可以让开发者打包应用以及依赖的库,然后发布到任何流行的linux发行版上,移植很方便 由go语言编写,基于apache2.0协议发布 基于linux kernel,要想在win 运行需要借助一个vm(虚拟机)来实现

Docker与传统的虚拟化比较

Docker的优势

启动非常快,秒级实现 资源利用率高,一台高配置服务器可以跑上千个docker容器 更快的交付和部署,一次创建和配置后,可以在传任意地方运行 内核级别的虚拟化,不需要额外的hypevisor支持,会有更高的性能和效率 易迁移,平台依赖性不强

特性 容器 虚拟机
启动 秒级 分钟级
硬盘使用 一般为MB 一般为GB
性能 接近原生 弱于
系统支持量 单机支持上千个容器 一般几十个

Docker核心概念

  • 镜像,是一个只读的模板,类似于安装系统用到的那个iso文件,我们通过镜像来完成各种应用的部署
  • 容器,镜像类似于操作系统,而容器类似于虚拟机本身。它可以被启动、开始、停止、删除等操作,每个容器都是相互隔离的。
  • 仓库,存放镜像的一个场所,仓库分为公开仓库和私有仓库。最大的公开仓库是Docker hub(hub.docker.com),国内公开仓库(dockerpool.com).

Docker安装

Docker下载与安装

[root@localhost yum.repos.d]# curl https://download.docker.com/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker.repo
[root@localhost yum.repos.d]# yum install docker-ce -y

查看docker-ce的版本

# yum list docker-ce.x86_64  --showduplicates | sort -r

安装指定版本的docker-ce

# yum install docker-ce-<version>

启动docker,查看进程

[root@localhost yum.repos.d]# systemctl start docker.service

启动后,会自动生成一些防火墙规则

镜像管理

拉取 centos 镜像

[root@localhost ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
469cfcc7a4b3: Pull complete 
Digest: sha256:bc494daa9d9ad7e37f93236fbd2c3f372739997c6336ef3c321e227f336e73d3
Status: Downloaded newer image for centos:latest

配置docker加速器

阿里云加速器

登录地址https://dev.aliyun.com/search.html 注册一个账号 点击“创建我的容器镜像”--->镜像加速器 按照上面的配置即可。

DaoCloud加速器

登入到 https://www.daocloud.io/mirror#accelerator-doc 注册账号

查看本地的镜像

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              e934aafc2206        5 hours ago         199MB

搜索镜像

[root@localhost ~]# docker search keywords // 例如搜索jumpserver
[root@localhost ~]# docker search jumpserver

给镜像打标签

[root@localhost ~]# docker tag centos apeng_centos
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
apeng_centos        latest              e934aafc2206        6 hours ago         199MB
centos              latest              e934aafc2206        6 hours ago         199MB
ubuntu              latest              f975c5035748        4 weeks ago         112MB

将镜像启动为容器

-i 表示容器的标准输入打开 -t 表示分配的伪终端 -d 表示后台启动

查看启动状态的容器,加上-a 选项可查看所有的容器

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
a8dfe32a2be7        centos              "/bin/bash"         2 minutes ago       Up 2 minutes                            eloquent_wing

删除镜像

[root@localhost ~]# docker rmi aming_centos:235123
Untagged: aming_centos:235123

通过容器创建镜像

镜像启动为容器之后,进入一个启动的容器

docker exec [OPTIONS] CONTAINER COMMAND [ARG...] [flags]

//拉取下来的centos系统大小很小,需要的软件自己安装
[root@localhost ~]# docker exec -it a8dfe32a2 /bin/bash
[root@a8dfe32a2be7 /]# ifconfig
bash: ifconfig: command not found
[root@a8dfe32a2be7 /]# yum install net-tools -y

通过容器创建由自己定义环境的镜像

[root@localhost ~]# docker commit -m "install_net-tools" -a "author:apeng" a8dfe32a2be7 new_centos

此时系统会产生一个new_cnetos的镜像

通过 docker build 创建一个基于centos的httpd web服务器镜像

创建工作目录,创建Dockerfile文件

[root@localhost ~]# mkdir /docker-build
[root@localhost ~]# touch /docker-build/Dockerfile

编辑Dockerfile

FROM centos
MAINTAINER user1 <user1@163.com>
RUN yum -y install httpd
ADD start.sh /usr/local/bin/start.sh
ADD index.html /var/www/html/index.html

创建start.sh和index.html

[root@localhost ~]# cd /docker-build/
[root@localhost docker-build]# echo "/usr/sbin/httpd -DFOREGROUND" > start.sh
[root@localhost docker-build]# chmod a+x start.sh
[root@localhost docker-build]# echo "docker image build test" > index.html

使用命令build来创建新的image

[root@localhost docker-build]# docker build -t centos:httpd .

将镜像导出为tar包

[root@localhost ~]# docker save -o /opt/docker-centos-httpd.tar centos:httpd

将tar包导入为本地镜像

[root@localhost ~]# docker load -i /opt/docker-centos-httpd.tar

容器端口映射

将镜像centos:httpd运行为容器,将80端口映射到本机的9000,启动httpd

[root@localhost ~]# docker run -d -p 9000:80 centos:httpd /bin/sh -c /usr/local/bin/start.sh

查看端口

[root@localhost ~]# netstat -tlnp|grep 9000
tcp6       0      0 :::9000                 :::*                    LISTEN      13774/docker-proxy

在浏览器中访问容器的httpd服务的主页

在容器中添加test1.html

[root@localhost ~]# docker exec -it e5fcdc897c58 /bin/bash
[root@e5fcdc897c58 /]# echo "docker image test1 successful" > /var/www/html/test1.html

在浏览器中访问test1.html