docker定义、安装、镜像、容器

Docker是什么

  • 使用最广泛的开源容器引擎
  • 一种操作系统级的虚拟化技术
  • 依赖于Linux内核特性:Namespace(资源隔离)和Cgroups(资源限制)
  • 一个简单的应用程序打包工具

Docker设计目标

  • 提供简单的应用程序打包工具
  • 开发人员和运维人员职责逻辑分离
  • 多环境保持一致性

docker定义、安装、镜像、容器

docker定义、安装、镜像、容器

像一艘货船,通过集装箱把代码打包,分开,然后再传输

Docker基本组成

Docker Client:客户端
Ddocker Daemon:守护进程
Docker Images:镜像
Docker Container:容器
Docker Registry:镜像仓库

docker定义、安装、镜像、容器

容器 VS 虚拟机

区别:
docker定义、安装、镜像、容器

传统的vm:
底层都是基础建设(服务器,台式机)===》操作系统===》安装虚拟机软件(vmware、esxi)===>虚拟化系统

容器:
底层都是基础建设(服务器,台式机)===》操作系统===》docker引擎====》容器

docker定义、安装、镜像、容器

Docker应用场景

  • 应用程序打包和发布
  • 应用程序隔离
  • 持续集成
  • 部署微服务
  • 快速搭建测试环境
  • 提供PaaS产品(平台即服务)

Linux安装Docker

Docker版本

社区版(Community Edition,CE)
企业版(Enterprise Edition,EE)

官网:https://docs.docker.com/install/linux/docker-ce/centos/

支持平台

Linux(CentOS,Debian,Fedora,Oracle Linux,RHEL,SUSE和Ubuntu)
Mac
Windows

CentOS7.x安装Docker

关闭selinux
关闭防火墙firewalld
systemctl stop firewalld
systemctl disable firewalld

#安装依赖包
yum install -y yum-utils device-mapper-persistent-data lvm2
#添加Docker软件包源(安装源,安装阿里的)
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo

yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 

#安装Docker CE
yum install -y docker-ce

#启动Docker服务并设置开机启动
systemctl start docker
systemctl enable docker

加速器:表示pull镜像快点
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io

执行后重启:
systemctl restart docker

[root@docker ~]# docker info
有警告
docker定义、安装、镜像、容器
解决:
在CentOS中
    vim /etc/sysctl.conf 
     net.bridge.bridge-nf-call-ip6tables = 1
     net.bridge.bridge-nf-call-iptables = 1
     net.bridge.bridge-nf-call-arptables = 1
重启系统。

镜像管理

镜像是什么

  • 一个分层存储的文件
  • • 一个软件的环境
  • • 一个镜像可以创建N个容器
  • • 一种标准化的交付
  • • 一个不包含Linux内核而又精简的Linux操作系统
  • 镜像不是一个单一的文件,而是有多层构成。我们可以通过docker history <ID/NAME> 查看镜像中各层内容及大小,每层
  • 对应着Dockerfile中的一条指令。Docker镜像默认存储在/var/lib/docker/\<storage-driver\>中。

镜像从哪里来?
Docker Hub是由Docker公司负责维护的公共注册中心,包含大量的容器镜像,Docker工具默认从这个公共镜像库下载镜像。
地址:https://hub.docker.com/explore

配置镜像加速器:https://www.daocloud.io/mirror
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io

镜像与容器联系:
比如我从一个centos镜像(大小:100M)中新建3个容器(3个容器总共大小 还是100M,因为是在容器之上进行读写的,大大提高磁盘利用率)
docker定义、安装、镜像、容器

管理镜像常用命令:
docker定义、安装、镜像、容器

拉去镜像:docker pull centos:7
例子:

[root@docker ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               1.15                e81eb098537d        9 days ago          109MB
centos              7                   75835a67d134        6 weeks ago         200MB
centos              latest              75835a67d134        6 weeks ago         200MB
导出镜像:
[root@docker ~]# docker image save centos:7 > centos7.tar 
[root@docker ~]# du -sh centos7.tar 
200M    centos7.tar

删除镜像:
[root@docker ~]# docker image rm centos:7
Untagged: centos:7
[root@docker ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               1.15                e81eb098537d        9 days ago          109MB
centos              latest              75835a67d134        6 weeks ago         200MB

镜像导入:
[root@docker ~]# docker image load < centos7.tar 

容器管理

创建容器常用选项
docker定义、安装、镜像、容器

错误:
WARNING: IPv4 forwarding is disabled. Networking will not work.
解决:
[root@docker ~]# echo "net.ipv4.ip_forward=1" >> /usr/lib/sysctl.d/00-system.conf
[root@docker ~]# systemctl restart network && systemctl restart docker

创建容器:
[root@docker ~]# docker run -d -p 88:80 --name=php-icar -h icar1 nginx
-p 宿主机端口:容器端口
--name=php-icar :容器的名称
-h icar1:表示容器主机的名称
nginx:镜像

访问:curl 192.168.1.13:88

查看日志:
[root@docker ~]# docker logs php-icar -f
[root@docker ~]# docker logs 2298942019ef -f

![](http://i2.51cto.com/images/blog/201811/26/ab56f940a4acfd0e35310e59f7348775.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

容器资源限制
可以限制下,以免一台机资源占用过多

docker定义、安装、镜像、容器

[root@docker ~]# docker image inspect centos 镜像信息
内存限额:
允许容器最多使用500M内存和600M的Swap,并禁用 OOM Killer:

CPU限额:
允许容器最多使用一个半的CPU:
docker run -d --name nginx04 --cpus="1.5" nginx
允许容器最多使用50%的CPU:
docker run -d --name nginx05 --cpus=".5" nginx

**
[root@docker ~]# docker run -d --name nginx07 -m 1G --memory-swap="600m" --cpus=".1" --oom-kill-disable nginx

[root@docker ~]# docker stats nginx08 --no-stream
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
9cbd71b8d7ce nginx08 0.00% 3.742MiB / 1GiB 0.37% 648B / 0B 0B / 0B 2**

[root@docker ~]# docker top nginx08 查看完整命令
UID PID PPID C STIME TTY TIME CMD
root 6107 6083 0 12:15 ? 00:00:00 nginx: master process nginx -g daemon off;
101 6142 6107 0 12:15 ? 00:00:00 nginx: worker process

进入容器:[root@docker ~]# docker exec -it nginx08 bash

猜你喜欢

转载自blog.51cto.com/jacksoner/2321989